Forum Moderators: coopster
This is a snippet of a form I'm creating
echo "<td><input name=\"IPrice[]\" value=\"".$IPrice."\" onchange=\"this.id='changed';\" type=\"text\" /></td>";
echo "<td><input name=\"IPriceExtended[]\" value=\"".$IPriceExtended."\" onchange=\"this.id='changed';\" type=\"text\" /></td>";
echo "<td><input type=\"checkbox\" name=\"delete[]\" value=\"".$ItemID."\" /></td>";
This is looped to display multiple records from a mysql database.
Then I have the following to process the form
for($i=0;$i<count($_POST["ItemID"]);$i++) {
if (isset($_POST['delete'][$i])) {
$sql = "DELETE FROM ORDER_ITEMS WHERE ItemID='" . $_POST['ItemID'][$i] . "'";
} else {
$sql = "UPDATE ORDER_ITEMS SET ISKU='" . $_POST['ISKU'][$i] . "',IDescription='" . $_POST['IDescription'][$i] . "'";
$sql .= ",IQuantity='" . $_POST['IQuantity'][$i] . "',IPrice='" . $_POST['IPrice'][$i] . "',IPriceExtended='" . $_POST['IPriceExtended'][$i] . "'";
$sql .= "WHERE ItemID='" . $_POST['ItemID'][$i] . "'";
}
}
Everything works well except when the delete box is checked the first record is always the one getting deleted. If I check the second record, the first one gets deleted. For some reason I can't match up the deleted checkbox with the correct record.
Any ideas?
Unless i'm mistaken $_POST['ItemID'] is never sent by the form because there is no input named "ItemID". I think you meant to put:
$sql = "DELETE FROM ORDER_ITEMS WHERE ItemID='" . $_POST['delete'][$i] . "'";
Andrew
$sql = "DELETE FROM ORDER_ITEMS WHERE ItemID='" . $_POST['ItemID'][$i] . "'";
as Little_G mentioned I don't see that having a value. It looks like it may work with
$_POST['delete'][$i]
as the item id seems to be set as the value of the checkbox here
echo "<td><input type=\"checkbox\" name=\"delete[]\" value=\"".$ItemID."\" /></td>";