Monkuar Posted March 6, 2012 Share Posted March 6, 2012 I have this code: if (isset($_POST['update'])){ $ids = implode(",", array_map('intval', $_POST['m'])); $ranks = implode(",", array_map('intval', $_POST['ranks'])); if ($ids < 1) message("Incorrect Data"); if ($ranks < 0) message("Incorrect Data"); echo $ids; echo "<br>"; echo $ranks; exit; $db->query('UPDATE friends set RANKS WHERE friend_id IN ('.$db->escape($ids).') AND user_id = '.$pun_user['id'].'') or error('Unable to remove users from online list', __FILE__, __LINE__, $db->error()); redirect("s.php?section=Friends","Thanks, Ranks Updated"); } I grab the id's from the rank This will spit out 3,4 for $ids and for $ranks, 1,2 as you can see, set for 1 and 2 respectivaly. My problem is how can I use MYSQL to update the data correspond to each id using a IN clause? $db->query('UPDATE friends set RANKS = "LOST $RANKS? HERE" WHERE friend_id IN ('.$db->escape($ids).') AND user_id = '.$pun_user['id'].'') or error('Unable to remove users from online list', __FILE__, __LINE__, $db->error()); Quote Link to comment https://forums.phpfreaks.com/topic/258380-how-to-update-fields-with-in-clause/ Share on other sites More sharing options...
scootstah Posted March 6, 2012 Share Posted March 6, 2012 That's not how IN works. IN will update every row where the row is "in" the IN clause. There's a thread on the front page with the same question. http://www.phpfreaks.com/forums/index.php?topic=355124.0 Quote Link to comment https://forums.phpfreaks.com/topic/258380-how-to-update-fields-with-in-clause/#findComment-1324448 Share on other sites More sharing options...
Monkuar Posted March 6, 2012 Author Share Posted March 6, 2012 So if i want to achieve this, if a user has 20 friends and wants to update each 1 with the respective rank, it will run 20 queries? I will use a foreach to do this correct? If so, i dont care , 20 queries is nothing if it's just simple updates Quote Link to comment https://forums.phpfreaks.com/topic/258380-how-to-update-fields-with-in-clause/#findComment-1324450 Share on other sites More sharing options...
scootstah Posted March 6, 2012 Share Posted March 6, 2012 But what if a several users have hundreds of friends, and they all update at the same time? Now you are running 1000+ queries. I think this post on SO is what you want. Quote Link to comment https://forums.phpfreaks.com/topic/258380-how-to-update-fields-with-in-clause/#findComment-1324452 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.