PHP Form Submits But Only Updates Last Row.. Need to Update All Rows

andy

I have a form that is populated by this query. This works fine and I have the values in the appropriate text fields. Here is the form code:

print "<form action='update.php' method='post'>";

print "<table width='655' border='1'><tr><th width='40'>Quantity</th><th width='40'>Code</th><th width='175'>Product Description</th><th width='50'>Unit Price</th><th width='50'>Total Price</th><th width='50'>Qty Checked</th><th width='65'>Quantity Passed</th><th width='65'>Failure Type</th>";

$result = mysqli_query($con,"SELECT * FROM b_tasks_po WHERE TASK_ID=$taskid");

while($row = mysqli_fetch_array($result))
{
echo "<td>" . $row['QTY'] . "</td>";
echo "<td><input type='hidden' name='ID[]' value='" . $row['ID'] . "'>" . $row['CODE'] . "</td>";
echo "<td>" . $row['PRODUCT_DESCRIPTION'] . "</td>";
echo "<td>" . $row['UNIT_PRICE'] . "</td>";
echo "<td>" . $row['TOTAL_PRICE'] . "</td>";
echo "<td><input type='text' size='5' name='QTY_CHECKED[]' style='padding:5px;' value='" . $row['QTY_CHECKED'] . "'></td>";
echo "<td><input type='text' size='5' name='QTY_PASSED[]' style='padding:5px;' value='" . $row['QTY_PASSED'] . "'></td>";
echo "<td><select name='FAILURE_TYPE[]'><option value=''></option><option value='Missing'>Missing</option><option value='Option 2'>Option 2</option></select></form></td>";
echo "</tr>";
}
echo "</table>";

print "<p><input type='submit' name='submit' value='Click to Save'></p><p><strong>Comments</strong></p>";

It then gets passed to my update which looks like the following:

if (isset($_POST['submit'])) {

$sql = "UPDATE b_tasks_po SET QTY_CHECKED='".$_POST['QTY_CHECKED']."', QTY_PASSED='".$_POST['QTY_PASSED']."', FAILURE_TYPE='".$_POST['FAILURE_TYPE']."' WHERE ID='".$_POST['ID']."'";
$result=mysql_query($sql)or 
die ("Error"); }

The first query which displays all the appropriate rows in the database returns 7 rows. There are form elements on each row. I currently want to update the numbers in each row, press submit and then it update them in the database. Currently the issue is that it only updates the last row. I understand after reading that it is recognising all the same field names but I've added [] to the end of each name but still no joy.

Each row in the table has an ID and I have a hidden text field which identifies the ID in each row, so where am I going wrong? Why is it just updating the last row?

Any help would be appreciated.

This is what echo var_dump($_POST['QTY_CHECKED']). returned:

array(7) { [0]=> string(2) "10" [1]=> string(5) "25000" [2]=> string(2) "25" [3]=> string(2) "15" [4]=> string(2) "25" [5]=> string(2) "54" [6]=> string(6) "120000" } 
Brandon Wamboldt

The form data is an array of arrays but you aren't looping through them. Your save code should look like this:

if (isset($_POST['submit'])) {
  foreach ($_POST['ID'] as $index => $id) {
    $sql = "UPDATE b_tasks_po SET QTY_CHECKED='".$_POST['QTY_CHECKED'][$index]."', QTY_PASSED='".$_POST['QTY_PASSED'][$index]."', FAILURE_TYPE='".$_POST['FAILURE_TYPE'][$index]."' WHERE ID='".$_POST['ID'][$index]."'";
    $result=mysql_query($sql)or 
    die ("Error"); }
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update multiple rows using a case statement only updates 1 row

From Dev

PHP delete/update only affects last MySQL row

From Dev

PL/SQL Update Trigger Updates All Rows

From Dev

WTForms only submits first form

From Dev

Capture all dynamic form submits

From Dev

Only checks the last row instead of entire rows

From Dev

ACF - Repeater shows only last entered row, not all rows from post

From Dev

ACF - Repeater shows only last entered row, not all rows from post

From Dev

Laravel foreach in form takes only last row

From Dev

PHP form to update MYSQL Row

From Dev

PHP Form Submits Info Twice

From Dev

Update all rows in a column in php

From Dev

Does Visual Studio clean reinstall need ALL updates or only latest?

From Dev

SQL Update, updates all rows (MS-SQL 2008)

From Dev

Update table inside stored procedure updates all rows

From Dev

SQL Update, updates all rows (MS-SQL 2008)

From Dev

Rails: Selectize multiselect only submits the last selection

From Dev

Ajax request only submits last one

From Dev

Update last row in mysql from PHP

From Dev

How to update one value in the last row in php

From Dev

Eloquent updates all rows

From Dev

PHP return only last row JSON

From Dev

Need to read rows but only want data from last per order

From Dev

HTML/Rails form only submits on reload

From Dev

Button click submits the form only on Firefox

From Dev

Copy rows until the last row with value ONLY to another workbook

From Dev

Sum ONLY at the last row where previous rows have same values

From Dev

Multiple rows of radio buttons, only gets the value of the last row

From Dev

PHP: Clear/reset form which submits to self

Related Related

HotTag

Archive