使用多个表将INSERT语句转换为UPDATE

时间:2013-09-26 01:23:46

标签: php mysql

如何使用MYSQL将此INSERT语句转换为UPDATE语句?表filmInfo引用actorsInfo的外键id_actoractorsInfo.id

可能是更好的方法,但这就是我现在所拥有的。

$sql="INSERT INTO filmInfo (filmTitle, filmRole, filmDirector, id_actor)
VALUES
('$_POST[filmTitle]','$_POST[filmRole]','$_POST[filmDirector]', 
( select id FROM actorsInfo WHERE email = '$_SESSION[email]'))";

这是我的查询。

$query="SELECT filmTitle, filmRole, filmDirector, idfilm FROM filmInfo, actorsInfo
WHERE (actorsInfo.id = filmInfo.id_actor) AND email = '$_SESSION[email]'";

我试过了,但UPDATE不起作用:

$sql= "Update filmInfo  join actorsInfo 
ON actorsInfo.id = filmInfo.id_actor
Set 
filmTitle = '$filmTitle',
filmRole = '$filmRole',
filmDirecto = '$filmDirector'
WHERE (actorsInfo.id = afillInfo.id_actor) AND email = '$_SESSION[email]'";

请帮忙!

1 个答案:

答案 0 :(得分:1)

这应该这样做:

$sql= "Update filmInfo  join actorsInfo 
ON actorsInfo.id = filmInfo.id_actor
Set 
filmTitle = '$filmTitle',
filmRole = '$filmRole',
filmDirecto = '$filmDirector'
WHERE email = '$_SESSION[email]'";

您在afillInfo.id_actor中引用了WHERE,但查询中没有提到这样的表格。所有加入条件都应在ON条款中。