本篇文章給大家?guī)砹岁P(guān)于PHP怎樣利用mysql實(shí)現(xiàn)增刪改查功能的實(shí)例,希望對(duì)大家有幫助。
PHP+Mysql實(shí)現(xiàn)增刪改查
PHP 是一種創(chuàng)建動(dòng)態(tài)交互性站點(diǎn)的強(qiáng)有力的服務(wù)器端腳本語言。
數(shù)據(jù)庫(Database)是按照數(shù)據(jù)結(jié)構(gòu)來組織、存儲(chǔ)和管理數(shù)據(jù)的倉庫。每個(gè)數(shù)據(jù)庫都有一個(gè)或多個(gè)不同的 API 用于創(chuàng)建,訪問,管理,搜索和復(fù)制所保存的數(shù)據(jù)。
MySQL 是一種在 Web 上使用,在服務(wù)器上運(yùn)行的數(shù)據(jù)庫系統(tǒng);MySQL 是非??焖?,可靠,且易于使用的,支持標(biāo)準(zhǔn)的 SQL。
Mysql語句
打開我們的wampserver服務(wù)器+Mysql可視化工具(這里我用Navicat),或則其它集成工具(Apache+PHP+Mysql)都可以。鏈接上我們的服務(wù)器
我們新建查詢來操作數(shù)據(jù)庫,先把基本的文件配置好
插入一條信息
INSERT INTO 語法
- 需指定要插入數(shù)據(jù)的列名,只需提供被插入的值即可
INSERT INTO table_name VALUES (value1,value2,value3,...);
- 需要指定列名及被插入的值
INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...);
不指定列名向表格插入一條數(shù)據(jù)
INSERT INTO stu VALUES (null,'提莫', 1,30);
我們用第二種語法向表格插入一條數(shù)據(jù)
INSERT INTO stu (name, gender, age) VALUES ('Faker', 0,24);
查詢語句
SQL SELECT 語句
SELECT column_name,column_name FROM table_name;
SELECT * FROM table_name;
查詢id一列
select id from stu;
查詢當(dāng)id為1的語句
select * from stu where id = 1;
因?yàn)閕d是唯一的,所以找到了該條數(shù)據(jù)則不用再繼續(xù)
select * from stu where id = 1 limit 1;
修改語句
SQL UPDATE 語句 需要加上where語句,否則整個(gè)表格都會(huì)更新
UPDATE table_name SET column1=value1,column2=value2,... WHERE some_column=some_value;
修改名字當(dāng)id為1的時(shí)候
update stu set name='the shy' where id=1;
刪除語句
SQL DELETE 語法 WHERE 子句規(guī)定哪條記錄或者哪些記錄需要?jiǎng)h除。如果您省略了 WHERE 子句,所有的記錄都將被刪除!
DELETE FROM table_name WHERE some_column=some_value;
刪除id為2的該條學(xué)生信息
delete from stu where id = 2;
使用PHP操作Mysql
如何鏈接數(shù)據(jù)庫
header("Content-Type:text/html;charset=utf-8");// 1. 使用mysqli鏈接數(shù)據(jù)庫(這里使用wampserver默認(rèn)的)$connection = mysqli_connect('127.0.0.1', 'root', '', 'students');// 2. 解決識(shí)別不了數(shù)據(jù)庫文件的中文mysqli_query($connection,"set names 'utf8';");if (!$connection) { // 連接數(shù)據(jù)庫失敗 exit('<h1>連接數(shù)據(jù)庫失敗</h1>');}// 每次只能查詢一條數(shù)據(jù)$query = mysqli_query($connection, 'select * from stu;');// 查詢所有的數(shù)據(jù)while ($row = mysqli_fetch_assoc($query)) { var_dump($row);}
查詢數(shù)據(jù)庫渲染主頁面(index.php)
- 采用混編的方法,在頭部鏈接數(shù)據(jù)庫
<?php// 1.鏈接我們的數(shù)據(jù)庫$link = mysqli_connect('127.0.0.1', 'root', '', 'students');// 2.設(shè)置中文編碼mysqli_query($link,"set names 'utf8';");// 3.檢測鏈接if ($link->connect_error) { die("連接失敗: " . $link->connect_error);}// 4.查詢數(shù)據(jù)$query = mysqli_query($link, 'select * from stu;');// 5.渲染數(shù)據(jù)?>
- 引入bootstrap@4(bootstrap官網(wǎng)下載并引入bootstrap.css)
<link rel="stylesheet" href="./lib/bootstrap.css">
- 使用
mysqli_fetch_assoc($query)
渲染數(shù)據(jù),因?yàn)楹罄m(xù)需要添加(add.php),刪除(del.php),修改(edit)操作所以這里先添加
<p class="container"> <h1 class="text-center">首頁</h1> <table class="table table-bordered"> <thead> <tr> <th class="text-center">學(xué)號(hào)</th> <th class="text-center">姓名</th> <th class="text-center">性別</th> <th class="text-center">年齡</th> <th class="text-center">操作</th> </tr> </thead> <tbody> <?php while ($row = mysqli_fetch_assoc($query)): ?> <tr class="text-center"> <td><?php echo $row['id']; ?></td> <td><?php echo $row['name']; ?></td> <td><?php echo $row['gender']==1?'♀' : '♂' ; ?></td> <td><?php echo $row['age']; ?></td> <td> <a href="del.php?id=<?php echo $row['id'];?>" class="btn btn-primary">刪除</a> <a href="edit_get.php?id=<?php echo $row['id'];?>" class="btn btn-danger">修改</a> </td> </tr> <?php endwhile;?> </tbody> </table> <a class="btn btn-primary btn-block" href="add.php">添加學(xué)生信息</a></p>
添加一條數(shù)據(jù)(add.php)
- 我們依舊使用混編的模式,表單數(shù)據(jù)提交到本頁面,使用
$_SERVER['PHP_SELF']
使得代碼魯棒性更強(qiáng) - 使用post提交數(shù)據(jù),記得在頁面提示信息錯(cuò)誤
- 在頭部鏈接數(shù)據(jù)庫,插入一條數(shù)據(jù)
<?php// 1. 判斷是否是post提交// 2. 處理表單傳遞過來的數(shù)據(jù)(不能為空!empty;這里我就先不做處理了)// 3. 連接數(shù)據(jù)庫并插入一條數(shù)據(jù)// 4. 開始查詢(insert into)// 5. 判斷是否查詢成功// 6. 判斷是否插入成功`mysqli_affected_rows()`// 7. 重定向function add_user(){ $name = $_POST['name']; $age = $_POST['age']; $gender = $_POST['gender']; $link = mysqli_connect('127.0.0.1', 'root', '', 'students'); mysqli_query($link,"set names 'utf8';"); if(!link){ $GLOBALS['msg'] = '連接數(shù)據(jù)庫失敗'; return; } $query = mysqli_query($link,"INSERT INTO stu (name, gender, age) VALUES ('{$name}',{$gender},{$age});"); if (!$query) { $GLOBALS['msg'] = '查詢過程失敗'; return; } $affected = mysqli_affected_rows($link); if($affected!==1){ $GLOBALS['error_message'] = '添加數(shù)據(jù)失敗'; return; } header('Location:index.php');}if($_SERVER['REQUEST_METHOD']==='POST'){ add_user();}?>
- 界面
<p class="container add"> <h4 class="alert alert-primary text-center">添加學(xué)生信息</h4> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p class="form-group row"> <label for="name" class="col-sm-2 col-form-label">姓名</label> <p class="col-sm-10"> <input type="text" class="form-control" name="name" id="name"> </p> </p> <p class="form-group row"> <label for="gender" class="col-sm-2 col-form-label">性別</label> <p class="col-sm-10"> <input type="text" class="form-control" id="gender" name="gender"> </p> </p> <p class="form-group row"> <label for="age" class="col-sm-2 col-form-label">年齡</label> <p class="col-sm-10"> <input type="text" class="form-control" id="age" name="age"> </p> </p> <!--這里添加提示--> <?php if(!empty($GLOBALS['msg'])): ?> <p class="alert alert-warning" role="alert"> <?php echo $GLOBALS['msg']; ?> </p> <?php endif ?> <button type="submit" class="btn btn-primary btn-block">保存</button> </form></p>
- 點(diǎn)擊添加學(xué)生信息,跳轉(zhuǎn)到add.php
刪除一條數(shù)據(jù)(del.php)
- 我們已經(jīng)在主頁面已經(jīng)寫好了,并傳入了id
- 我們根據(jù)傳入的id使用sql語句進(jìn)行刪除即可
- 刪除完成重定向
<?php// 1. 接收傳遞過來的id if(empty($_GET['id'])){ exit('<h1>連接數(shù)據(jù)庫失敗</h1>'); } $id = $_GET['id'];// 2. 連接數(shù)據(jù)庫 $link = mysqli_connect('127.0.0.1', 'root', '', 'students'); mysqli_query($link,"set names 'utf8';");// 3. 刪除該條數(shù)據(jù) $query = mysqli_query($link,"delete from stu where id = {$id}");// 4. 查詢失敗的處理 if (!$query) { exit('<h1>查詢數(shù)據(jù)失敗</h1>'); }// 5. 受影響的行數(shù) $affected_rows = mysqli_affected_rows($link);// 6. 刪除失敗 if ($affected_rows <= 0) { exit('<h1>刪除失敗</h1>'); } header('Location: index.php');?>
修改操作
- 接收index.php傳過來的id,然后根據(jù)id查詢數(shù)據(jù)(id是唯一的)
- 將數(shù)據(jù)渲染到界面上
- 通過id鏈接數(shù)據(jù)庫查詢?cè)摋l數(shù)據(jù)
if(empty($_GET['id'])){ exit('<h1>必須傳入指定參數(shù)</h1>'); return; } $id = $_GET['id']; $link = mysqli_connect('127.0.0.1', 'root', '', 'students'); mysqli_query($link,"set names 'utf8';"); if(!$link){ exit('<h1>連接數(shù)據(jù)庫失敗</h1>'); } $query = mysqli_query($link,"select * from stu where id = {$id} limit 1"); if(!$query){ exit('<h1>查詢數(shù)據(jù)失敗</h1>'); } $user = mysqli_fetch_assoc($query); if(!$user){ exit('<h1>找不到你要編輯的數(shù)據(jù)</h1>'); }
- 界面數(shù)據(jù)渲染
<p class="container edit"> <h4 class="alert alert-primary text-center">添加學(xué)生信息</h4> <form method="post" action="edit_post.php"> <p class="form-group row"> <label for="name" class="col-sm-2 col-form-label">id</label> <p class="col-sm-10"> <input type="text" class="form-control" name="id" id="name" value="<?php echo $user['id']; ?>"> </p> </p> <p class="form-group row"> <label for="name" class="col-sm-2 col-form-label">姓名</label> <p class="col-sm-10"> <input type="text" class="form-control" name="name" id="name" value="<?php echo $user['name']; ?>"> </p> </p> <p class="form-group row"> <label for="gender" class="col-sm-2 col-form-label">性別</label> <p class="col-sm-10"> <input type="text" class="form-control" id="gender" name="gender" value="<?php echo $user['gender']; ?>"> </p> </p> <p class="form-group row"> <label for="age" class="col-sm-2 col-form-label">年齡</label> <p class="col-sm-10"> <input type="text" class="form-control" id="age" name="age" value="<?php echo $user['age']; ?>"> </p> </p> <button type="submit" class="btn btn-primary btn-block">保存</button> </form></p>
- 結(jié)果(生產(chǎn)環(huán)境中id是要隱藏的)
- post提交數(shù)據(jù),根據(jù)id修改數(shù)據(jù)
<?phpvar_dump($_POST);$id = $_POST["id"];$name = $_POST['name'];$age = $_POST['age'];$gender = $_POST['gender'];$link = mysqli_connect('127.0.0.1', 'root', '', 'students');mysqli_query($link,"set names 'utf8';");if(!$link){ exit('<h1>連接數(shù)據(jù)庫失敗</h1>');}//$query = mysqli_query($link,"update stu set name={$name},age={$age},gender={$gender} where id = {$id};");var_dump("UPDATE stu SET gender={$gender},age={$age},name='{$name}' WHERE id={$id}");$query = mysqli_query($link,"UPDATE stu SET gender={$gender},age={$age},name='{$name}' WHERE id={$id}");if (!$query) { exit('<h1>查詢數(shù)據(jù)失敗</h1>');}$affected = mysqli_affected_rows($link);if($affected!==1){ exit('<h1>找不到你要編輯的數(shù)據(jù)</h1>');}header('Location:index.php');?>
大家如果感興趣的話,可以點(diǎn)擊《PHP視頻教程》進(jìn)行