Jump to content

Delete Mysql rows with php


joshuaer

Recommended Posts

Hello I am trying to figure out how to delete multiple rows with php script. I found several methods searching the internet but I came across one article that lays out the entire script for you http://www.phpeasyst...om/mysql/8.html So I created the test database and used the script provided. It all seemed to be going well, it does not throw up any errors at any point of the script but when I choose the rows I want to delete and clock delete it just refreshes the page and does not remove any of the rows This is the code I am using, I was hoping someone might be able to point me in the direction to try and figure out what I am doing wrong. Thanks <?php$host="."; // Host name$username=""; // Mysql username$password=""; // Mysql password$db_name=""; // 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); echo '<meta name="viewport" content="width=device-width; initial-scale=1.0">'; $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> <?phpwhile($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> <?php // Check if delete button active, start thisif($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.phpif($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=test.php\">"; }} mysql_close();echo "$checkbox"?> </table></form></td></tr></table>

Edited by joshuaer
Link to comment
Share on other sites

For one, you're displaying the rows before you delete them. You're also not setting the value of $checkbox anywhere, you need to get the submitted form data from $_POST. That code will only work on very old versions of PHP with outdated settings. I would recommend that you add error reporting to the top of your code, it will report issues like that: ini_set('display_errors', 1);error_reporting(E_ALL);

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...