php数据库增删改查
2018-06-22 05:29:39来源:未知 阅读 ()
首先建立一个数据库db_0808,将db_0808中表格student导入网页。
CURD.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Title</title> </head> <body> <?php $db = new Mysqli("localhost","root","root","db_0808"); //!$db?"":die("链接错误"); empty(mysqli_connect_error())?"":die("链接错误"); $sql = "select * from student where is_delete='0'"; //$data = $db->query($sql)->fetch_all();//索引数组形式的所有数据 ?> <table border="1"> <tr> <td>id</td> <td>名字</td> <td>性别</td> <td>班级</td> <td>生日</td> <td>操作</td> </tr> <?php $result=$db->query($sql); while ($data=$result->fetch_row()){ //索引数组形式的第一条数据 // foreach ($data as $i){ if ($data[2]==1){ $data[2]="男"; }else if ($data[2]==0){ $data[2]="女"; }else{ $data[2]="保密"; } echo "<tr> <td>{$data[0]}</td> <td>{$data[1]}</td> <td>{$data[2]}</td> <td>{$data[3]}</td> <td>{$data[4]}</td> <td><a href='delete.php?id={$data[0]}'>删除</a> <a href='xiugai.php?id={$data[0]}'>修改</a> </td> </tr>"; } ?> </table> <a href="add.php">新增用户</a> </body> </html>
向数据库中添加新信息add.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form method="post" action="addpost.php"> <input type="text" name="name" placeholder="姓名"> <input type="radio" name="sex" value="1" id="man"><label for="man">男</label> <input type="radio" name="sex" value="0" id="nv"><label for="nv">女</label> <input type="text" name="banji" placeholder="班级"> <!-- <input type="text" name="age" placeholder="年龄">--> <input type="text" name="birthday" placeholder="出生年月"> <input type="submit" value="提交"> </form> </body> </html>
对add.php信息处理addpost.php
<?php /** * Created by fcc * User: Administrator * Date: 2017/10/13 * Time: 15:49 */ $name = $_POST['name']; //var_dump($name); $sex = $_POST['sex']; $ban=$_POST['banji']; //$age = $_POST['age']; $birthday = $_POST['birthday']; $db=new Mysqli("localhost","root","root","db_0808"); $sql = "INSERT INTO student VALUES (null,'{$name}',{$sex},{$ban},'{$birthday}',DEFAULT,null)"; if ($db->query($sql)){ header("location:CURD.php"); }else{ header("location:add.php"); }
添加信息成功
删除信息delete.php
<?php /** * Created by fcc * User: Administrator * Date: 2017/10/14 * Time: 10:56 */ $id=$_GET['id']; $db=new Mysqli("localhost","root","root","db_0808"); empty(mysqli_connect_error())?"":die("链接错误"); //$sql="DELETE FROM student WHERE Sno='{$id}'";//彻底删除,数据库中内容删除 $sql = "update student set is_delete = '1' where Sno= '{$id}'";//表面删除,数据库中内容仍存在 if ($db->query($sql)){ header("location:CURD.php"); };
更改信息页面xiugai.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body>
<?php
$s = null;
if(isset($_GET['id'])){
$id = $_GET['id'];
require_once "../t/DBDA.class.php";
// $db=new Mysqli("localhost","root","root","db_0808");
// empty(Mysqli_connect_error())?"":die("连接错误");
$db = new DBDA();
$sql="select * from student where Sno='{$id}'";
//二维数组,输出方法$r[0][0]
$r = $db->Query($sql);
$s = $r[0];
// $r=$db->query($sql);
////var_dump($r);
// fetch_row 索引数组
// $s=$r->fetch_row();
}
?>
<form method="post" action="xiugaichuli.php"> <input type="hidden" name="id" value="<?php echo $s[0]?>"> <input type="text" name="name" placeholder="<?php echo $s[1]?>"> <input type="radio" name="sex" value="0" <?php echo $s[2]?"":"checked='checked'"; ?> id="nv"><label for="nv">女</label> <input type="radio" name="sex" value="1" <?php echo $s[2]?"checked='checked'":""; ?> id="nan"><label for="nan">男</label> <input type="text" name="banji" placeholder="<?php echo $s[3]?>"> <!-- <input type="text" name="age" placeholder="年龄">--> <input type="text" name="birthday" placeholder="<?php echo $s[4]?>"> <input type="submit" value="提交"> </form> </body> </html>
更改信息处理页面xiugaichuli.php
<?php /** * Created by fcc * User: Administrator * Date: 2017/10/17 * Time: 9:07 */ $id=$_POST['id']; $name=$_POST['name']; $sex=$_POST['sex']; $banji=$_POST['banji']; $birthday=$_POST['birthday']; $db=new Mysqli("localhost","root","root","db_0808"); empty(Mysqli_connect_error())?"":"连接错误"; $sql="UPDATE student SET Sname='{$name}',Ssex='{$sex}',class='{$banji}',birthday='{$birthday}'WHERE Sno='{$id}'"; //var_dump($sql); if ($db->query($sql)){ header("location:CURD.php"); }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- PHP写UltraEdit插件脚本实现方法 2020-03-29
- php 带逗号千位符数字的处理方法 2020-03-28
- PHP三元运算符的结合性介绍 2020-03-28
- PHP静态延迟绑定和普通静态效率的对比 2020-03-28
- 基于php流程控制语句和循环控制语句 2020-03-28
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash