Rabu, 07 Januari 2009

Cara Menghapus record.php dengan mysql

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']
";
echo "Name : $row['name']
";
echo "Email : $row['email']
";
echo "Tel : $row['tel']
";

// Add a link with a parameter(id) and it's value. This for update record at update.php
echo 'Update';

// Add a link with a parameter(id) and it's value. This for delete record at delete.php
echo 'Delete';

}

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 : jack@abcd.com
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 : joe@abcd.com
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.

Advertise
Put Ebay RSS Feeds onto your website
SEO Elite Software
Domain Dashboard CPanel & Seo Manager
Work at Home Ideas and Opportunities
• 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

Tidak ada komentar:

Poskan Komentar