Cannot add or update a child row: a foreign key constraint fails
- by myaccount
// Getting the id of the restaurant to which we are uploading the pictures
$restaurant_id = intval($_GET['restaurant-id']);
if(isset($_POST['submit']))
{
$tmp_files = $_FILES['rest_pics']['tmp_name'];
$target_files = $_FILES['rest_pics']['name'];
$tmp_target = array_combine($tmp_files, $target_files);
$upload_dir = $rest_pics_path;
foreach($tmp_target as $tmp_file => $target_file)
{
if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file))
{
$sql = sprintf("
INSERT INTO rest_pics
(branch_id, pic_name)
VALUES ('%s', '%s')"
, mysql_real_escape_string($restaurant_id)
, mysql_real_escape_string(basename($target_file)));
$result = mysql_query($sql) or die(mysql_error());
}
I get the next error:
Cannot add or update a child row: a
foreign key constraint fails
(rest_v2.rest_pics, CONSTRAINT
rest_pics_ibfk_1 FOREIGN KEY
(branch_id) REFERENCES
rest_branches (branch_id) ON
DELETE CASCADE ON UPDATE CASCADE
However, this error totally disappears and everything goes well when I put directly the restaurant id (14 for example) instead of $restaurant_id variable in the sql query.
The URL am getting the id from is:
http://localhost/rest_v2/public_html/admin/add-delete-pics.php?restaurant-id=2
Any help please?