简体   繁体   English

MySQL查询选择错误的字段

[英]mysql query selecting wrong field

$something_else = mysql_query('SELECT image_id FROM items p LEFT JOIN list up ON p.item_id = up.item_id WHERE up.UserID =  "' . $user_id . '"');

     while ($r=mysql_fetch_assoc($something_else)){
         foreach($r as $item_id2)

$query ='DELETE FROM list WHERE UserID="' .$user_id. '" AND item_id="' .$item_id2. '"';}

This is for a product "wish list." 这是针对产品的“愿望清单”。 Each is relative to a user. 每个都是相对于用户的。 I can add to the wish list, but I can't delete the proper item. 我可以添加到愿望清单,但不能删除适当的项目。 What this code is doing is deleting the last item on the list, or perhaps the item with the highest 'image_id'. 此代码正在执行的操作是删除列表中的最后一项,或者删除具有最高“ image_id”的项目。

Either way, I'm not getting the relative 'image id' pertaining to the the associated item. 无论哪种方式,我都不会获得与关联项目有关的相对“图像ID”。

I'm pretty sure this should be enough information to solve the problem. 我很确定这应该是解决问题的足够信息。 I'm most certain my problem lies in the MySQL query: 我敢肯定,我的问题出在MySQL查询中:

mysql_query('SELECT image_id FROM items p LEFT JOIN list up ON p.item_id = up.item_id WHERE up.UserID = "' . $user_id . '"');

first of all as mentioned before your queries are NOT secure, please use PDO. 首先,如前所述,在您的查询不安全之前,请使用PDO。 anyways try this : 无论如何尝试这个:

$something_else = mysql_query('SELECT image_id FROM items p LEFT JOIN list up ON p.item_id = up.item_id WHERE up.UserID =  "' . $user_id . '"');

while ($r=mysql_fetch_assoc($something_else)) {    
    $query ='DELETE FROM list WHERE UserID="' .$user_id. '" AND item_id="' .$r['image_id'] . '"';
}

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

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