PHP基础----PHP 与 MySQL----30删除用户数据



myconfig25.php

定义链接数据库常量
<span style="font-size:24px;"><span style="font-size:24px;"><span style="font-size:24px;"><?php
//定义链接数据库常量
/**
 * Created by PhpStorm.
 * User: hao
 * Date: 2015/9/3
 * Time: 7:45
 */
//定义的常亮
//本地
define('MYSQL_HOST','localhost');
//用户名
define('MYSQL_ROOT','root');
//密码
define('MYSQL_PW','');</span></span></span>

myfunctions25.php

链接数据库
<span style="font-size:24px;"><span style="font-size:24px;"><span style="font-size:24px;"><?php
//链接数据库
/**
 * Created by PhpStorm.
 * User: hao
 * Date: 2015/9/3
 * Time: 7:50
 */
require_once 'myconfig25.php';

//链接服务器的方法:
function connectDb(){
//    链接服务器
$conn=mysql_connect(MYSQL_HOST,MYSQL_ROOT,MYSQL_PW);
if(!$conn){
    die('can not conn db');
}
//    选择数据库
mysql_select_db('myapp');
return $conn;
}</span></span></span>

index25.php

从mysql数据库中查询到数据,最终写到html表格中,添加删除一列,点击跳转到删除界面
<span style="font-size:24px;"><span style="font-size:24px;"><?php
//删除用户数据
//从mysql数据库中查询到数据,最终写到html表格中,添加删除一列,点击跳转到删除界面
require_once 'myfunctions25.php';
?>
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<table width='500px' border='1' style='border-collapse:collapse;text-align: left '>
    <tr>
        <th>id</th>
        <th>名字</th>
        <th>年龄</th>
        <th>删除</th>
    </tr>

    <?php
    //链接数据库
    $conn = connectDb();
    if ($conn) {
        echo '链接成功';
//选择数据库
        mysql_select_db('myapp', $conn);
//查询表中所有的数据
//改变sql语句,可以随意查询
        $result = mysql_query("SELECT * FROM mytable");
//mytable表中有多少条数据
        $mycount = mysql_num_rows($result);
        print_r($mycount);
//循环拿出所有数据
        for ($i = 0; $i < $mycount; $i++) {
//把数据转化成数组
            $result_arr = mysql_fetch_assoc($result);
//打印出数组数据
            print_r($result_arr);
//  打印出来3条数据,这里我就写了1条,其他模式都一样
//        Array
//        (
//            [_id] =>; 1
//    [_name] =>; zhang3
//    [_age] =>; 12
//)
            $id = $result_arr['_id'];
            $name = $result_arr['_name'];
            $age = $result_arr['_age'];

            echo "<tr><td>$id</td><td>$name</td><td>$age</td><td><a href='delete25.php?id=$id'>删除</a></td></tr>";
        }
    } else {
        echo '链接失败';
    }
    ?>
    <table/>
</body>
</html>
</span></span>
delete25.php
链接数据库,删除数据库中的某条数据,并跳转到查询界面
<span style="font-size:24px;"><span style="font-size:24px;"><?php
//链接数据库,删除数据库中的某条数据,并跳转到查询界面
/**
 * Created by PhpStorm.
 * User: hao
 * Date: 2015/9/3
 * Time: 18:31
 */
if(empty($_GET['id'])){
 die("id is empty");
}
require_once 'myfunctions25.php';
connectDb();
$id=intval($_GET['id']);

mysql_query("DELETE FROM mytable  WHERE _id = $id");


if(mysql_errno()){

  die("huo qu id shi bai $id");

}else{
 header("Location:index25.php");
}
</span></span>
源码下载:
http://download.csdn.net/detail/zhaihaohao1/9079123



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值