首页 > php教程 > php手册 > 正文

php mysql 删除记录, url传值删除数据

php中文网
发布: 2016-05-25 16:42:35
原创
1124人浏览过

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

<?php
$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 > < tdheight = "22" > & nbsp; < ? phpecho $row['name']; & nbsp; < / td > < tdheight = "22" > & nbsp; < ? phpecho $row['city']; & nbsp; < / td > < tdheight = "22" > & nbsp; < ? phpecho $row['created_time']; & nbsp; < / td > < tdheight = "22" > & nbsp; < aonclick = "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 (emptyempty($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;
}
//删除用户数据//开源代码phprm.com
$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>查看数据';
?>
登录后复制


本文地址:

绘蛙AI商品图
绘蛙AI商品图

电商场景的AI创作平台,无需高薪聘请商拍和文案团队,使用绘蛙即可低成本、批量创作优质的商拍图、种草文案

绘蛙AI商品图 148
查看详情 绘蛙AI商品图

转载随意,但请附上文章地址:-)

相关标签:
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号