首页 > php代码 > php mysql 删除记录( url传值删除数据)

php mysql 删除记录( url传值删除数据)

本款删除数据代码是一款根据用户url地址栏传过来的参数,再去mysql数据库中执行删除操作哦。
 代码如下 复制代码

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

$conn = mysql_connect($host,$user_name,$password);
if(!$conn)
{
    die('数据库连接失败:'.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/>产生问题的sql:".$sql);
?>
<html>
<head>
<title>13-12.php</title>
<script language="网页特效">

</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>用户id</strong></div></td>
        <td><div align="center"><strong>用户名称</strong></div></td>
        <td><div align="center"><strong>来自城市</strong></div></td>
        <td><div align="center"><strong>注册时间</strong></div></td>
        <td><div align="center"><strong>操作</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('确定要删除用户信息吗?')) return true; else return false;" href="13-13.php?id=<?php echo $row['id']; ?>">删除</a>&nbsp;</td>
    </tr>
<?php
    }
}
mysql_close($conn);
?>

</table>
</body>
</center>
</html>

 代码如下 复制代码

<?php
if(!isset($_get['id']))
{
    echo '参数错误!';
    exit;
}

$id = $_get['id'];
if(empty($id))
{
    echo '用户id不能为空!';
    exit;
}

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

$conn = mysql_connect($host,$user_name,$password);
if(!$conn)
{
    die('数据库连接失败:'.mysql_error());
}
mysql_select_db('test');

//先判断是否存在该id的用户
$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 '用户id错误!';
    exit;
}

//删除用户数据
$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 '数据删除成功,返回<a href="13-12.php">13-12.php</a>查看数据';



永久地址:http://www.phprm.com/code/34352.html

转载随意~请带上教程地址吧^^

标签:数据库连接

相关文章

发表留言