Delete records from MySQL database with HTML form and PHP
I have created a database with a table named “test’ with 2 fields “name” and “password”.
I have created a HTML file with a HTML form called “deleteform.html”.
I have created a php file called “delete.php”.
Here are the codes:
For deleteform.html:
A HTML form with 1 text field and one submit button create here. User has to input the name into the “name” text field. When the submit button is clicked, the form will direct us to the “delete.php” file:
<form action="delete.php" method="post"> Name: <input name="name" type="text" /> <input name="Submit" type="submit" value="delete record" /> </form> |
The HTML form:
Name:
For delete.php:
First we have to connect to database.
mysql_connect("Host Name", "User Name", "User Password") or die("Connection Failed"); mysql_select_db("DataBase Name")or die("Connection Failed"); |
Then we create a PHP variable to hold the value from the text field “name” of the HTML form.
$name = $_POST['name']; |
Now we create the query for deleting all of the records which equals to the name from the text field “name” of the HTML form.
$query = "delete from test where name = '".$name."'"; |
Now we run the query with the “mysql_query( )” PHP function and to check whether the records have deleted successfully, I have put an “if” condition there to echo out the result.
if(mysql_query($query)){ echo "deleted";} else{ echo "fail";} |
All together for delete.php:
1 2 3 4 5 6 7 8 | mysql_connect("Host Name", "User Name", "User Password") or die("Connection Failed"); mysql_select_db("DataBase Name")or die("Connection Failed"); $name = $_POST['name']; $query = "delete from test where name = '".$name."'"; if(mysql_query($query)){ echo "deleted";} else{ echo "fail";} |
Similar Posts:
- Delete table from MySQL database with HTML form and PHP
- Display data from MySQL database with HTML form and PHP
- MD5 – insert an encrypted string or password to the MySQL database with HTML form and PHP
- Empty a table in MySQL database with HTML form and PHP
- Insert values into MySQL database with HTML form and PHP
- Update a MySQL database field data with HTML form and PHP
- Get MySQL database records as HTML form text field’s value
- Use PHP to change MySQL field size
- Create MySQL database table with PHP
- Check how many record in the MySQL database matches the query






sachin said,
thanks this code information
rajesh said,
Is this query deleted for particular records? but i required fetch the data from database and delete rows values in front of html pages
Shek said,
Hey Rajesh,
If that is the case, you should use select statement to get all the record and display in a html table and delete the record by creating a textfield for inputting the primary key to do so.
Add A Comment