Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 17th, 2011, 10:11 AM
Authorized User
 
Join Date: Jul 2011
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
Default deleting using php

hi all,
how toto delete the selected records for the given php program below....
also i am not getting field values in correct order one field is more than its field...
kindly tell me how to do it......
Code:
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="";     // Mysql password
$db_name="test"; // Database name
$tbl_name="emp"; // 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 emp";
$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 align="center" bgcolor="white"><strong>EmpNo</strong></td>
<td align="center" bgcolor="white"><strong>EmpName</strong></td>
<td align="center" bgcolor="white"><strong>Desig</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result, MYSQL_ASSOC))
{

?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['empno']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['empname']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['desig']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if($_POST['delete'])
{
//print_r($_POST);
//exit;
for($i=0;$i<count($_POST['checkbox']);$i++)
{
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple5.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=h_delete.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
 
Old August 29th, 2011, 07:39 PM
Friend of Wrox
 
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

Try this, it is untested mind...
Code:
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="";     // Mysql password
$db_name="test"; // Database name
$tbl_name="emp"; // 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 emp";
$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 align="center" bgcolor="white"><strong>EmpNo</strong></td>
		<td align="center" bgcolor="white"><strong>EmpName</strong></td>
		<td align="center" bgcolor="white"><strong>Desig</strong></td>
	</tr>

<?php
	while($rows=mysql_fetch_array($result, MYSQL_ASSOC))
	{
?>
	<tr>
		<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
		<td bgcolor="#FFFFFF"><?php echo $rows['empno']; ?></td>
		<td bgcolor="#FFFFFF"><?php echo $rows['empname']; ?></td>
		<td bgcolor="#FFFFFF"><?php echo $rows['desig']; ?></td>
	</tr>
<?php
	}
?>

	<tr>
		<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
	</tr>

<?php
// Check if delete button active, start this
if( isset($_POST['delete']) )
{
	//print_r($_POST);
	//exit;

	$del_id_list = ( isset($_POST['checkbox']) ? $_POST['checkbox'] : array());
	if( sizeof($del_id_list) )
	{
		$del_sql = '';
		foreach($del_id_list as $key => $value)
		{
			$del_sql .= $value ', ';
		}
		$del_sql = substr($del_sql, 0, -2);
		
		// Remove the items
		$sql = "DELETE FROM $tbl_name WHERE id IN (" . $del_sql , ")";
		$result = mysql_query($sql);
	}
	else
	{
		echo '<tr>';
		echo '<td colspan="3">Nothing selected to delete</td>';
		echo '</tr>';
	}
	// if successful redirect to delete_multiple5.php
	if($result)
	{
		echo "<meta http-equiv=\"refresh\" content=\"0;URL=h_delete.php\">";
	}
}
mysql_close();
?>
	</table>
	</form>
</td>
</tr>
</table>





Similar Threads
Thread Thread Starter Forum Replies Last Post
inserting and deleting in php ravi951 PHP Databases 0 August 16th, 2011 09:58 AM
deleting images from a directory using php Adam H-W Beginning PHP 1 June 7th, 2006 08:57 AM
deleting a topic BJGroot Access VBA 1 March 18th, 2006 08:39 AM
Deleting Duplicates ioates SQL Server 2000 5 August 31st, 2003 10:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.