tshing70 Posted July 11, 2012 Share Posted July 11, 2012 I have a table name collection withc_id int PK AUTO_INCREMENT,memberid int FK,date date,amount Numeric (12,2),refno VARCHAR(20), my FK references to my 1st table PK, name P_Id I want to add date, amount and refno,but when i click add it show me INSERT INTO collection (date, refno, amount) VALUES ('03/07/2012','345','234')Error: Cannot add or update a child row: a foreign key constraint fails (`member_db`.`collection`, CONSTRAINT `collection_ibfk_1` FOREIGN KEY (`memberid`) REFERENCES `newmember` (`P_Id`)) Anyone help pls.... Add.html<html><head><title>Add Info</title><link rel="stylesheet" type="text/css" href="tcal.css" /><script type="text/javascript" src="tcal.js"></script></head><body><form action = "add.php" method="post"><center><table width="250" border="1"><tr valign="top"><td style="background-color:white;width:100px;text-align:top;"><p>Date<p>Reference No<p>Amount</td><td style="background-color:#EEEEEE;height:200px;width:1000px;text-align:top;"><input type ="date" name="date" class="tcal" value=""/><p><input type ="text" name="refno"><p><br><input type ="number" name="amount"><p><input type ="submit" value="Add"></td></tr></form></table></center></body></html> Add.php<?php$con = mysql_connect("localhost","root","");if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("member_db", $con);$sql="INSERT INTO collection (date, refno, amount)VALUES ('$_POST[date]','$_POST[refno]','$_POST[amount]')";echo $sql;if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }echo "1 record added";echo "<br>";echo "<a href='edit.php'>Back to Edit page</a>";mysql_close($con);?> Link to comment Share on other sites More sharing options...
divinedesigns1 Posted July 11, 2012 Share Posted July 11, 2012 what does memberid int FK means? i never saw int FK in a mysql table Link to comment Share on other sites More sharing options...
birbal Posted July 11, 2012 Share Posted July 11, 2012 similar problem like youhttp://w3schools.invisionzone.com/index.php?showtopic=44261 Link to comment Share on other sites More sharing options...
birbal Posted July 11, 2012 Share Posted July 11, 2012 what does means? i never saw int FK in a mysql tableit means Foreign keys. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 11, 2012 Share Posted July 11, 2012 You need to include a value for the memberid in the query, you can't insert the other 3 values without a memberid. 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