首頁 > 後端開發 > php教程 > php資料庫增刪改查方式

php資料庫增刪改查方式

一个新手
發布: 2023-03-16 17:20:02
原創
1879 人瀏覽過

先建立一個資料庫db_0808,將db_0808中表格student匯入網頁。

CURD.php

<!DOCTYPE html>
<html>
<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=&#39;0&#39;";
//$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=&#39;delete.php?id={$data[0]}&#39;>删除</a>
                     <a href=&#39;xiugai.php?id={$data[0]}&#39;>修改</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[&#39;name&#39;];//
 var_dump($name);
 $sex = $_POST[&#39;sex&#39;];
 $ban=$_POST[&#39;banji&#39;];//
 $age = $_POST[&#39;age&#39;];
 $birthday = $_POST[&#39;birthday&#39;];
 $db=new Mysqli("localhost","root","root","db_0808");
 $sql = "INSERT INTO student VALUES (null,&#39;{$name}&#39;,{$sex},{$ban},&#39;{$birthday}&#39;,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[&#39;id&#39;];$db=new Mysqli("localhost","root","root","db_0808");empty(mysqli_connect_error())?"":die("链接错误");//$sql="DELETE FROM student WHERE Sno=&#39;{$id}&#39;";//彻底删除,数据库中内容删除$sql = "update student set is_delete = &#39;1&#39; where Sno= &#39;{$id}&#39;";//表面删除,数据库中内容仍存在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[&#39;id&#39;])){    
    $id = $_GET[&#39;id&#39;];    
    $db=new Mysqli("localhost","root","root","db_0808");    
    empty(Mysqli_connect_error())?"":die("连接错误");    
    $sql="select * from student where Sno=&#39;{$id}&#39;";    
    $r=$db->query($sql);//var_dump($r);
    $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=&#39;checked&#39;";   ?> id="nv"><label for="nv">女</label>
    <input type="radio" name="sex" value="1" <?php echo $s[2]?"checked=&#39;checked&#39;":"";   ?> 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[&#39;id&#39;];
$name=$_POST[&#39;name&#39;];
$sex=$_POST[&#39;sex&#39;];
$banji=$_POST[&#39;banji&#39;];
$birthday=$_POST[&#39;birthday&#39;];
$db=new Mysqli("localhost","root","root","db_0808");
empty(Mysqli_connect_error())?"":"连接错误";
$sql="UPDATE student SET Sname=&#39;{$name}&#39;,Ssex=&#39;{$sex}&#39;,class=&#39;{$banji}&#39;,birthday=&#39;{$birthday}&#39;WHERE Sno=&#39;{$id}&#39;";
//var_dump($sql);
if ($db->query($sql)){
    header("location:CURD.php");
}
登入後複製

以上是php資料庫增刪改查方式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板