PHP simple : PHP tutorial, MySQL tutorial
PHP Tutorial - Free PHP Script

Home | Tutorials | PHP & MySQL Resources | Web Tools
Introduction & Get Start
Using Form
PHP
PHP Tags
PHP Variables
PHP Control Structures
MySQL
Create Database & Table
Database Connection
Insert Record
Select Record
Update Record
Delete Record

MySQL : Delete Record

Delete records from MySQL database is an easily SQL command. You need to put the key value just as id, name or others into this command. This tutorial use the file select.php from Update Record tutorial and make a new file delete.php.

Syntax :

delete from table_name where column_name='value'

This tutorail need 2 files.

  1. select.php from Update Record tutorial.
  2. delete.php


select.php

In this file, you have to put some codes for a new link. This link will send GET method parameter to delete.php.

select.php
<?
// Connect database
include("connectdb.php");

// Get all records in all columns from table and put it in $result.
$result=mysql_query("select * from phonebook");

/*Split records in $result by table rows and put them in $row.
Make it looping by while statement. */

while($row=mysql_fetch_assoc($result)){

// Output
echo "ID : $row['id'] <br/>";
echo "Name : $row['name'] <br/>";
echo "Email : $row['email'] <br/>";
echo "Tel : $row['tel'] <hr>";


// Add a link with a parameter(id) and it's value. This for update record at update.php
echo '<a href="update.php?id='.$row['id'].'">Update</a>';

// Add a link with a parameter(id) and it's value. This for delete record at delete.php
echo '<a href="delete.php?id='.$row['id'].'">Delete</a>';

}

mysql_close();
?>


Browse "select.php" by go to http://localhost/select.php.

Result : (Links below are NOT real on this page)

ID : 1
Name : Jack
Email : [email protected]
Tel : 12345678
Update
Delete <- when you mouse over this. It will show "http://localhost/delete.php?id=1"in your browser's status bar.


ID : 2
Name : Joe
Email : [email protected]
Tel : 87654321
Update
Delete <- when you mouse over this. It will show "http://localhost/delete.php?id=2" in your browser's status bar.

...

The values are the same as ID parameters. When you click on these links, the page will change to delete.php.



delete.php

This file will get parameter($id) from select.php and delete the record where id column equal in $id .

insert.php
<?
// Connect database.
include("connectdb.php");

// Get values from form.
$id=$_GET['id'];

// Do delete statement.
mysql_query("delete from phonebook where id='$id'");

// Close database connection
mysql_close();

// Redirect to select.php.
header("location:select.php");

?>

 

Test

Browse select.php on your web browser as http://localhost/select.php and click on "Delete" link. This page will go to delete.php with id record and delete.php will delete this record and re-direct back to select.php immediately.


Input - Output String Functions
This tutorial is showing some functions that you need to use for your inputing form (as text field, text area) because, it is not security to receive string datas and input them directly into your database.
Make a Number Format
In this tutorial we'll make number format like 0001, 0002, 0003 in stead of 1, 2 ,3. It's very easy to do this for your counter, category or product ID. etc.


PHPsimple.net
[email protected]