tomantford
tomantford

Reputation: 182

foreach $_POST update depending on $key

I'm trying to update my database with a foreach loop. Each time the loop is run, two rows will need to be changed, however the database updates both rows with the same data. My php code for the form is:

<input name="poles<?php echo $curtains['room_curtains_id']; ?>" type="radio" class="calc" id="<?php echo $poles['curtains_poles_id']; ?>" value="<?php echo $poles['curtains_poles_price']; ?>,<?php echo $poles['curtains_poles_id']; ?>">
<input name="pleats<?php echo $curtains['room_curtains_id']; ?>" type="radio" class="calc" id="<?php echo $pleats['curtains_pleats_id']; ?>" value="<?php echo $pleats['curtains_pleats_price']; ?>,<?php echo $pleats['curtains_pleats_id']; ?>">

When posting, my return is:

Array
(
    [poles1] => 0.00,1
    [pleats1] => 40.00,2
    [poles2] => 120.00,2
    [pleats2] => 0.00,1
    [poles3] => 150.00,3
    [pleats3] => 40.00,2
)

Now when it comes to updating the database I am using the following code:

foreach ($_POST as $key => $value) {
$new_value = explode(",", $value);

if (strpos($key, 'poles')!==false) {

$result = mysql_query("UPDATE basket SET product_id = '".mysql_real_escape_string($new_value[1])."', line_price= '".mysql_real_escape_string($new_value[0])."' WHERE session_id = '$session_id' AND plot_id = '".mysql_real_escape_string($plot_id)."' AND product_type='Poles' AND rooms_curtains_id='".mysql_real_escape_string($key)."'");

}

if (strpos($key, 'pleats')!==false) {

$result2 = mysql_query("UPDATE basket SET product_id = '".mysql_real_escape_string($new_value[1])."', line_price= '".mysql_real_escape_string($new_value[0])."' WHERE session_id = '$session_id' AND plot_id = '".mysql_real_escape_string($plot_id)."' AND product_type='Pleats' AND rooms_curtains_id='".mysql_real_escape_string($key)."'");
}

}

When I run this, the database is updated correctly for product_type='Poles' ($result) but with the same information for product_type='Pleats' ($result2), where it should be updating using the key pleats1,pleats2,pleats3.

Upvotes: 1

Views: 1107

Answers (1)

Ivan Yonkov
Ivan Yonkov

Reputation: 7034

You handle the conditions for the MySQL resultset, but you never handled the conditions in your PHP code. You expect magically the MySQL to understand when you specify column with value Pleats, to search in your foreach() loop for %pleats%, but it will never happen, because that are two different things.

The mysql query will be executed as many times, as the iterations in the loop are. In your case - both queries will be executed 6 times with each $key.

If you want to update the rows which has product_type='Pleats' with the $key which contains pleats, you should tell the PHP so. It's no more on MySQL level.

foreach ($_POST as $key => $value) {
    $new_value = explode(",", $value);

    $result = mysql_query("UPDATE basket SET product_id = '".mysql_real_escape_string($new_value[1])."', line_price= '".mysql_real_escape_string($new_value[0])."' WHERE session_id = '$session_id' AND plot_id = '".mysql_real_escape_string($plot_id)."' AND product_type='Poles' AND rooms_curtains_id='".mysql_real_escape_string($key)."'");

    if (strpos($key, 'pleats')!==false) {
        $result2 = mysql_query("UPDATE basket SET product_id = '".mysql_real_escape_string($new_value[1])."', line_price= '".mysql_real_escape_string($new_value[0])."' WHERE session_id = '$session_id' AND plot_id = '".mysql_real_escape_string($plot_id)."' AND product_type='Pleats' AND rooms_curtains_id='".mysql_real_escape_string($key)."'");
    }
}

Upvotes: 1

Related Questions