Delete records from MySQL database with HTML form and PHP

Posted by Shek on May 15, 2008 under PHP | 3 Comments to Read | Total View: 19,625 views

Editor’s note: In some cases such as a company’s login system for example, the login names and passwords are stored in the MySQL database. But if a staff leaves the company and so his account has to be removed from the database. Therefore it is good to design a HTML form for the administrator to remove the record from the database. And here is the example.

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:

VN:F [1.9.22_1171]
Rating: 9.4/10 (9 votes cast)
VN:F [1.9.22_1171]
Rating: +2 (from 2 votes)
Delete records from MySQL database with HTML form and PHP, 9.4 out of 10 based on 9 ratings

Not what you want? Try a Search

  • sachin said,

    thanks this code information

    VA:F [1.9.22_1171]
    Rating: 5.0/5 (1 vote cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 2 votes)
  • 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

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  • 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.

    VN:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.22_1171]
    Rating: 0 (from 0 votes)

Add A Comment

*