If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > MySQL error

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-26-11, 08:17
Alexio Alexio is offline
Registered User
 
Join Date: Mar 2011
Posts: 2
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
Reply With Quote
  #2 (permalink)  
Old 03-26-11, 09:50
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-26-11, 10:17
Alexio Alexio is offline
Registered User
 
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On