0

I have this array:

Array (     [3] => 40000     [5] => 20300     [17] => 29000     
[4] => 35000     [6] => 59000     [54] =>      [34] =>      [7] => 113000     
[55] =>      [35] =>      [8] => 219000     [56] =>      [36] =>      [9] => 40600      )

array keys = id

array values = price

I want update table with this values and when the price is exist, save the price and when the price is not exist, save the "NULL"

Here my code:

<form method="post">
    <?php
    $qr2=$mysqli->query("SELECT `price`,`id` FROM `pricing`");
    while($row2=$qr2->fetch_object()){
    ?>
    <input type="text" class="nw-prc" value="<?php echo $row2->price; ?>" name="price[<?php echo $row2->id; ?>]">
    <?php
    }
    ?>
</form>
<input type="submit" id="up" name="up" value="save">

<?php
if (isset($_POST['up'])) {
    $price=$_POST['price'];
}

$keys=array_keys($price);
$values=array_values($price);

foreach($keys as $k){
    foreach($values as $v){
        $mysqli->query("UPDATE `pricing` SET `price`='$v' WHERE `id`='$k'");
    }

}
?>
2
  • Can you print_r($_POST) to see what is the result we get? Commented Dec 15, 2015 at 12:26
  • What error you are getting ? Commented Dec 15, 2015 at 12:32

1 Answer 1

2

Just use

foreach($your_array as $key => $value){
    $mysqli->query("UPDATE `pricing` SET `price`='$value' WHERE `id`='$key'");
}

instead of

 foreach($keys as $k){
    foreach($values as $v){
        $mysqli->query("UPDATE `pricing` SET `price`='$v' WHERE `id`='$k'");
     }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.