PHP mysql Delete record (url pass value delete data)

Source: Internet
Author: User
The code is as follows Copy Code

$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 ("<br/>error: <b>". Mysql_error (). " </b><br/> problem-generating sql: ". $sql);
?>
<title>13-12.php</title>
<script language= "Web Effects" >

</script>
<center>

<body>
<table width= "75%" border= "0" cellpadding= "0" cellspacing= "1" bgcolor= "#7b7b84" >
<tr bgcolor= "#8bbcc7" >
<TD height= "><div" align= "center" ><strong> User id</strong></div></td>
<td><div align= "center" ><strong> User name </strong></div></td>
<td><div align= "center" ><strong> from City </strong></div></td>
<td><div align= "Center" ><strong> Registration Time </strong></div></td>
<td><div align= "Center" ><strong> Operations </strong></div></td>
</tr>

<?php
if ($num = mysql_num_rows ($result))
{
while ($row = Mysql_fetch_array ($result, MYSQL_ASSOC))
{
?>
<tr bgcolor= "#ffffff" >
&LT;TD height= "align=" "right" ><?php echo $row [' id '];?>&nbsp;</td>
&LT;TD height= ">&nbsp;<?php" echo $row [' name '];?>&nbsp;</td>
&LT;TD height= ">&nbsp;<?php echo $row [' City '];?>&nbsp;</td>
&LT;TD height= ">&nbsp;<?php" echo $row [' created_time '];?>&nbsp;</td>
&LT;TD height= ">&nbsp;<a onclick=" javascript:if (' Determine to delete user information? ')) 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>

The code is as follows Copy Code

<?php
if (!isset ($_get[' id '))
{
echo ' parameter Error! ';
Exit
}

$id = $_get[' id '];
if (empty ($id))
{
The echo ' User ID cannot 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 if the user of the ID exists
$sql = "SELECT * from Users where id= $id";
$result = mysql_query ($sql) or Die ("<br/>error: <b>". Mysql_error (). " </b><br/>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 ("<br/>error: <b>". Mysql_error (). " </b><br/>sql: ". $sql);
Mysql_close ($conn);

Echo data was deleted successfully, returning <a href= "13-12.php" >13-12.php</a> viewing data ';

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.