Ander_3 Posted July 9, 2012 Share Posted July 9, 2012 (edited) 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 July 9, 2012 by Ander_3 Link to comment Share on other sites More sharing options...
justsomeguy Posted July 9, 2012 Share Posted July 9, 2012 It sounds like the value in DVDID doesn't exist in the linked table. Link to comment Share on other sites More sharing options...
Ander_3 Posted July 9, 2012 Author Share Posted July 9, 2012 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 More sharing options...
justsomeguy Posted July 9, 2012 Share Posted July 9, 2012 The error is with the data, not the code. Link to comment Share on other sites More sharing options...
birbal Posted July 9, 2012 Share Posted July 9, 2012 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 More sharing options...
Ander_3 Posted July 9, 2012 Author Share Posted July 9, 2012 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 More sharing options...
birbal Posted July 10, 2012 Share Posted July 10, 2012 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now