我有一个php UPDATE脚本,它使用键入表单的信息更新数据库中的行。
我有一个名为status
以及名为resolved
如果resolved
等于1
,我需要将status
更改为Done
,否则resolved
需要0
。
我想出了这个,但它似乎没有工作,代码运行得很好,没有错误。但是已解决的字段不会改变。
...
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"UPDATE affiliate_tasks SET status= '$_POST[task_status_edit]',task_name = '$_POST[task_name_edit]',due_date = '$_POST[task_date_edit]',if ($_POST[task_status_edit] == 'Done') {
resolved = '1'
}
else{
resolved = '0'
}
WHERE task_id='$_POST[task_id]'");
echo '<meta http-equiv="Refresh" content="0;url=http://www.green-panda.com/website/panda/affiliates/task.php?msg=' . urlencode(base64_encode('A task was updated!')) . '">';
?>
答案 0 :(得分:4)
您在SQL语句中混合使用PHP,从而创建了一个无效语句(这正是错误消息告诉您的内容:You have an error in your SQL syntax
)。记住:SQL与PHP无关。
您应该在运行查询之前检查$_POST[task_status_edit]
的值,并相应地设置resolved
变量。
然后运行查询:
$resolved = $_POST['task_status_edit'])=='Done'?1:0;
mysqli_query($con,"UPDATE affiliate_tasks
SET resolved='$resolved',
status= '$_POST[task_status_edit]',
task_name = '$_POST[task_name_edit]',
due_date = '$_POST[task_date_edit]'
WHERE task_id='$_POST[task_id]'");
答案 1 :(得分:1)
这个怎么样:
($_POST[task_status_edit] == 'Done')?1:0;
您可以将if替换为if else结构:
mysqli_query($con,"UPDATE affiliate_tasks SET status= '$_POST[task_status_edit]',
task_name = '$_POST[task_name_edit]',
due_date = '$_POST[task_date_edit]',
resolved=".($_POST[task_status_edit] == 'Done')?1:0 ."
WHERE task_id='$_POST[task_id]'");
答案 2 :(得分:1)
像这样...
if($_POST['task_status_edit']=='Done'){
$resolved =1;} else {$resolved=0;}
mysqli_query($con,"UPDATE affiliate_tasks SET resolved='$resolved', status= '$_POST[task_status_edit]',task_name = '$_POST[task_name_edit]',due_date = '$_POST[task_date_edit]' WHERE task_id='$_POST[task_id]'");
或者更容易,一个班轮的方式...
mysqli_query($con,"UPDATE affiliate_tasks SET status= '$_POST[task_status_edit]', task_name = '$_POST[task_name_edit]', due_date = '$_POST[task_date_edit]', resolved=".($_POST[task_status_edit] == 'Done')?1:0 ." WHERE task_id='$_POST[task_id]'");