Reputation: 459
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" }
Upvotes: 2
Views: 1968
Reputation: 8819
Andy you have to run your query in loop, try something like below code.
$count = count($_POST['ID']);
if($count>0){
for($i=0;$i<$count;$i++){
$sql = "UPDATE b_tasks_po SET QTY_CHECKED='".$_POST['QTY_CHECKED'][$i]."', QTY_PASSED='".$_POST['QTY_PASSED'][$i]."', FAILURE_TYPE='".$_POST['FAILURE_TYPE'][$i]."' WHERE ID='".$_POST['ID'][$i]."'";
$result=mysql_query($sql)or die ("Error");
}
}
Upvotes: 0
Reputation: 16656
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"); }
}
}
Upvotes: 2