Forum Moderators: coopster

Message Too Old, No Replies

Problem with array[ ] in checkbox

Can't figure this one...

         

wkpride

12:42 am on Apr 6, 2009 (gmt 0)

10+ Year Member



Howdy,

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

rocknbil

3:43 pm on Apr 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



..... 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.

d40sithui

3:47 pm on Apr 6, 2009 (gmt 0)

10+ Year Member



Error says it's SQL related so "deleterecord[]" is probably not causing this.

This part is likely the problem.


$maintresult2 = mysql_query("SELECT * FROM maint ");
mysql_query($maintresult2) or die(mysql_error());

Looks like you mysql_query() a sql resource rather than a query.

wkpride

10:01 pm on Apr 6, 2009 (gmt 0)

10+ Year Member



Excellent. I'll try a re-write & try it again.
Most appreciated!

KP