Jump to content

How to Update fields with IN Clause


Monkuar

Recommended Posts

friendsov.jpg

 

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/258380-how-to-update-fields-with-in-clause/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.