2015-10-26 136 views
0

我想要使用php来选择和删除表中的多行。每当我选择检查多行来删除时,我会得到这样的错误:“你的SQL语法有错误;查看与你的MySQL服务器版本相对应的手册,查看在第1行'id 5'附近使用的正确语法” 。当我单独检查我的复选框时,我可以删除单行。这里是一个片段,以我的代码:使用复选框删除多行

<?php 

mysql_connect("localhost", "root", "") or die(mysql_error()); 

mysql_select_db("mydatabase") or die(mysql_error()); 

if (isset($_GET['delete'])){ 

$multiple = $_GET['multiple']; 
$i = 0; 

$sql = "DELETE FROM product "; 

foreach ($multiple as $item_id) { $i ++; 
    if ($i == 1){ 
     $sql .= " WHERE id = " . mysql_real_escape_string($item_id) . ""; 
    } else{ 
     $sql .= "OR id ". mysql_real_escape_string($item_id) . ""; 
    } 
} 
    mysql_query($sql) or die(mysql_error()); 
header("location: " . $_SERVER['PHP_SELF']); 
exit(); 
} 

?> 
+2

这是一个语法错误'或ID “.'缺少'=' –

+0

'$ SQL =” OR ID“。mysql_real_escape_string($ item_id)。”“;'失败的原因有两个,缺少等号和OR',我不知道你为什么使用它和'if/else'。读取为'$ sql =“DELETE FROM product OR id”。mysql_real_escape_string($ item_id)。“”;'在'else'中,因此你的if($ i == 1){'失败了。 –

回答

0

快,但没有安全的解决方案:

$sql = 'DELETE FROM product WHERE id IN ('.implode(',',$multiple).')'; 

mysql_query($sql) or die(mysql_error()); 
+0

Thanks for快速的解决方案,这对我的本地机器来说是完美的,但是当我脓的时候h到服务器,它告诉我“没有这样的文件或目录”。你会碰巧遇到过这个问题吗? – Ash

+0

我不知道你在说什么。只是我的猜测:你可以尝试删除这部分'header(“location:”。$ _SERVER ['PHP_SELF']); exit();'从脚本中 – Alex