简体   繁体   English

从(* php变量*)中的id返回错误的表中删除

[英]Delete from table where id in (*php variable*) returns error

I working in a php application where I must delete the selected items from a list where each item haves their own ID from mysql database, everything goes ok until execute the query in php. 我在php应用程序中工作,在该应用程序中我必须从列表中删除选定的项目,其中每个项目都有自己的mysql数据库ID,一切正常,直到在php中执行查询为止。

This is the error message: 这是错误消息:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5 检查与您的MySQL服务器版本相对应的手册以获取在第5行的''附近使用的正确语法

this is the String that I execute in the query: 这是我在查询中执行的字符串:

$queryDE =  "delete from md5_agenda
             where id_empresa = $empi
             and id_unidade = $unii
             and id_usuario = $usrr
             and id_item_agenda in ($deletar);"

The variable $deletar receives their value from post method and their value is like: 35,36,47,... and can be one ore many different values 变量$deletar从post方法接收其值,其值类似于: 35,36,47,...并且可以是一个或多个不同的值

But my problem is if I change $deletar for the exactly values everything goes fine, but if I use the php variable with THE EXACTLY SAME VALUE it doesn't work and returns the previous error message, I have no more ideas about what to do... I wanna keep in this way where I can choose all IDs that I want delete, without repeat the query. 但是我的问题是,如果我将$deletar更改为确切的值,一切都会正常进行,但是,如果我将php变量与“完全相同”的值一起使用,它将无法正常工作并返回上一个错误消息,那么我就不知道该做什么...我想保持这种方式,在这里我可以选择要删除的所有ID,而无需重复查询。

Thanks. 谢谢。

edit: 编辑: 回声返回$ queryDE和mysql_error()

foreach($deletar as $val)
{
             $queryDE =  "delete from md5_agenda
             where id_empresa = $empi
             and id_unidade = $unii
             and id_usuario = $usrr
             and id_item_agenda = $val;"
}

your code is not working because $deleter is return multiple value.
check code it's working.  

Why don't you use a safe parametrized query? 为什么不使用安全的参数化查询?

$db =new PDO('... your connection string ... ');
$stmt = $db->prepare("delete from md5_agenda
         where id_empresa = :empi
         and id_unidade = :unii
         and id_usuario = :usrr
         and id_item_agenda in (:deletar);");
$stmt->execute(array(
                ':empi' => $empi,
                ':unii' => $unii,
                ':usrr' => $usrr,
                ':deletar' => $deletar
              )
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM