...

View Full Version : Problem in deleting multiple rows



LakkadBabu
05-08-2004, 02:41 PM
Hello everybody,
I am having problem in deleting multiple rows using checkbox. It is showing the following error :

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in \delete.php


Code:

print"<form name=\"form1\" method=\"post\" action=\"delete.php\">";
print"<tr>";
print"<td>Designation</td>";
print"<td>First Name</td>";
print"<td>Last Name</td>";
print"<td>&nbsp;</td>";
print"</tr>";
while ($a_row = mysql_fetch_array ($result))
{
print"<tr>\n";

print "<td><center>$a_row[designation]</center></td>
<td><center>$a_row[fname]</center></td>
<td><center>$a_row[lname]</center></td>

print "<td><input type=\"checkbox\" name=\"delete[]\" value=$a_row[id]></td>";
print "</tr>";
}
print "</table>";
print"<center>";
print"<p>";
print"<input type=\"submit\" name=\"delete\" value=\"Delete\">";
print"<input type=\"reset\" name=\"submit2\" value=\"Cancel\">";
print"</p>";
print"</center>";
print"</form></td>";

DELETE.PHP

<?php
//print "$a_row[id]";
if ( ! delete){
echo "<b>Please choose fields to be deleted.</b><br>";
}
else{
$sql = "delete * from work_committee where id in ($_POST['delete'])";
$result = mysql_query($sql);
if (! $result )
echo"ERROR !!! " . mysql_error();
else
echo "DELETED!!!";
} //end if
?>


Where id is int type field.

Any help would appreciated. Thanks in advance.

gismo
05-08-2004, 05:27 PM
i would try this, it worked for me

if (is_array($_POST['delete']))
{
$deleted = 0;

while(list($key, $id) =each($_POST['delete']))
{
if (mysql_query("DELETE FROM `table_name` WHERE `id`='$id'"))
{
$deleted .= mysql_affected_rows();
}
else
{
die("SQL error:<br />" . mysql_error());
}
}
echo $deleted . " rows deleted<br><br>";


}else{
echo "No entry selected";
}

I would also change the name of your delete button to something other than "delete" as that might be causing it problems.
print"<input type=\"submit\" name=\"delete\" value=\"Delete\">";

sidney
05-08-2004, 05:39 PM
you forgot to close the else


else{
$sql = "delete * from work_committee where id in ($_POST['delete'])";
$result = mysql_query($sql);
if (! $result )
echo"ERROR !!! " . mysql_error();
else
echo "DELETED!!!";
} //end if
}//end else
?>

Íkii
05-09-2004, 12:12 PM
I would advise getting a syntax highlighting IDE (perhaps phpedit.com for a freebie) so you can easily tell where code errors like that lay.

Your second print block has no terminating " btw, which explains the T_STRING return.

firepages
05-09-2004, 03:45 PM
note the braces around {$_POST['delete']} , they tell PHP that the enclosed value is an array and will stop your parse error.



<?
$sql = "delete * from work_committee where id in ({$_POST['delete']})";
?>

LakkadBabu
05-10-2004, 08:51 AM
Thanks everybody :thumbsup: .

Gismo Your code is running as it is except - when I delete two rows it display - 1 Row Deleted.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum