PHP数据库页面增删查

Posted 小太白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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=\'shanchu.php?code={$attr[0]}\' onclick=\\"return confirm(\'确定删除么\')\\">
        删除
        </a>
        </td></tr>";
    }
    
    
    ?> 

</table>
<a href="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="添加" /></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数据库页面增删查的主要内容,如果未能解决你的问题,请参考以下文章

用php的mvc框架实现增删查改功能(php+mysql)

怎样快速学会mysql的增删查改

如何在JSP页面中实现对数据库的增删查改?

Java代码实现 增删查 + 分页——实习第四天

php数据访问增删查

php+mysql基础增删查改