首页 > 代码库 > 补完 简单的增删

补完 简单的增删

目的是为了在页面现实数据并且实现增加和删除数据的功能

1.首先需要布局表格来放数据库里面的内容

例:

<table width="100%" border="1" cellpadding="0" cellspacing="0" >
    <tr>
        <td>姓名</td>
        <td>身份证号</td>
        <td>电话</td>
        <td>房间类型</td>
        <td>定金</td>
        <td>操作</td>
    </tr>

2.查询数据库病显示

<?php
    
        $db = new MySQLi("localhost","root","123","酒店");
        $sql = "select * from yuding";
        
        $result = $db->query($sql);
        
        while($attr = $result->fetch_row())
    {
        echo "<tr><td>{$attr[1]}</td><td>{$attr[2]}</td><td>{$attr[3]}</td><td>{$attr[4]}</td><td>{$attr[5]}</td><td>
        
        
        </td></tr>";
    }    
    
    
    
    
    
    ?>

3.添加页面

<?php

$name = $_POST["name"];
$id = $_POST["id"];
$tel = $_POST["tel"];
$type = $_POST["type"];
$dmoney = $_POST["dmoney"]; 
if(!empty($name) && !empty($id) && !empty($tel) && !empty($type) && !empty($dmoney))//这句是为了保证文本框里面都有内容,;类似非空验证
{
            $db = new MySQLi("localhost","root","123","酒店");
        
        $sql = "insert into yuding values(‘‘,‘{$name}‘,‘{$id}‘,‘{$tel}‘,‘{$type}‘,‘{$dmoney}‘)";
        
        
        $x = $db->Query($sql);
        if($x)
        {
            header("location:yuding.php");
        }
        else
        {
            echo"添加失败=-=";
        }
}else
{
    echo "内容不能有空";
    header("location:yuding.php");
    }

4.删除以及删除页面

在上面2.的代码里面添加

<td>

<a href=http://www.mamicode.com/‘ydshanchu.php?code={$attr[0]}‘ onclick=\"return confirm(‘确定删除么‘)\">
删除
</a>
</td>, 

<?php
$code = $_GET["code"];
$db = new MySQLi("localhost","root","123","酒店");

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

$x = $db->query($sql);
if($x)
{
    header("location:yuding.php");
}
else
{
    echo "删除失败";
}

 

补完 简单的增删