1

I am working on an invoice in which i want to update table data.

My script only updates the last row of the table. Can this work with this foreach loop?

Following is the script I am trying to do it with:

HTML input fields

<tr>
    <input type="hidden" name="data['+i+'][Id]" value="<?php echo $Id; ?>" >
    <input type="hidden" name="data['+i+'][ItemId]" value="<?php echo $item_Id; ?>" >
    <td><input type="text" value="<?php echo $item; ?>"></td>
    <td><input type="text" name="data['+i+'][QTY]" value="<?php echo $Quantity; ?>"</td>
    <td><input type="number" value="<?php echo $price; ?>"></td>
    <td><input type="number" name="data['+i+'][total]" value="<?php echo $total; ?>"></td>
</tr>

PHP

if (isset($_POST['submit'])) {
    foreach($_POST['data'] as $key => $data) {
        $Id = intval($data['Id']);
        $itemId = intval($data['ItemId']);
        $QTY = intval($data['QTY']);
        $Total = intval($data['total']);

        if($itemId > 0) {
            $query = $db - > prepare("UPDATE table SET QTY = :QTY where id = :ID ");
            $query - > execute(array(':ID' => $ID, ':QTY' => $QTY));
        }
    }
}
7
  • You have several variable name mismatches between your HTML and PHP snippets. Namely, $item_Id vs. $itemId, $Quantity vs $QTY, $total vs. $Total. You'd also need to provide the snippet that shows how you assign $price and $item. Commented May 31, 2018 at 15:01
  • What is the "i" variable inside the index of input's name? Commented May 31, 2018 at 15:03
  • 1
    @IvanTalanov The name of the variables would not have any impact on what is submitted, but the name of the inputs would. Commented May 31, 2018 at 15:04
  • @Tobia - these are multiple rows fetched from database. thats why i assigned "i" . Commented May 31, 2018 at 15:11
  • Can you please tell us what is the resulting html? For example:<input type="hidden" name="data[0][Id]" value="" > Commented May 31, 2018 at 15:13

1 Answer 1

3

This does not seem to be valid syntax:

data['+i+']

PHP will consider this as a string with a fixed value, meaning your $_POST[data] array will only have one key, each new occurrence overwriting the previous one. In the end it will look like this:

$_POST[data] == array( '+i+' => array(
    'Id' => ...,
    'ItemId' => ...,
    'QTY' => ...,
    'total' => ...
));

You probably want to use some PHP in your code in order to properly write the i index:

<tr>
    <input type="hidden" name="data[<?php echo $i; ?>][Id]" value="<?php echo $Id; ?>" >
    <input type="hidden" name="data[<?php echo $i; ?>][ItemId]" value="<?php echo $item_Id; ?>" >
    <td><input type="text" value="<?php echo $item; ?>"></td>
    <td><input type="text" name="data[<?php echo $i; ?>][QTY]" value="<?php echo $Quantity; ?>"</td>
    <td><input type="number" value="<?php echo $price; ?>"></td>
    <td><input type="number" name="data[<?php echo $i; ?>][total]" value="<?php echo $total; ?>"></td>
</tr>

and increment it for each row.

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.