| | |
help with delete multiple rows in mysql using checkboxes
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 18
Reputation:
Solved Threads: 0
please help i don't know how to delete multiple rows in mysql using checkboxes, because it seems the delete doesn't seem to work.
here's the code:
here's the code:
php Syntax (Toggle Plain Text)
<?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); $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=delete_multiple.php\">"; } } mysql_close(); ?> </table> </form> </td> </tr> </table>
Last edited by fortiz147; Aug 13th, 2008 at 5:09 am. Reason: code tag
see this code will work at this url:
http://www.daniweb.com/forums/thread134908.html
http://www.daniweb.com/forums/thread134908.html
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
ya,this is the continuation code for the above post...
after getting all check boxes you checked,then write this query to delete them(checked)
after getting all check boxes you checked,then write this query to delete them(checked)
php Syntax (Toggle Plain Text)
if(isset($_POST['check_compare'])) { $tempids=$_POST['check_compare']; echo $tempids; $ser_qry="delete from sometable where find_in_set(p_id,'".$_POST['check_compare']."')"; $ser_res=mysql_query($ser_qry); }
Last edited by Shanti Chepuru; Aug 13th, 2008 at 7:00 am.
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
•
•
Join Date: Jun 2008
Posts: 40
Reputation:
Solved Threads: 2
hi.. i am a also a newbie.. try this one.. this worked for me. 

php Syntax (Toggle Plain Text)
<html> <head> </head> <body> <?php //mysql connection $dbconn = mysql_connect("","","") or die(); mysql_select_db("table") or die(); $sqlquery = "your query"; // query on table $sqlresult = mysql_query($sqlquery, $dbconn); $count = mysql_num_rows($sqlresult); // count query result ?> <form method="post" action="delete.php"> <!-- some table headers here / or html tags --> <?php //loop here while($row = mysql_fetch_array($sqlresult)) { //inside while ?> <tr> <td><input type="checkbox" name="checkbox[]" id="checkbox[]" value="<?php echo $row['id']?>" /></td> <!-- and other data you want to print out.. --> </tr> <?php } ?> <tr><input id='delete' type='submit' name='delete' value='Delete'/></tr> </form> </body> </html> <?php #these following code for delete.php file ?> <html> <?php //mysql connection here if($_POST['delete']) // from button name="delete" { $checkbox = $_POST['checkbox']; //from name="checkbox[]" $countCheck = count($_POST['checkbox']); for($i=0;$i<$countCheck;$i++) { $del_id = $checkbox[$i]; $sql = "delete from table where id = $del_id"; $result = mysql_query($sql, $dbconn); } if($result) { echo "successful delete"; } else { echo "Error: ".mysql_error(); } } ?> </html>
Last edited by enim213; Aug 14th, 2008 at 5:21 am.
![]() |
Similar Threads
Other Threads in the PHP Forum
- Previous Thread: need HELP
- Next Thread: Using A List Box in If Statement
| Thread Tools | Search this Thread |
.htaccess alerts apache api archive array autocomplete beginner binary broken cakephp checkbox class cms code convert cron curl database dataentry date display duplicates dynamic echo email emptydisplayvalue error execute explodefunction file files firstoptioninphpdroplist folder form forms function functions google hack href htaccess html htmlspecialchars image include insert ip javasciptvalidation javascript joomla keywords limit link login mail matching menu methods mlm multiple mysql network object oop paypal pdf php problem query radio random recursion recursive redirect remote script search securephp server sessions shot sms source space sql subscription syntax system table tutorial tutorials update upload url validator variable video web youtube






