Jump to content

Recommended Posts

Hi Guys,

 

Sorry this is similar to the last threat I started, however I think I made it a little confusing. I use the code below to update rows in my database depending on which check-box is ticked. Basically id like to carry out the following but with the query outside of the while loop so that only 1 query is ran rather than a query for each check-box.

 

$i = 0;
while($row = mysql_fetch_assoc($query)){
	$check = $_POST['checkbox_value'][$i];
	$value1 = $_POST['value1'][$i];

	mysql_query("UPDATE table1 SET field1='$value1' WHERE field2='$check'");

$i++; 
}

 

Iv tried using mysql_query("UPDATE table1 SET field1='$value1' WHERE field2 IN ('$check')"); outside of the while loop however I don't know how to get it working with the arrays.

 

Any help is greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/241914-update-multiple-rows-with-1-query/
Share on other sites

Hi AyKay47

 

Iv threw together a quick example of the form im using: www.directbullion.co.uk

 

When a user changes the value in the text box, ticks the checkbox and clicks save changes, im trying to update the database using as few a queries as possible.

 

If I use a while loop wouldn't multiple queries be submitted?

 

Thanks

the best way to do this is to find a unique key. For example the id of the record of the database. Pass that unique id through with the form, then you can use an insert statement and use "on duplicate key". Basically you'll never want to insert, but the unique id will always cause the on dupe key action to be called for every insert

 

for example lets say you have a database

product_id | name             | price
----------------------------------------
1              | some name     | 23
2              | another name | 32
3              | abcdefg          | 12

 

the form

<form>
<input name=product[1] />
<input name=product[2] />
<input name=product[3] />
</form>

 

the php

$insert = array();

foreach($_POST['product'] as $id => $val)
{
$insert[] = "($id, $val)";
}

mysql_query(" INSER INTO products(id,price) VALUES " . implode(',', $insert) . " ON DUPLICATE KEY UPDATE price=VALUES(price)");

Hi djlee,

 

I was using the checkbox value as the unique key.

 

Iv tried using your example however for the life of my I cant get it to work. Im only working with 2 products atm, it is inserting 2 new rows each time i submit the form, and it is updating the value in all of the previous rows apart from the original 2 that i am trying to update.

 

Do you have any idea as to what i could be doing wrong?

 

Thanks

Hi everyone,

 

In case anybody else comes across this post looking for the same thing, here is my solution. It only uses 1 query to update the database and after tests updating 2 columns with 100 rows each, it was found to be about 4 times faster than using a query within a while loop.

 

$i = 0;
while($row = mysql_fetch_assoc($query)){
$product_string .= " WHEN domain='" . $_POST['checkbox_value'][$i] . "' THEN '" . $_POST['product'][$i] . "'";
$price_string .= " WHEN domain='" . $_POST['checkbox_value'][$i] . "' THEN '" . $_POST['price'][$i] . "'";
$i++;
}

mysql_query("UPDATE table1 SET column1 = CASE" . $product_string . " ELSE column1 END, column2 = CASE" . $price_string . " ELSE column2 END");

 

I hope it helps.

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.