首页 > 代码库 > PHP数据库页面增删查

PHP数据库页面增删查

1.用户操作页面

<!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>
<table width="100%" border="1" cellpadding="0" cellspacing="0" >
    <tr>
        <td>代号</td>
        <td>名称</td>
        <td>价格</td>
        <td>产地</td>
        <td>库存</td>
        <td>操作</td>
    </tr>
    
    <?php
    //造连接对象
    $db = new MySQLi("localhost","root","123","mydb");
    //写SQL语句
    $sql = "select * from fruit";
    
    //执行
    $result = $db->query($sql);
    
    //取数据
    /*$attr = $result->fetch_all();
    
    foreach($attr as $v)
    {
        echo "<tr><td>{$v[0]}</td><td>{$v[1]}</td><td>{$v[2]}</td><td>{$v[3]}</td><td>{$v[4]}</td></tr>";
    }*/
    
    while($attr = $result->fetch_row())
    {
        echo "<tr><td>{$attr[0]}</td><td>{$attr[1]}</td><td>{$attr[2]}</td><td>{$attr[3]}</td><td>{$attr[4]}</td><td>
        
        <a href=http://www.mamicode.com/‘shanchu.php?code={$attr[0]}‘ onclick=\"return confirm(‘确定删除么‘)\">
        删除
        </a>
        </td></tr>";
    }
    
    
    ?> 

</table>
<a href="http://www.mamicode.com/add.php">添加数据</a>
</body>
<script type="text/javascript">

</script>
</html>

技术分享

 

 

2.添加数据页面

<!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>
<form action="addchuli.php" method="post">
<div>代号:<input type="text" name="ids" /></div>
<div>名称:<input type="text" name="name" /></div>
<div>价格:<input type="text" name="price" /></div>
<div>产地:<input type="text" name="chandi" /></div>
<div>库存:<input type="text" name="numbers" /></div>
<div><input type="submit" value="http://www.mamicode.com/添加" /></div>
</form>
</body>
</html>

技术分享

 

 

3.删除处理页面

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

//造连接对象
$db = new MySQLi("localhost","root","123","mydb");

//写SQL语句
$sql = "delete from fruit where ids=‘{$code}‘";

//执行
$r = $db->query($sql);

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

4.添加处理页面

<?php
$ids = $_POST["ids"];
$name = $_POST["name"];
$price = $_POST["price"];
$chandi = $_POST["chandi"];
$numbers = $_POST["numbers"];

//造连接对象
$db = new MySQLi("localhost","root","123","mydb");

//写SQL语句
$sql = "insert into fruit values(‘{$ids}‘,‘{$name}‘,{$price},‘{$chandi}‘,{$numbers},‘‘)";

//执行
$r = $db->query($sql);

if($r)
{
    header("location:main.php");
}
else
{
    echo "添加失败!";
}

 

PHP数据库页面增删查