好吧,我正在创建一个PHP脚本,找到任何行WHERE user_id等于所记录用户的user_id,如果找到与用户具有相同user_id的任何行,则“移动”来自 friend_requests的所有行将她的user_id添加到名为 friend_requestes_notificated
的新表中我的脚本中的一切正常,除了将行复制到新表后删除行的代码。
这是错误:
“您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以便在第1行'DELETE FROM friend_requests WHERE user_id = 1'附近使用正确的语法”
CODE:
<?php include_once("includes/head.php"); ?>
<?php require_once("includes/connect/connect.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php require_once("includes/jquery.php"); ?>
<?php function friend_request_notification(){
global $db;
global $userid;
$userid = $_SESSION['userid'];
$query_id_see = "SELECT user_id FROM friend_requests WHERE user_id=\"{$userid}\" ";
$result_set3 = mysql_query($query_id_see, $db) or die(mysql_error());
$change_table = "INSERT INTO friend_requests_notificated (id, user_id, user_id_requester) SELECT id, user_id, user_id_requester FROM friend_requests WHERE user_id={$userid} DELETE FROM friend_requests WHERE user_id={$userid}";
$change_table2 = mysql_query($change_table) or die(mysql_error());
if ($id_requests = mysql_fetch_array($result_set3)){
}
else
{
}
}
if ($id_requests = mysql_fetch_array($change_table2)){
}
else
{
}
friend_request_notification();
?>
答案 0 :(得分:1)
您需要将插入查询和删除查询分开。你不能一次做到这两件事。 (我在那里看到一个选择查询。这也需要它自己的查询。)
$insert_table = "INSERT INTO friend_requests_notificated (id, user_id, user_id_requester) SELECT id, user_id, user_id_requester FROM friend_requests WHERE user_id={$userid}";
$change_table2 = mysql_query($insert_table) or die(mysql_error());
$delete_table = "DELETE FROM friend_requests WHERE user_id={$userid}";
$change_table3 = mysql_query($delete_table) or die(mysql_error());
此外,please, don't use mysql_*
functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDO或MySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial。