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';