码迷,mamicode.com
首页 > 其他好文 > 详细

数据访问例题

时间:2016-06-14 00:53:09      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

1.查询表中元素,性别显示男女,民族显示名称

技术分享

</head>

<body>

<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
    <td>代号</td>
    <td>姓名</td>
    <td>性别</td>
    <td>民族</td>
    <td>生日</td>
   </tr>

<?php

//造对象
$db= new Mysqli("localhost","root","","aaas");
//判断是否出错
!mysqli_connect_error() or die("连接失败!");
//写SQL语句
$sql="select * from info";
//执行SQL语句
$result=$db->query($sql);
//读取数据
$attr=$result->fetch_all();
//遍历数组,显示
foreach($attr as $v)
{

    $sex=$v[2]?"男":"女";

    $sql="select name from nation where code=‘{$v[3]}‘";
    $r=$db->query($sql);
    $a=$r->fetch_row();

    echo "
    <tr>
    <td>{$v[0]}</td>
    <td>{$v[1]}</td>
    <td>{$sex}</td>
    <td>{$a[0]}</td>
    <td>{$v[4]}</td>
   </tr>
    ";
}

?>
</table>

  

 2.添加数据

 主页:

<!--##############################################添加数据##############################################-->


<h1>添加数据</h1>
<form action="0613addchuli.php" method="post">

<div>代号:<input type="text" name="code"></div>
<div>姓名:<input type="text" name="name"></div>
<div>性别:<input type="radio" value="1" name="sex">男
    <input type="radio" value="0" name="sex">女
</div>
    <div>民族:
        <select name="nation">
            <?php
            $db=new Mysqli("localhost","root","","aaas");
            !mysqli_connect_error() or die("连接错误!");
            $sql="select * from nation";
            $result=$db->query($sql);
            $attr=$result->fetch_all();
            foreach($attr as $v)
            {
                echo "<option value=‘{$v[0]}‘>{$v[1]}</option>";
            }

            ?>

            </select>
    </div>
    <div>生日:<input type="text" name="birthday"></div>
    <input type="submit" value="添加">
</form>

<a>返回主页面</a>

  

"0613addchuli.php"代码
<?php
 $code=$_POST["code"];
$name=$_POST["name"];
$sex=$_POST["sex"];
$nation=$_POST["nation"];
$birthday=$_POST["birthday"];

$s=$sex==1?‘true‘:‘false‘;//没必要处理
//造对象
$db=new mysqli("localhost","root","","aaas");
//判断连接是否正确
!mysqli_connect_error() or die("链接失败!");
//写SQL语句
$sql="insert into info values(‘{$code}‘,‘{$name}‘,{$s},‘{$nation}‘,‘{$birthday}‘)";//$s属于bool型,不要加单引号
//执行SQL语句
$r=$db->query($sql);
if($r)
{
    header("location:0613chaxun.php");
}
else{
    echo "添加失败!";
}

  

 3.删除数据

 

<!--#######################删除表中元素#######################-->


<table width="100%" border="1" cellspacing="0" cellpading="0">
<tr>
    <td>代号</td>
    <td>姓名</td>
    <td>性别</td>
    <td>民族</td>
    <td>生日</td>
    <td>操作</td>
</tr>

    <?PHP

    //早对象那个
    $db=new mysqli("localhost","root","","aaas");
    //判断链接
    !mysqli_connect_error() or die("链接错误!");
    //SQL语句
    $sql="select * from info";
    //执行SQL语句
    $result=$db->query($sql);
    //读取数据
    $attr=$result->fetch_all();




    foreach($attr as $v)
    {
    //性别显示男女$v[2]
        $sex=$v[2]?"男":"女";
        //判断民族 $v[3]
        $sql1="select name from nation where code=‘$v[3]‘";
        $re=$db->query($sql1);
        $a=$re->fetch_row();
        echo "
        <tr>
        <td>{$v[0]}</td>
        <td>{$v[1]}</td>
        <td>{$sex}</td>
        <td>{$a[0]}</td>
        <td>{$v[4]}</td>
        <td><a href=‘0613delete.php?code={$v[0]}‘>删除</a></td>
</tr>
        ";
    }

    ?>

</table>

 

0613delete.php代码

 

<?php
$code=$_GET["code"];

$db=new mysqli("localhost","root","","aaas");

!mysqli_connect_error () or die("了解失败!");

$sql="delete from info where code=‘{$code}‘";

$r=$db->query($sql);

if ($r)
{
    header("location:0612chaxun.php");
}
else{
    echo "删除失败!";
}

  4.修改数据

<!--#######################修改表中元素#######################-->

<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
    <td>代号</td>
    <td>姓名</td>
    <td>性别</td>
    <td>民族</td>
    <td>生日</td>
    <td>操作</td>
</tr>
    <?php
    //造对象
    $d=new mysqli("localhost","root","","aaas");
    //判断连接
    !mysqli_connect_error()or die("链接失败!");
    //构造SQL函数‘
    $sql="select * from info";
    //执行SOL函数
    $result = $db->query($sql);
    //读取数组
    $attr=$result->fetch_all();
    //遍历数组
    foreach($attr as $v)
    {
        //性别显示男女
        $sex=$v[2]?"男":"女";
        //民族显示名字 $v[3]
        $sql="select name from nation where code=‘$v[3]‘";
        $re=$db->query($sql);
        $a=$re->fetch_row();
        echo "
        <tr>

        <td>{$v[0]}</td>
        <td>{$v[1]}</td>
        <td>{$sex}</td>
        <td>{$a[0]}</td>
        <td>{$v[4]}</td>
        <td><a href=‘0613update.php?code={$v[0]}‘>修改</a></td>

</tr>
        ";
    }

    ?>
</table>

  

0613update.php 代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>

<h1>修改数据</h1>
<?php
$code = $_GET["code"];
$db=new mysqli("localhost","root","","aaas");
$sql="select * from info where code=‘{$code}‘";
$result=$db->query($sql);
$attr=$result->fetch_row();

?>


<form action="0613updatechuli.php" method="post">

    <div>
        代号:<input type="text" name="code" value="<?php echo $attr[0] ?>">

    </div>
    <div>
        姓名:<input type="text" name="name" value="<?php echo $attr[1] ?>">
    </div>

    <div>

        性别:<input type="radio" value="1" name="sex" <?php echo $attr[2]?"checked=‘checked‘":"" ?>>男
        <input type="radio" value="0" name="sex" <?php echo $attr[2]?"":"checked=‘checked‘" ?>>女

    </div>
    <div>
         民族:
        <select name="nation">
            <?php
            $db=new Mysqli("localhost","root","","aaas");
            !mysqli_connect_error() or die("连接错误!");
            $sql="select * from nation";
            $result=$db->query($sql);
            $attr1=$result->fetch_all();
            foreach($attr1 as $v)
            {
                //判断民族是否选中
                if($attr[3] ==$v[0])
                {
                    echo "<option selected=‘selected‘ value=‘{$v[0]}‘>{$v[1]}</option>";
                }
                else
                {
                    echo "<option value=‘{$v[0]}‘>{$v[1]}</option>";
                }

            }

            ?>

        </select>
    </div>
    <div>生日:<input type="text" name="birthday" value="<?php echo $attr[4] ?>"></div>
    <input type="submit" value="添加">
</form>

<a>返回主页面</a>

  

0613updatechuli.php代码

 

<?php

$code=$_POST["code"];
$name=$_POST["name"];
$sex=$_POST["sex"];
$nation=$_POST["nation"];
$birthday=$_POST["birthday"];



$s=$sex==1?‘true‘:‘false‘;//没必要处理
//造对象
$db=new mysqli("localhost","root","","aaas");
//判断连接是否正确
!mysqli_connect_error() or die("链接失败!");
//写SQL语句
$sql="update  info set name=‘{$name}‘,sex={$sex},nation=‘{$nation}‘,birthday=‘{$birthday}‘ where code=‘{$code}‘";
//执行SQL语句
$r=$db->query($sql);
if($r)
{
    header("location:0613chaxun.php");
}
else{
    echo "修改失败!";
}

  

 

数据访问例题

标签:

原文地址:http://www.cnblogs.com/pangchunlei/p/5582485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!