Jump to content

Deleting multiple rows with checkbox help please


ozone1

Recommended Posts

Hi guys i need a little help i want to delete multiple rows from mysql with checkbox.

 

Here is my code everything is listing but delete function is not working please have a look at my code and tell me where i went wrong i am a noob in php. please thanks

 

<?php

require('../config.php'); 
include('header.php');
?>

<table width="800" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="report.php">
<table width="800" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">ID</td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Title</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Comment</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>IP</strong></td>
</tr>

<?php
  $result = mysql_query("SELECT * FROM report ORDER BY id");

while($row = mysql_fetch_array($result))

  {
  $id = $row['id'];
  $email = $row['email'];
  $link = $row['link'];
  $comment = $row['comment'];
  $ip = $row['ip'];
  
  ?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $row['email']; ?></td>
<td bgcolor="#FFFFFF"><? echo $row['link']; ?></td>
<td bgcolor="#FFFFFF"><? echo $row['comment']; ?></td>
<td bgcolor="#FFFFFF"><? echo $row['ip']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>

<?
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM report WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to report.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=report.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>

 

 

in config.php i have this

 


<?php

$user = 'my_user';
$pass = 'my_pass';
$host = 'localhost'; 
$db = 'my_db';

@mysql_connect($host,$user,$pass) or die(mysql_error());
@mysql_select_db($db) or die(mysql_error());

?>

I think you should try some trial error thats how you will learn however just take the following code as one off help ;)

hopefully it will work, hopefully I assume you are trying this on a test server.

 

// Check if delete button active, start this
if($_POST['delete']){
$sql = "DELETE FROM report WHERE id IN (" . implode(',', $_POST['checkbox'] . ")";
$result = mysql_query($sql);
}

try this:

 

<?php
if (isset ($_POST['delete']))
{
if (is_array ($_POST['checkbox']))
{
	$del_ids = implode ("','", mysql_real_escape_string ($_POST['checkbox']));

	$sql = "DELETE FROM `report` WHERE `id` IN ('{$del_ids}')";
	$result = mysql_query ($sql) or trigger_error (mysql_error());
}
}
?>

 

in place of:

 

<?php
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM report WHERE id='$del_id'";
$result = mysql_query($sql);
}
?>

 

do you have register_globals on?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.