| | |
Delete multiple rows in mysql with check box
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: May 2007
Posts: 20
Reputation:
Solved Threads: 0
hi all. I have got below code from a website. Its work all but its can't delete the selected multiple row. plz help me... where is the problem.
PHP Syntax (Toggle Plain Text)
<?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="forum"; // 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 test_mysql"; $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, 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['id']; ?></td> <td bgcolor="#FFFFFF"><?php echo $rows['name']; ?></td> <td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?></td> <td bgcolor="#FFFFFF"><?php 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> <?php // Check if delete button active, start this if($delete){ for($i=0;$i<5;$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=dele.php\">"; } } mysql_close(); ?> </table> </form> </td> </tr> </table>
php Syntax (Toggle Plain Text)
<?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="forum"; // 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 test_mysql"; $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, 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['id']; ?></td> <td bgcolor="#FFFFFF"><?php echo $rows['name']; ?></td> <td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?></td> <td bgcolor="#FFFFFF"><?php 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> <?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_multiple.php if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;URL=h_delete.php\">"; } } mysql_close(); ?> </table> </form> </td> </tr> </table>
This should work.
Cheers,
Nav
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
It works for me though! But anyway, try the following.
php Syntax (Toggle Plain Text)
$checkbox=$_POST['checkbox']; for($i=0;$i<count($checkbox);$i++){ $del_id = $checkbox[$i]; .......
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: May 2007
Posts: 20
Reputation:
Solved Threads: 0
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="forum";
$tbl_name="test_mysql";
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
$sql="SELECT * FROM test_mysql";
$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, 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['id']; ?></td><td bgcolor="#FFFFFF">
<?php echo $rows['name']; ?></td><td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><?php 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>
<?php
if($_POST['delete']){
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($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=h_delete.php\">";}}
mysql_close();?></table></form></td></tr></table>
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="forum";
$tbl_name="test_mysql";
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
$sql="SELECT * FROM test_mysql";
$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, 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['id']; ?></td><td bgcolor="#FFFFFF">
<?php echo $rows['name']; ?></td><td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><?php 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>
<?php
if($_POST['delete']){
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($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=h_delete.php\">";}}
mysql_close();?></table></form></td></tr></table>
php Syntax (Toggle Plain Text)
<?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="test"; $tbl_name="users"; 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, MYSQL_ASSOC)){ ?> <tr><td align="center" bgcolor="#FFFFFF"> <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['user_id']; ?>"> </td><td bgcolor="#FFFFFF"><?php echo $rows['user_id']; ?></td><td bgcolor="#FFFFFF"> <?php echo $rows['login_name']; ?></td><td bgcolor="#FFFFFF"><?php echo $rows['first_name']; ?></td> <td bgcolor="#FFFFFF"><?php 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> <?php if($_POST['delete']){ for($i=0;$i<count($_POST['checkbox']);$i++){ $del_id = $checkbox[$i]; $sql = "DELETE FROM $tbl_name WHERE id='$del_id'"; print $sql; $result = mysql_query($sql);} if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=h_delete.php\">";}} mysql_close(); } ?> </table></form></td></tr></table>
This works for me. Umm.. I have also printed the query and it prints ,
DELETE FROM users WHERE id='1'
DELETE FROM users WHERE id='2'
DELETE FROM users WHERE id='3'I have no clue why your code is 'losing' checkbox array values.
Try taking out this part
if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=h_delete.php\">";} and then check. Maybe the page is getting refreshed, so its losing the posted values. Lets see if that works. Cheers,
Nav
Last edited by nav33n; Dec 19th, 2007 at 10:20 am.
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: May 2007
Posts: 20
Reputation:
Solved Threads: 0
i use php ----
Apache version :Apache/2.0.55 (Win32) PHP version :5.1.2 Loaded extensions :
bcmath, calendar, com_dotnet, ctype, date, ftp, iconv, odbc, pcre, Reflection, session, libxml, standard, tokenizer, zlib, SimpleXML, dom, SPL, wddx, xml, xmlreader, xmlwriter, apache2handler, mbstring, mysql, mysqli, PDO, pdo_sqlite, SQLite MySQL version :5.0.18-nt
this the problem... i want to say is the problem of php version....
or do i need any change in my php.ini file
Apache version :Apache/2.0.55 (Win32) PHP version :5.1.2 Loaded extensions :
bcmath, calendar, com_dotnet, ctype, date, ftp, iconv, odbc, pcre, Reflection, session, libxml, standard, tokenizer, zlib, SimpleXML, dom, SPL, wddx, xml, xmlreader, xmlwriter, apache2handler, mbstring, mysql, mysqli, PDO, pdo_sqlite, SQLite MySQL version :5.0.18-nt
this the problem... i want to say is the problem of php version....
or do i need any change in my php.ini file
![]() |
Other Threads in the PHP Forum
- Previous Thread: PHP array_merge memory limit - Optimise function
- Next Thread: Delete Records With Effect Using jQuery And PHP
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql number oop parameter paypal pdf php phpincludeissue problem query radio random recourse recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white wordpress xml youtube






