I’m trying to delete rows of MySQL date using Checkboxes. When I apply the script below it doesn’t do what I want it to do. Action is not performed.. And I get
Undefined variable: checkbox in C:\wamp\www\project\topics.php on line 108
I tried echoing out the actual sql that is being sent for execution and it gave this
DELETE FROM forum_topics WHERE topic_id = intval()
PHP
<?php
$topics = mysql_query(" SELECT topic_id , topic_head ,
topic_tags , topic_owner , topic_date
FROM forum_topics ") or die (mysql_error());?>
<form method='post' action='topics.php'>
<div class='admin'><table>
<tr>
<td >DELETE</td>
<td >ID</td>
<td>Title</td>
<td>Tags</td>
<td>Owner</td>
<td>Date</td>
</tr>
<?php
while($row=mysql_fetch_array($topics)){ ?>
<tr align=center>
<td align="center" bgcolor="black">
<input name="checkbox[]" type="checkbox" value="<?php echo $row['topic_id'];?>">
</td>
<td><?php echo intval($row['ID']); ?></td>
<td><?php echo htmlspecialchars($row['topic_head']); ?></td>
<td><?php echo htmlspecialchars($row['topic_tags']); ?></td>
<td>><?php echo htmlspecialchars($row['topic_owner']); ?></td>
<td>
<?php
$date = date_create($row['topic_date']) ;
echo date_format($date, 'F j, Y, g:i A');
?>
</td>
<?php echo"
</tr>";
}?>
<td ><input name="delete" type="submit" value="DELETE"></td>
<?php
// Check if delete button active, start this
if (isset($_POST['delete'])) {
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM forum_topics WHERE topic_id = intval($del_id)";
mysql_query($sql);
}
}
?>
</table>
</form>
</div>
Well you need to rewrite your deletion logic into something like this
Also consider to do not use
mysql_*functions since deprecation process has begun.EDIT Added missed parenthesis in
ifconditionEDIT
Also I recommend to redirect user to the same page after form submission it’ll prevent from repeated form submits on page refresh. See updated code. And of course deletion logic should be placed before you made any selects