简体   繁体   中英

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.

This is the error message:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 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

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.

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
              )
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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