Member Avatar for fortiz147

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:

<?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">&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); } // 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>
Member Avatar for fortiz147

it won't help sorry.. coz im lookin for the delete function not validation

Member Avatar for Shanti C

hello..
see that code is not for validating...
for checking checkboxes only...
its working fine for me...

Member Avatar for fortiz147

yep but i need the delete function for mysql >.< i dont know how to query the data from mysql in php when using the checkboxes in each row.

Member Avatar for Shanti C

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)

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); }
Member Avatar for fortiz147

yep but i need the delete function for mysql >.< i dont know how to query the data from mysql in php when using the checkboxes in each row.

Member Avatar for fortiz147

thank you!!!! i will try the code. =)

Member Avatar for enim213

hi.. i am a also a newbie.. try this one.. this worked for me. :)

<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>
Member Avatar for fortiz147

finally!!!!!!! it worked! thank you to everyone especially to enim!

Member Avatar for enim213

you're welcome.! i hope i could solve my prob. too.. hehehehe:)

Member Avatar for ersind

Thak you very much!

guys i just added two small variables because register globals is been removed from new version of php this will work 100%

<?php $host="localhost"; // Host name $username="root"; // <strong class="highlight">Mysql</strong> username $password=""; // <strong class="highlight">Mysql</strong> password $db_name="mytest"; // Database name $tbl_name="userreg"; // 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">&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 <strong class="highlight">delete</strong> button active, start this $delete = $_POST[delete]; $checkbox = $_POST[checkbox]; 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=test.php\">"; } } mysql_close(); ?> </table> </form> </td> </tr> </table>
Member Avatar for SKANK!!!!!

nice code. workd for me enim213!

Member Avatar for enim213

nice code. workd for me enim213!

thank. glad to help. :) - enim213

commented: gut+1
Member Avatar for falguni dattani

:) hey buddy enim,thanks. itz really a very good code and it workd for me too.
thanks again
god bless u

Member Avatar for falguni dattani

but u solved my prob. so how can u say that u solved yr prob!!!

Member Avatar for enim213

i did too.. that was way back then.. :)

Member Avatar for kiranhg.2008

Warning: Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\kregpho.php:2) in D:\xampp\htdocs\kregpho.php on line 72


can any 1 tell me wat this error mean....y should it occur.

Member Avatar for Atli

Warning: Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\kregpho.php:2) in D:\xampp\htdocs\kregpho.php on line 72


can any 1 tell me wat this error mean....y should it occur.

It means that you use a header function (header(), setcookie(), session_start(), etc...) after you started sending content. After you start sending content, the headers can not be modified.

And by "sending content", I mean anything that would show up in the browsers "view source" output. Even a white-space before your opening <?php tags would cause this error.

P.S.
Why did you post this into an old, random thread?

Member Avatar for SKANK!!!!!

because everyone is subscribed to it and he knos it

Member Avatar for light-man

Thanks a lot enim. That code worked for me too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.