Jump to content

Updating multiple records


Canman2005

Recommended Posts

Hi all

 

I have some PHP code which basically outputs the following form

 

<input name="name[]" type="text" value="apple" />
<input name="product[]" type="text" value="mp3" />
<input name="id[]" type="hidden" value="2" />    
<br /><br />
<input name="name[]" type="text" value="microsoft" />
<input name="product[]" type="text" value="software" />
<input name="id[]" type="hidden" value="10" />    
<br /><br />
<input name="name[]" type="text" value="apple" />
<input name="product[]" type="text" value="software" />
<input name="id[]" type="hidden" value="11" />

   

   

when I submit the form, I want to run an update QUERY for everything on the form, using the following QUERY

 

$sql = "UPDATE `items` SET `name` = '".$_POST['name']."', `product` = '".$_POST['product']."' WHERE `id` = '".$_POST['id']."'";
$query = mysql_query($sql);

 

So it's almost like a need to run 3 updates scripts

 

Is this possible?

 

I thought maybe with a

 

foreach()

 

but i've no idea where to start.

 

any ideas anyone?

 

thanks all

 

dave

Link to comment
Share on other sites

Either three queries or one complex query. If you will only ever have three items, then three queries would probably be the most straitforward approach. If you will have many items, then you should probably do some testing to see what is the most efficient.

 

Anyway, this is what the "complex" query would look like:

$query =  "UPDATE `items` SET

    `name` = CASE id
        WHEN {$_POST['id'][0]} THEN {$_POST['name'][0]}
        WHEN {$_POST['id'][1]} THEN {$_POST['name'][1]}
        WHEN {$_POST['id'][2]} THEN {$_POST['name'][2]}
    END

    product = CASE id
        WHEN {$_POST['id'][0]} THEN {$_POST['product'][0]}
        WHEN {$_POST['id'][1]} THEN {$_POST['product'][1]}
        WHEN {$_POST['id'][2]} THEN {$_POST['product'][2]}
    END

WHERE id IN ({$_POST['id'][0]}, {$_POST['id'][0]}, {$_POST['id'][0]})"

 

Of course, if the number of items is variable, you would want to script that:

$itemCount = count($_POST['id']);
$nameCases = '';
$productCases = '';
for($itemIdx=0; $itemIdx<$itemCount; $itemIdx++)
{
    $nameCases     .= "WHEN {$_POST['id'][$itemIdx]} THEN {$_POST['name'][$itemIdx]}\n";
    $productCases .= "WHEN {$_POST['id'][$itemIdx]} THEN {$_POST['product'][$itemIdx]}\n";
}

$query = "UPDATE `items` SET

    `name` = CASE id
      {$nameCases}
    END

    product = CASE id
      {$productCases}
    END

WHERE id IN (" . implode(',', $_POST['id']) . ")";

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.