简体   繁体   中英

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'.

Either way, I'm not getting the relative 'image id' pertaining to the the associated item.

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_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. 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'] . '"';
}

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