Jump to content

Cannot add or update a child row: a foreign key constraint fails.


shenagsandnags

Recommended Posts

here is my form layout,

 

Title: txtfield

URL: txtfield

Category: dropdown (pulls the just category fields in my "categories" table

submit - when working right this form is suppose to enter this info into my "movies" table.

 

Code to form.php

<html>
      <form id="form1" name="Update" method="post" action="add3.php">
   
      <label>
   
      Title: <input type="text" name="Title" id="Title" />
   
      </label>
   
      <br />
   
      <label>
   
      URL: <input type="text" name="URL" id="URL" />
   
      </label>

<select name='dropdown' id='dropdown'>
<?php
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not connect to DB: ' . mysql_error() );
}

mysql_select_db ("mydb", $con);
$query = "SELECT Category from categories";
$result = mysql_query($query) OR DIE ("There was an error" .mysql_error());
while($row=mysql_fetch_array($result)) 
    { 
            $category = $row['Category']; 

echo " <option value=\"$category\">$category</option>"; 
    } 

php?>

</select>

   
      <input name="" type="submit" value="send" />

      </form>
</html>

 

 

then here is the code to the process page (add.php)

 

<?php
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not connect to DB: ' . mysql_error() );
}

mysql_select_db ("m5", $con);

$sql="INSERT INTO movies (Category, URL, Title)
VALUES ('$_POST[dropdown]','$_POST[url]','$_POST[Title]')";

if (!mysql_query($sql,$con))
{
die ('Error: ' . mysql_error());
}
echo "Record added";

 

 

after i submit my form i get

 

"Error: Cannot add or update a child row: a foreign key constraint fails (`mydb`.`movies`, CONSTRAINT `movies_ibfk_1` FOREIGN KEY (`Category`) REFERENCES `categories` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE)"

 

correct me if im wrong but im almost %99 sure that my tables are set up correctly for my project.

 

just to be sure,

 

movies

------------

ID (PK)

Title

Category (FK)

URL

 

categories

---------------

ID (PK)

Category

 

at first i was also getting a "undefined index" along with this error but i reversed the values in the

INSERT so now im just getting the CONSTRAINT error so this now leaves me to beleive that i may have made a error one of the tags possibly. also while i was looking around i decided to create a different procces page hoping i would run into some quick luck:

 

add3.php

<?php

$Category=$_POST['dropdown'];
$URL=$_POST['URL'];
$Title=$_POST['Title'];

$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not connect to DB: ' . mysql_error() );
}

mysql_select_db ("mymb", $con);

$query = "INSERT INTO movies VALUES ('$Category','$URL','Title')";
$result= mysql_query($query);

if ($result) {
        echo "Successful";
}
else {
        echo "Failed";
}

mysql_close();


?>

 

and this one gives me "Failed" error.

 

 

i have noticed that there are many many ways of doing this so i went ahead and posted both process page hoping this could give some better ideas rather then confusion (i hope anyways).

im not really sure which ones its more along the lines of php/form "standard" if there is any but whichever may be the easiest for you to take a look at it doesnt matter and i would be greatly thankful. IMO i think its just a varriation of not having the formats right and just not taylored correctly to my needs. many thanks in advanced.

Link to comment
Share on other sites

That error is related to your database structure, it has nothing to do with your HTML or PHP.  Basically, the database is saying "you have a table set up as a foreign key, which means every value in tableA corresponds to a value in tableB.  You are trying to update tableA to have a value that does not appear in tableB."

 

-Dan

Link to comment
Share on other sites

To follow up on ManiacDan's response:

 

In your movies table you have a field called "Category" - is that supposed to be the category name? That wouldn't make sense. So, I assume the value of that field should be the category ID. But, your code for the select list is using the category name as the value.

 

So, if my assumption is correct that the "category" field in the movies table should be the category ID, my first recommendation would be to rename that field to prevent this type of problem in the future. But, to fix the problem as you have it now, you would just need to change the code that generates the select list so the user "sees" the category name, but the value is the category ID.

$query = "SELECT ID, Category FROM categories";
$result = mysql_query($query) OR DIE ("There was an error" .mysql_error());
while($row=mysql_fetch_array($result)) 
{ 
    echo " <option value=\"{$row['ID']}\">{$row['Category']}</option>"; 
} 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.