Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Update using Foreach

7:38 pm on Nov 27, 2013 (gmt 0)

Hi all

I've done this many times before but I cannot for love nor money figure out how to get this working. I've looked through my old posts but nothing is in there, also search the forums etc.

I've got a pricelist with multiple rows, the user updates the prices and presses updates. Simple as really.


<td>Product 1</td>
<td><input type="text" value="4080" name="3" style="width:40px;" /></td>
<td>Product 2</td>
<td><input type="text" value="970" name="18" style="width:40px;" /></td>

The name value is the ID within the database.

I am using AJAX to post the data across to be processed with the following:

<script type="text/javascript">
// <![CDATA[
$(function() {
var dataString = $(this).serialize();
type: "POST",
url: "includes/process/pricelist.php",
data: dataString,
dataType: "json",
cache: false,
success: function(response) {
//alert (JSON.stringify(response));
if (response.result === "success" ) {
else if (response.result === "error") {
alert("There has been an error, please contact support");
return false;
// ]]>

The alert(datastring) is giving me the correct output (I think). Example 3=4080&18=970.

This is where I am now stuck with what seems to be a brick wall in front of me. Here's the code I've got to process the form:

foreach($_POST as $key => $value) {
$data[$key] = clean($value);
$id = $data['id'];
$price = $data['price'];

foreach($id as $key => $price) {
$sql = "UPDATE table SET product_price='$price' WHERE product_id='$id'";
$result = mysql_query($sql) or die(mysql_error());
if ($result) {
$arr = array("result" => "success");
echo json_encode($arr);
} else {
$arr = array("result" => "error");
echo json_encode($arr);

I am getting an alert of "There has been an error...." but I cannot see why.

Can anyone point me in the right direction please?
1:13 pm on Nov 28, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

from a quick glance, if ($result) { .... is outside your foreach loop.
This means that you check only your last $result in the loop - and it gives you an error.
Perhaps an empty value/id in your $id array is causing the error?
7:47 pm on Nov 28, 2013 (gmt 0)

WebmasterWorld Senior Member Top Contributors Of The Month

If this is really what you're sending: 3=4080&18=970 you're not setting $data['id']; or $data['price']; within your first foreach(), you're setting $data['3']; and $data['18']; so you end up with $id=''; and $price=''; and even if you get the right values, you're only setting the last one, because the foreach() is closed before $id and $price are set; also $id and $price are not arrays, they're variables, so they won't work with a foreach().



foreach($_POST as $key => $value) {

# Let's make sure we don't set $id=pricelist_edit by using is_numeric()
# to check the key.

if(is_numeric($key)) {
$id[] = clean($key);
$price[] = clean($value);

# Guessing clean() is a function you wrote to validate what's sent/set?

# You need a counter to associate the $price array piece and the $id array
# piece correctly, and a for() is generally faster than a foreach(), so I just
# went with a for();


for($i=0; $i<$id_count; $i++) {
$sql = "UPDATE table SET product_price='$price[$i]' WHERE product_id='$id[$i]'";
mysql_query($sql) or die(mysql_error());

# If there's an error the script dies (hopefully that's only for testing since
# showing the error on the screen is not recommended and killing the script
# means we won't make it to the final if), but for now, since we're this far we
# can increment a counter and know how many items were updated if any were.

# mysql_query($sql) or $error++;
# I'll leave the possible uses for the line above to your imagination ;)


# Below will still work the way it was since $result=0 == !$result, but it
# might be better to send the value of $result and let people know if there
# was 1 successful price update or 10

if ($result) {
$arr = array("result" => $result);
echo json_encode($arr);
} else {
$arr = array("result" => $result);
echo json_encode($arr);

# Then edit the JSON if so > 0 == "Success" and 0 == "Error"
1:03 pm on Dec 2, 2013 (gmt 0)

Thank you for taking the time to post a spot on solution to the problem I was having.

I've got the pricelist to update perfectly now.
9:08 pm on Dec 2, 2013 (gmt 0)

WebmasterWorld Senior Member Top Contributors Of The Month

Glad it worked for you!

Featured Threads

Hot Threads This Week

Hot Threads This Month