2 Replies - 5577 Views - Last Post: 05 September 2011 - 01:16 AM Rate Topic: -----

#1 davisliwo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-September 11

deleting multiple checkbox values selected from mysql database

Posted 04 September 2011 - 10:17 AM

I am trying to DELETE multiple checkbox values in my database but these values are being retrieved from the database and checkboxes are added here is the code

<?php
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="test_mysql"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
<echo
$count=mysql_num_rows($result);

?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></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 $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}




but when i view this is browser it is fine but when i check the values to delete them nothing is happening please help me.

Is This A Good Question/Topic? 0
  • +

Replies To: deleting multiple checkbox values selected from mysql database

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: deleting multiple checkbox values selected from mysql database

Posted 04 September 2011 - 11:36 AM

Firstly PHP mixed with your HTML like that is very confusing. They're suppose to be two separate files.

Example
<!-- HTML -->
<form action="delete.php" method="post">
    <input type="checkbox" name="check[]">
    <!-- other elements -->
    <input type="submit" name="delete_btn">
</form>


<?php
//verify that values were selected in our checkbox
//no need to check the submit button since they can submit without checking values
if(isset($_POST['check']) && count($_POST['check']) > 0) {
	//code to delete
}
//redirect after processing
header('location: another_page.php');
?>



Your website will be a lot more organized if you can follow the principle of separating logic from presentation.

You don't need to run more than one query to delete your values. Use the loop to build your query instead. Remember there's the OR operator for comparison.

I'm not sure what's going on for Line #14 either.

Consider moving to MySQLi also. One more tip is you should centralize your database credentials as in store then in one PHP file and then use require_once to include then in the script. If you hardcode it into every script you're going to have a hard time changing it in each script.
Was This Post Helpful? 0
  • +
  • -

#3 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: deleting multiple checkbox values selected from mysql database

Posted 05 September 2011 - 01:16 AM

Are you relying on register_globals based on this code?

if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}


Where is $delete being defined?

You should be doing something like (above the code):
$delete = isset($_POST['delete']);


Or, even better (to reduce the code) just replace the if statement with the isset clause.

Moving on from that, it baffles me why you're mixing your calls to mysql_* methods, some have or die on the end and some don't, why? Pretty much every call can fail, so whilst debugging you should really have that clause on all of your queries so that you can easily debug.

The same goes with using $checkbox within your loop, you should be using the $_POST variable. Code like this is more sane:
if (isset($_POST['delete']) && !empty($_POST['checkbox'])) { // Do an !empty(..) check here to ensure that there is something to delete also
    foreach ($_POST['checkbox'] as $delete_id) {
        $sql = 'DELETE FROM ' . $tbl_name . ' WHERE id = ' . mysql_real_escape_string($delete_id);
        mysql_query($sql) or die('Deletion Error: ' . mysql_error());
    }
}


Another thing, in your query you was defining $result but not using it?!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1