3 Replies - 8783 Views - Last Post: 18 June 2011 - 03:51 AM Rate Topic: -----

#1 heavensgate006   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 12-October 07

deleting records using checkbox

Posted 25 September 2008 - 10:42 PM

hi.. i have a problem regarding deleting records using checkbox PHP.. this is my database:
database name: test
table name: test_mysql
CREATE TABLE `test_mysql` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`lastname` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=7;



INSERT INTO `test_mysql` VALUES (1, 'Billly', 'Blueton', '[email protected]');
INSERT INTO `test_mysql` VALUES (2, 'Jame', 'Campbell', '[email protected]');
INSERT INTO `test_mysql` VALUES (3, 'Mark', 'Jackson', '[email protected]');
INSERT INTO `test_mysql` VALUES (4, 'Linda', 'Travor', '[email protected]');
INSERT INTO `test_mysql` VALUES (5, 'Joey', 'Ford', '[email protected]');
INSERT INTO `test_mysql` VALUES (6, 'Sidney', 'Gibson', '[email protected]');



after creating my database, here is my PHP code:

file name: sample.php
<?php
$host="localhost"; // Host name
$username="root"; // 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);

$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"> </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);
}

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



the problem is i cannot delete the records or multiple records in the database using checkbox.. please help me.. thanks a lot. xoxo

Is This A Good Question/Topic? 1

Replies To: deleting records using checkbox

#2 Hary   User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 427
  • Joined: 23-September 08

Re: deleting records using checkbox

Posted 26 September 2008 - 01:30 AM

You access the checkboxes from the form directly in a variable, ie its name.

That only works if php safe mode is off. You'd better off using the $_POST['delete'] variable.
Was This Post Helpful? 0
  • +
  • -

#3 pemcconnell   User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: deleting records using checkbox

Posted 26 September 2008 - 01:34 AM

Hi heavensgate,

Firstly you need to set the action of the form, then you need to check the $_POST values of your form to see what has been set.

I thought I'd give you a helping hand with this:

<?php
$host="localhost"; // Host name
$username="root"; // 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");

// Check if delete button active, start this
if(isset($_POST['delete'])){
	if(sizeof($_POST['chkbx'])){
		foreach($_POST['chkbx'] AS $val){
			$sql = mysql_query("DELETE FROM $tbl_name WHERE id=".(int)$val)or die(mysql_error());
		}
	}
	
	// if successful redirect to delete_multiple.php
	if($sql){
		header('Location:delete_multiple.php');//THIS WAS SAMPLE.PHP - YOU MIGHT WANT TO CHANGE IT BACK
	}
}

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table width="400" 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">#</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
$x = 0;
while($rows=mysql_fetch_array($result)){
$x++;
?>
	<tr>
		<td align="center" bgcolor="#FFFFFF"><input name="chkbx[]" type="checkbox" id="checkbox_<? echo $x; // you need a unique id per 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>
<?
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>


Was This Post Helpful? 1

#4 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,998
  • Joined: 08-August 08

Re: deleting records using checkbox

Posted 18 June 2011 - 03:51 AM

  • Please don't necro-post!
  • Please post your solution in this forum, not some one else's solution from 2 years ago!

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1