oledaveoh Posted March 1, 2011 Share Posted March 1, 2011 Hello, I have created two tables, one called 'posts' and the other 'selected_pic'. The idea is to have a table of posts that gets stored with an ID that references the image in the 'selected_pic' table. From what i have gathered, using Foreign keys is probably the best way y do this? If theres something easier, then please let me know. However when i try to make a post on the web page "Cannot add or update a child row: a foreign key constraint fails " comes up and thus fails to insert into the table. This is what i have set up so far.. TABLE : selected-pic 'pic_id' - int(11) - NOT_NULL - auto_increment 'username' - varchar(32) - NOT_NULL 'imagelocationpic' - varchar(100) - NOT_NULL Primary Key - 'pic_id' Table : posts 'posts_id' - int(11) - NOT_NULL - auto_increment 'pic_id' - int(11) - NOT_NULL 'username' - varchar(11) - NOT_NULL 'text' - varchar(100) - NOT_NULL 'date' - date - NOT_NULL 'time' - int(11) - NOT_NULL 'up' - int(11) - NOT_NULL 'down' - int(11) - NOT_NULL 'profile_pic' - varchar(100) - NOT_NULL Primary Key - 'posts_id' Index key - 'pic_id' *Here i have set the 'pic_id' from the 'posts' table to reference the 'pic_id' in the 'selected_pic' table. PHP & Mysql code on page. <?php $id=$_SESSION['username']; $time=time(); $date=date("Y-m-d"); if($_POST['post']) { //get the data $text=$_POST['text']; $image=$_POST['profile_pic']; //check for existance if ($text) { //insert data include ("connect2.php"); $time=time(); $insert=mysql_query("INSERT INTO posts VALUES('', '', '$id','$text','$date', '$time','','','$image')") or die(mysql_error()); echo ""; } else echo "Please fill out the text field"; } echo" <br>"; ?> Thanks in advance! Dave Quote Link to comment https://forums.phpfreaks.com/topic/229289-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails/ Share on other sites More sharing options...
fenway Posted March 2, 2011 Share Posted March 2, 2011 Oh, so many things. First, where's the column list? Second, what makes you think that pic_id exists? Quote Link to comment https://forums.phpfreaks.com/topic/229289-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails/#findComment-1182002 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.