日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
十分鐘利用PHP+Mysql實(shí)現(xiàn)增刪改查(實(shí)例詳解)

本篇文章給大家?guī)砹岁P(guān)于php怎樣利用mysql實(shí)現(xiàn)增刪改查功能的實(shí)例,希望對大家有幫助。

創(chuàng)新互聯(lián)于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元翔安做網(wǎng)站,已為上家服務(wù),為翔安各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575

PHP+Mysql實(shí)現(xiàn)增刪改查

打開我們的wampserver服務(wù)器+Mysql可視化工具(這里我用Navicat),或則其它集成工具(Apache+PHP+Mysql)都可以。鏈接上我們的服務(wù)器


我們新建查詢來操作數(shù)據(jù)庫,先把基本的文件配置好

插入一條信息

INSERT INTO 語法

  1. 需指定要插入數(shù)據(jù)的列名,只需提供被插入的值即可
INSERT INTO table_name VALUES (value1,value2,value3,...);
  1. 需要指定列名及被插入的值
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('

連接數(shù)據(jù)庫失敗

'); } // 每次只能查詢一條數(shù)據(jù) $query = mysqli_query($connection, 'select * from stu;'); // 查詢所有的數(shù)據(jù) while ($row = mysqli_fetch_assoc($query)) { var_dump($row); }
查詢數(shù)據(jù)庫渲染主頁面(index.php)
  1. 采用混編的方法,在頭部鏈接數(shù)據(jù)庫
connect_error) {
    die("連接失敗: " . $link->connect_error);
}
// 4.查詢數(shù)據(jù)
$query = mysqli_query($link, 'select * from stu;');
// 5.渲染數(shù)據(jù)
?>
  1. 引入bootstrap@4(bootstrap官網(wǎng)下載并引入bootstrap.css)
  1. 使用mysqli_fetch_assoc($query)渲染數(shù)據(jù),因?yàn)楹罄m(xù)需要添加(add.php),刪除(del.php),修改(edit)操作所以這里先添加

首頁

學(xué)號 姓名 性別 年齡 操作
刪除 修改
添加學(xué)生信息

添加一條數(shù)據(jù)(add.php)
  1. 我們依舊使用混編的模式,表單數(shù)據(jù)提交到本頁面,使用$_SERVER['PHP_SELF']使得代碼魯棒性更強(qiáng)
  2. 使用post提交數(shù)據(jù),記得在頁面提示信息錯(cuò)誤
  3. 在頭部鏈接數(shù)據(jù)庫,插入一條數(shù)據(jù)
  1. 界面

添加學(xué)生信息

  1. 點(diǎn)擊添加學(xué)生信息,跳轉(zhuǎn)到add.php
刪除一條數(shù)據(jù)(del.php)
  1. 我們已經(jīng)在主頁面已經(jīng)寫好了,并傳入了id
  2. 我們根據(jù)傳入的id使用sql語句進(jìn)行刪除即可
  3. 刪除完成重定向
連接數(shù)據(jù)庫失敗');
    }
    $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('

查詢數(shù)據(jù)失敗

'); }// 5. 受影響的行數(shù) $affected_rows = mysqli_affected_rows($link);// 6. 刪除失敗 if ($affected_rows <= 0) { exit('

刪除失敗

'); } header('Location: index.php');?>
修改操作
  1. 接收index.php傳過來的id,然后根據(jù)id查詢數(shù)據(jù)(id是唯一的)
  2. 將數(shù)據(jù)渲染到界面上
  3. 通過id鏈接數(shù)據(jù)庫查詢該條數(shù)據(jù)
    if(empty($_GET['id'])){
        exit('

必須傳入指定參數(shù)

'); return; } $id = $_GET['id']; $link = mysqli_connect('127.0.0.1', 'root', '', 'students'); mysqli_query($link,"set names 'utf8';"); if(!$link){ exit('

連接數(shù)據(jù)庫失敗

'); } $query = mysqli_query($link,"select * from stu where id = {$id} limit 1"); if(!$query){ exit('

查詢數(shù)據(jù)失敗

'); } $user = mysqli_fetch_assoc($query); if(!$user){ exit('

找不到你要編輯的數(shù)據(jù)

'); }
  1. 界面數(shù)據(jù)渲染

添加學(xué)生信息

  1. 結(jié)果(生產(chǎn)環(huán)境中id是要隱藏的)
  2. post提交數(shù)據(jù),根據(jù)id修改數(shù)據(jù)
連接數(shù)據(jù)庫失敗');
}
//$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('

查詢數(shù)據(jù)失敗

'); } $affected = mysqli_affected_rows($link); if($affected!==1){ exit('

找不到你要編輯的數(shù)據(jù)

'); } header('Location:index.php'); ?>

新聞名稱:十分鐘利用PHP+Mysql實(shí)現(xiàn)增刪改查(實(shí)例詳解)
新聞來源:http://m.5511xx.com/article/cccdejh.html