我是 php 新手,我有 2 个具有多对多关系的表和另一个用于关系的表:
每次我想更新外部表时,我都会给出错误:
Cannot add or update a child row: a foreign key constraint fails (`wikiseda`.`genre_singer`, CONSTRAINT `genre_singer_ibfk_1` FOREIGN KEY (`f_singer_id`) REFERENCES `singers` (`singerid`) ON DELETE CASCADE ON UPDATE CASCADE);
这是我的代码:
<?php
include('../db_inc.php');
define("UPLOAD_DIR",realpath(dirname(__FILE__)));
$singer_name =$_POST['singer_name'];
$singer_gender=$_POST['singer_gender'];
$singer_des=$_POST['singer_description'];
$singer_genre=$_POST['genre_list'];
$path = UPLOAD_DIR .'/musics/'.$singer_name;
if(!file_exists($path)){
mkdir($path,0777,true);
}
$sql ="INSERT INTO singers(singer_name,singer_gender,singer_description) VALUES ('$singer_name','$singer_gender','$singer_des')" ;
$singer_id = mysql_insert_id();
$sql2 =("INSERT INTO genre_singer(f_singer_id,f_genre_id) VALUES ('$singer_id','$singer_genre')");
$result=mysql_query($sql)or die(mysql_error());
$result2=mysql_query($sql2)or die(mysql_error());
if('$result'){
echo "insert successfully";
};
?>