Results 1 to 3 of 3

Thread: MySQL error

  1. #1
    Join Date
    Mar 2011
    Posts
    2

    Unanswered: MySQL error

    Hi!

    I've been trying to make a delete function to my website but when i try to delete all i get is this msg:

    Cannot delete or update a parent row: a foreign key constraint fails (`hotellromformidling`.`rombestilling`, CONSTRAINT `fk_bestilling_kunde` FOREIGN KEY (`kundeid`) REFERENCES `kunde` (`idkunde`) ON DELETE NO ACTION ON UPDATE NO ACTION)

    Here is my php-page script:

    <?php require_once('Connections/hotellromformidling.php'); ?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

    switch ($theType) {
    case "text":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "long":
    case "int":
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
    break;
    case "double":
    $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
    break;
    case "date":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "defined":
    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
    break;
    }
    return $theValue;
    }
    }

    if ((isset($_GET['slettID'])) && ($_GET['slettID'] != "")) {
    $deleteSQL = sprintf("DELETE FROM kunde WHERE idkunde=%s",
    GetSQLValueString($_GET['slettID'], "int"));

    mysql_select_db($database_hotellromformidling, $hotellromformidling);
    $Result1 = mysql_query($deleteSQL, $hotellromformidling) or die(mysql_error());
    }

    mysql_select_db($database_hotellromformidling, $hotellromformidling);
    $query_kunde = "SELECT idkunde, fornavn, etternavn, adresse, postnr, telefonnr, epost, passnr FROM kunde";
    $kunde = mysql_query($query_kunde, $hotellromformidling) or die(mysql_error());
    $row_kunde = mysql_fetch_assoc($kunde);
    $totalRows_kunde = mysql_num_rows($kunde);
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <table border="1">
    <tr>
    <td>idkunde</td>
    <td>fornavn</td>
    <td>etternavn</td>
    <td>adresse</td>
    <td>postnr</td>
    <td>telefonnr</td>
    <td>epost</td>
    <td>passnr</td>
    <td>&nbsp;</td>
    </tr>
    <?php do { ?>
    <tr>
    <td><?php echo $row_kunde['idkunde']; ?></td>
    <td><?php echo $row_kunde['fornavn']; ?></td>
    <td><?php echo $row_kunde['etternavn']; ?></td>
    <td><?php echo $row_kunde['adresse']; ?></td>
    <td><?php echo $row_kunde['postnr']; ?></td>
    <td><?php echo $row_kunde['telefonnr']; ?></td>
    <td><?php echo $row_kunde['epost']; ?></td>
    <td><?php echo $row_kunde['passnr']; ?></td>
    <td><a href="?slettID=<?php echo $row_kunde['idkunde']; ?>">Slett</a></td>
    </tr>
    <?php } while ($row_kunde = mysql_fetch_assoc($kunde)); ?>
    </table>
    </body>
    </html>
    <?php
    mysql_free_result($kunde);
    ?>


    Would really appreciate some help

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by Alexio View Post
    Cannot delete or update a parent row: a foreign key constraint fails (`hotellromformidling`.`rombestilling`, CONSTRAINT `fk_bestilling_kunde` FOREIGN KEY (`kundeid`) REFERENCES `kunde` (`idkunde`) ON DELETE NO ACTION ON UPDATE NO ACTION)
    this is where you should focus your attention

    the php script is of very little use in the mysql forum -- not all mysql users also use php, dontcha know

    are you having trouble understanding what the error message is telling you?

    why don't you give it a shot, see if you can figure out why you're getting that error

    hint: the problem lies in your data, not your script
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Mar 2011
    Posts
    2

    :)

    I managed to work it out. I chose the wrong table for deletion. That was the register table which is not supposed to be deleted, so I just changed the table to the correct one and it worked right away.

    Thanks for the help anyways! Like you said the answer lies in the error-msg.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •