Forum Moderators: coopster

Message Too Old, No Replies

Help With Form Variables

         

boxfan

12:26 am on Feb 12, 2006 (gmt 0)

10+ Year Member



Hello,

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?

Little_G

12:51 am on Feb 12, 2006 (gmt 0)

10+ Year Member



Hi,

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] . "'";

I asume that $_POST['ItemID'] is being evaluated by the for statment as 0 and therefor it runs the statement whilst $i = 0, and stops after that.

Andrew

boxfan

1:25 am on Feb 12, 2006 (gmt 0)

10+ Year Member



I didn't include all of the form, the ItemID is sent through the form.

The form functions properly except for the problem with the delete checkmarks.

jatar_k

6:09 pm on Feb 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



though it may be the delete query that is grabbing the wrong id

$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>";