Jump to content

Error in query.....a foreign key constrant fails...


Ander_3

Recommended Posts

I keep getting this error and can't figure out how to fix it... Error in query $sqlString Cannot add or update a child row: a foreign key constraint fails (`dvdrentals`.`transactions`, CONSTRAINT `transactions_ibfk_2` FOREIGN KEY (`DVDID`) REFERENCES `dvds` (`DVDID`)) This is the code causing the error: <?php//Build the insert statement$sqlString='insert into Transactions(OrderID, DVDID, DateOut, DateDue) values('.$orderId.', '.$dvdId.', "'.$dateOut.'", "'.$dateDue.'")';//Execute the insert statementmysql_query($sqlString) or die('Error in query $sqlString '.mysql_error());?>Thank you!

Edited by Ander_3
Link to comment
Share on other sites

I forgot to put this code in. This is from update.php. The code looks very similiar to the code I posted previously but this one does not have any errors: <?php//Build the update statement$sqlString='update Transactions set OrderID='.$orderId.', DVDID='.$dvdId.', DateOut="'.$dateOut.'", DateDue="'.$dateDue.'", DateIn="'.$dateIn.'" where TransID='.$transactionId.'';//Execute the update statementmysql_query($sqlString) or die('Error in query $sqlString' .mysql_error());?>

Link to comment
Share on other sites

how does 'transactions' table linked to another tables and what are the constraint of the linked tables column? it is possible that you are trying to insert something in child table where parent table does not have the reference of it.

Link to comment
Share on other sites

index.php: $selectSql='select '. 'Transactions.TransID, '. 'Transactions.OrderID, '. 'Transactions.DVDID, '. 'Transactions.DateOut, '. 'Transactions.DateDue, '. 'Transactions.DateIn, '. 'Customers.CustFN, '. 'Customers.CustLN, '. 'DVDs.DVDName '. 'from Transactions, Orders, Customers, DVDs '. 'where Orders.OrderID=Transactions.OrderID '. 'and Customers.CustID=Orders.CustID '. 'and DVDs.DVDID=Transactions.DVDID '. 'order by Transactions.OrderID desc, Customers.CustLN asc, '. 'Customers.CustFN asc, '. 'Transactions.DateDue desc, DVDs.DVDName asc;';//Execute the SQL query$result=mysql_query($selectSql, $link);

Link to comment
Share on other sites

i mean what is your relation between transaction table and other tables. i am no talking about query, i am talking about the db schema. if you are using phpmyadmin you can see "view relation" where you can see how does they linked up with foreign keys. which tables are linked to transactions table? check "view relation" of them.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...