0

This seems like a very basic task; I am not sure if I am having a brain cramp or if I ran into something a bit more rare. But it doesn't seem so.

I have a table called products that have these columns: ID, Model, Cost, Quantity

My HTML form prints out all price and quantity discounts by model; I loop thru like this:

 <form method="post">

 <table>
       <tr>
            <th>Quantity</th>
            <th>Pricing</th>
       </tr>

      <?php while ($row = mysqli_fetch_array($result)) { ?>

       <tr>
           <td><?php echo $row['model']; ?></td>
           <td><input type="text" name="<?php echo $row['id']; ?>" value="<?php echo $row['cost']; ?>" /></td>

      </tr>

     <?php } ?>

 </table>


  <input type="submit" name="retail_price" value="Update Pricing"  />

 </form>

So it prints out all pricing in text fields, and has a button so that there can be "mass" update to pricing.

My question is regarding the PHP where I am stuck:

if (isset($_POST['retail_price'])) {  // THIS CODE IS NOT COMPLETE!

    $query = "
        UPDATE retail_pricing
        SET pricing = {$new price}
                    WHERE id = {$id} ";
    mysqli_query($connection, $query);

}

The issue I have run into, I want to go through each field and update each one. Doing a quick search for other Stack questions, it seems like I need to use a foreach to iterate through each field. Is this correct? Also, would the query be in the loop as well?

2
  • Why do you want to iterate over all the fields? You can make one update with all the fields if they are in the same table. Commented Sep 25, 2013 at 16:01
  • I think the biggest thing I am struggling with is the identifying mark. What to put in the name="" field. It should be unique, but the ID doesn't seem like that is appropriate. So on the POST, I'd have to manually spell out each field and since I am looping through the fields to create them in HTML, seemed like I would need to do the same on the php end. Commented Sep 25, 2013 at 16:06

2 Answers 2

2
foreach($_POST as $key => $value) {
    if(is_numeric($key))
    {
        $query = "UPDATE retail_pricing SET pricing = $value  WHERE id = $key ";
        mysqli_query($connection, $query);
    }  
}
Sign up to request clarification or add additional context in comments.

3 Comments

It will most likely work, but this will also accept any numeric field name even if it is not a valid ID in your DB, which may cause failures.
@Sébastien Ok thanks, good to know. This is avery, very simply project so I'll stick with it since it's not broke, but going forward I would be inclined to use yours; so I upvoted as well.
As long as you are aware of it :)
2

Add a hidden input in your HTML that will collect all IDs:

<tr>
    <td><?php echo $row['model']; ?></td>
    <td>
    <input type="text" name="<?php echo $row['id']; ?>" value="<?php echo $row['cost']; ?>" />
    <input type="hidden" name="ids[]" value="<?php echo $row['id']; ?>" />
    </td>
</tr>

then when you submit the form you can loop through all those IDs and update you DB:

foreach( $_POST['ids'] as $id )
{
    $price = $_POST[$id];
     $query = "
        UPDATE retail_pricing
        SET pricing = {$price}
                    WHERE id = {$id} ";
    mysqli_query($connection, $query);
}

1 Comment

I prefer this method as it will prevent any issues with the query if you add further form fields in the future.

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.