Forum Moderators: coopster
This is the small script using checkbox to remove a record, but something isn't right...
<?php
$maintresult2 = mysql_query("SELECT * FROM maint ");
$maintrecordfound2 = mysql_num_rows($maintresult2);
if (isset($_POST['submit']))
{ foreach ($_POST['deleterecord'] as $delete_id)
{ $query = "DELETE FROM maint WHERE sysid = $delete_id";
mysql_query($query) or die(mysql_error());}
}
$maintresult2 = mysql_query("SELECT * FROM maint ");
mysql_query($maintresult2) or die(mysql_error());
while ($row = mysql_fetch_array($maintresult2))
{ echo '<input type="checkbox" value= "' . $row['sysid'] . '" name= "deleterecord[]" />';
echo $row['polenum'];
echo ' ' . $row['location'];
echo '<br />';
}
?>
<form action= "<?php echo $_SERVER['PHP_SELF']; ?> " method="POST";>
<input type="submit" name="submit" value="Remove" /> </form>
This is error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #4' at line 1
I THINK it's.... name= "deleterecord[]" causing array error...
Any thoughts?
Thanks,
KP
..... name= "deleterecord[]".....the right syntax to use near 'Resource id #4'
See what's happening? deleterecord[] contains "Resource id #4". You probably want to populate that with something specific to the row, however, I would not suggest making the form "name" a numeric value only, preamble it with text, something like
...........
while ($row = mysql_fetch_array($maintresult2))
$delete_name = 'delete_' . $row['sysid]'; // Or record id . . .
{ echo '<input type="checkbox" value= "' . $row['sysid'] . '" name= "' . $delete_name . '">';
........
then,
........
foreach ($_POST as $key=>$value) {
if (preg_match("/delete\_\d+/",$key)) {
$query = "DELETE FROM maint WHERE sysid = $value";
.......
Note that a checkbox value will only exist in $_POST/$_GET/$_REQUEST if it is checked, so no worries about deleting records unintentionally.