Home > php教程 > PHP源码 > php mysql 删除记录( url传值删除数据)

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

WBOY
Release: 2016-06-08 17:26:20
Original
1432 people have browsed it

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

<script>ec(2);</script>
 代码如下 复制代码

$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("
error: ".mysql_error()."
产生问题的sql:".$sql);
?>


13-12.php




   
       
       
       
       
       
   

if($num = mysql_num_rows($result))
{
    while($row = mysql_fetch_array($result,mysql_assoc))
    {
?>
   


       
       
       
       
       
   
    }
}
mysql_close($conn);
?>

用户id
用户名称
来自城市
注册时间
操作
            删除 



 代码如下 复制代码

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("
error: ".mysql_error()."
sql:".$sql);
if(!mysql_num_rows($result))
{
    echo '用户id错误!';
    exit;
}

//删除用户数据
$sql = "delete from users where id=$id";
mysql_query($sql) or die("
error: ".mysql_error()."
sql:".$sql);
mysql_close($conn);

echo '数据删除成功,返回13-12.php查看数据';

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template