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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.