php mysql delete records (url value delete data)

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags .url address address bar based code data delete delete data

php tutorial mysql tutorial to delete records (url value delete data 0
This section to delete the data code is based on the user url address bar passed parameters, go to the mysql database tutorial delete operation oh.
* /

$ host = 'localhost';
$ user_name = 'root';
$ password = 'admin';

$ conn = mysql_connect ($ host, $ user_name, $ password);
if (! $ conn)
{
die ('database connection failed:'. mysql_error ());
}
mysql_select_db ('test');

$ sql = 'select id, name, city, created_time from users';

$ result = mysql_query ($ sql) or die ("error: <b>". mysql_error (). "</ b> sql causing the problem:". $ sql);
?>
<html>
<head>
<title> 13-12.php </ title>
<script language = "webpage effects">

</ script>
</ head>
<center>

<body>
<table width = "75%" border = "0" cellpadding = "0" cellspacing = "1" bgcolor = "# 7b7b84">
<tr bgcolor = "# 8bbcc7">
<td height = "33"> <div align = "center"> <strong> user id </ strong> </ div> </ td>
<td> <div align = "center"> <strong> username </ strong> </ div> </ td>
<td> <div align = "center"> <strong> From City </ strong> </ div> </ td>
<td> <div align = "center"> <strong> Registration Hours </ strong> </ div> </ td>
<td> <div align = "center"> <strong> Actions </ strong> </ div> </ td>
</ tr>

<? php
if ($ num = mysql_num_rows ($ result))
{
while ($ row = mysql_fetch_array ($ result, mysql_assoc))
{
?>
<tr bgcolor = "# ffffff">
<td height = "22" align = "right"> <? php echo $ row ['id'];?> & nbsp; </ td>
<td height = "22"> & nbsp; <? php echo $ row ['name'];?> & nbsp; </ td>
<td height = "22"> & nbsp; <? php echo $ row ['city'];?> & nbsp; </ td>
<td height = "22"> & nbsp; <? php echo $ row ['created_time'];?> & nbsp; </ td>
<td height = "22"> & nbsp; <a onclick = "javascript: if (confirm ('determine to delete user info?')) return true; else return false;" href = "13-13.php? id = <? php echo $ row ['id'];?> "> delete </a> & nbsp; </ td>
</ tr>
<? php
}
}
mysql_close ($ conn);
?>

</ table>
</ body>
</ center>
</ html>
<? php
if (! isset ($ _ get ['id']))
{
echo 'parameter error! ';
exit
}

$ id = $ _get ['id'];
if (empty ($ id))
{
echo 'user id can not be empty! ';
exit
}

$ host = 'localhost';
$ user_name = 'root';
$ password = 'admin';

$ conn = mysql_connect ($ host, $ user_name, $ password);
if (! $ conn)
{
die ('database connection failed:'. mysql_error ());
}
mysql_select_db ('test');

/ / First determine whether the id of the user
$ sql = "select * from users where id = $ id";
$ result = mysql_query ($ sql) or die ("error: <b>". mysql_error (). "</ b> sql:". $ sql);
if (! mysql_num_rows ($ result))
{
echo 'user id error! ';
exit
}

// delete user data
$ sql = "delete from users where id = $ id";
mysql_query ($ sql) or die ("error: <b>". mysql_error (). "</ b> sql:". $ sql);
mysql_close ($ conn);

echo 'data deleted successfully, return <a href="13-12.php"> 13-12.php </a> to view the data';

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.