PHP從入門到能用(十一)PHP操作數據庫(小型學生成績管理系統demo實戰)

【10-1】在student數據庫下創建score數據表

score.sql

create table score(
	id int not null auto_increment,
	name char(255) not null,
	chinese int(4) not null,
	english int(4) not null,
	math int(4) not null,
	primary key(id)
);

在這裏插入圖片描述

【10-1】mysqli_connect()的用法

10-1.php

<?php
$link = mysqli_connect("localhost","root","","mydemo") or die("數據庫連接失敗".mysqli_connect_error());
echo "數據庫連接成功!";
mysqli_set_charset($link,"utf8");

在這裏插入圖片描述

【10-2】 mysqli_select_db()的用法

<?php
$link = mysqli_connect("localhost","root","","mydemo") or die("數據庫連接失敗".mysqli_connect_error());
echo "數據庫連接成功!";
$sql = mysqli_select_db($link,"test_student");
if($sql) echo"連接數據庫成功!";
else echo "選擇數據庫失敗!";
mysqli_set_chartset($link,"utf8");

在這裏插入圖片描述

【10-3】mysqli_query()的用法

<?php
include "10-1.php";
$sql = "select * from score";
$result = mysqli_query($link, $sql);
$n = mysqli_num_rows($result);
echo "查詢到{$n}條記錄";
$sql = "insert into score values(null,'張飛','100','100','100')";
echo $sql;
$result = mysqli_query($link, $sql);
if ($result) {
    echo "數據插入成功!";
} else {
    echo "數據插入失敗!";
}

在這裏插入圖片描述
在這裏插入圖片描述

【10-4】mysqli_fetch_object()應用

include("10-1.php");
$sql = "select * from score order by id desc";
$data = mysqli_query($link , $sql);
echo '<table border="1"><caption>學生成績瀏覽界面</caption><tr><td>姓名</td><td>語文</td><td>英文</td><td>數學</td></tr>';

while($output=mysqli_fetch_object($data)){
	echo "<tr>
	<td>{$output->id}</td>
	<td>{$output->name}</td>
	<td>{$output->chinese}</td>
	<td>{$output->english}</td>
	<td>{$output->math}</td>
	</tr>"
}

echo "</table>";

在這裏插入圖片描述

【10-4b】利用mysqli_fetch_assoc()函數實現上例效果

mysqli_fetch_assoc()的作用是從結果集中取得一行作爲關聯數組

<?php
include("10-1.php");
$sql = "select * from score order by id asc";
$data = mysqli_query($link , $sql);
echo '<table border="1"><caption>學生成績瀏覽界面</caption><tr><td>姓名</td><td>語文</td><td>英文</td><td>數學</td></tr>';

while($output=mysqli_fetch_assoc($data)){
	echo "<tr>
	<td>{$output['name']}</td>
	<td>{$output['chinese']}</td>
	<td>{$output['english']}</td>
	<td>{$output['math']}</td>
	</tr>";
};

echo "</table>";

// 釋放結果集
mysqli_free_result($data);
 
mysqli_close($link);

在這裏插入圖片描述

【10-4c】 利用mysqli_fetch_row()函數實現上例效果

<?php
include "10-1.php";
$sql = "select * from score order by id asc";
$data = mysqli_query($link, $sql);
echo '<table border="1"><caption>學生成績瀏覽界面</caption><tr><td>姓名</td><td>語文</td><td>英文</td><td>數學</td></tr>';

while ($output = mysqli_fetch_row($data)) {
    echo "<tr>
        <td>{$output[1]}</td>
        <td>{$output[2]}</td>
        <td>{$output[3]}</td>
        <td>{$output[4]}</td>
        </tr>";
}
;

echo "</table>";

// 釋放結果集
mysqli_free_result($data);

mysqli_close($link);

在這裏插入圖片描述

【10-4d】 利用mysqli_fetch_array()函數實現上例效果

<?php
include "10-1.php";
$sql = "select * from score order by id asc";
$data = mysqli_query($link, $sql);
echo '<table border="1"><caption>學生成績瀏覽界面</caption><tr><td>姓名</td><td>語文</td><td>英文</td><td>數學</td></tr>';

while ($output = mysqli_fetch_array($data)) {
    echo "<tr>
        <td>{$output[1]}</td>
        <td>{$output[2]}</td>
        <td>{$output[3]}</td>
        <td>{$output[4]}</td>
        </tr>";
}
;

echo "</table>";

// 釋放結果集
mysqli_free_result($data);

mysqli_close($link);

實戰作業

題目一

一、建立數據庫,創建一張表,此表至少包含 5 個字段。
要求:

(1) 數據庫名、表名、字段名自定義。
(2) 寫出SQL語句:創建數據庫、數據表。
(3) 用二維表表示,表中列出:字段名、數據類型、長度、是否主鍵、備註等

create.sql

create database student;
create table score(
	id int not null auto_increment comment '自增長的ID序號',
	username varchar(100) not null comment '學生姓名',
	chinese int(4) comment '中文',
	english int(4) comment '英文',
	math int(4) comment '數學',
	total int(4) comment '總分',
	primary key(id) comment '主鍵'
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

在這裏插入圖片描述

題目二

二、根據第一題中建立的數據庫、數據庫表,實現添加功能
具體要求:製作頁面參考如圖 1 所示,內容包括數據庫表中的每個字段。當點擊“提交”按鈕時,可以向數據庫中添加數據,如果添加數據失敗,能給出提示,如果添加成功,則跳轉到另一頁面 main.php,參考如圖 2 所示,本頁面可以把數據庫表中所有記錄顯示出來。
備註:內容必須與第一題中自己建立的數據庫表字段對應。

圖1>
在這裏插入圖片描述
圖2>
在這裏插入圖片描述

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form action="./main.php" method="get">
        <h3>成績統計</h3>
        <hr>
        用戶名: <input type="text" name="username" id="">
        <br>
        語文成績:<input type="number" name="chinese" id="">
        <br>
        英語成績:<input type="number" name="english" id="">
        <br>
        數學成績:<input type="number" name="math" id="">
        <br>
        <br>
        <input type="submit" value="提交">
    </form>
</body>

</html>

在這裏插入圖片描述

main.php

<?php
try {
    $username = $_GET['username'];
    $chinese = $_GET['chinese'];
    $english = $_GET['english'];
    $math = $_GET['math'];
} catch (Exception $e) {
    echo "<script>console.log('{$e}');</script>";
}

$link = mysqli_connect("localhost", "root", "", "student") or die("數據庫連接失敗" . mysqli_connect_error());
mysqli_set_charset($link, "utf8");

$sql = "insert into score(username,chinese,english,math) values('{$username}','{$chinese}','{$english}','{$math}')";
$result = mysqli_query($link, $sql);
if ($result) {
    echo "數據插入成功!";
} else {
    echo "數據插入失敗!";
}

$sql = "select * from score order by id asc";
$data = mysqli_query($link, $sql);
echo '<table border="1" style="margin:0 auto;text-align:center;"><th colspan="6">學生個人信息表</th><tr><td>序號</td><td>姓名</td><td>語文</td><td>英文</td><td>數學</td><td>操作</td></tr>';

while ($output = mysqli_fetch_row($data)) {
    echo "<tr>
        <td>{$output[0]}</td>
        <td>{$output[1]}</td>
        <td>{$output[2]}</td>
        <td>{$output[3]}</td>
        <td>{$output[4]}</td>
        <td><a href='modify.php?x={$output[0]}'>修改</a>/<a href='delete.php?x={$output[0]}'>刪除</a></td>
        </tr>";
}
;


在這裏插入圖片描述

題目三

三、根據第一題中建立的數據庫、數據庫表,實現修改功能。
具體要求: 當點擊 main.php 中的“修改”時,可將網頁連接到如圖 3 所示的修改頁面。信息修改後,保存到數據庫中,成功保存數據後,可將頁面轉至 main.php頁面。
備註: 內容必須與第一題中自己建立的數據庫表字段對應。

圖3>
在這裏插入圖片描述
在這裏插入圖片描述

modify.php

<?php
$link = mysqli_connect("localhost", "root", "", "student") or die("數據庫連接失敗" . mysqli_connect_error());
mysqli_set_charset($link, "utf8");

$sql = "select * from score where id={$_GET['x']}";
$data = mysqli_query($link, $sql);
if ($data) {
    while ($output = mysqli_fetch_row($data)) {
        echo "<div style='width:420px; margin:40px auto;'>
        <form action='./update.php' method='get'>
        <input type='hidden' name='x' value='{$output[0]}'>
        姓名: <input type='text' name='username' value='{$output[1]}'>";
        echo "<br/>語文:  <input type='number' name='chinese' value='{$output[2]}'>";
        echo "<br/>英語:  <input type='number' name='english' value='{$output[3]}'>";
        echo "<br/>數學:  <input type='number' name='math' value='{$output[4]}'>";
        echo "<br/><br/><input type='submit' value='修改'></div>";
    }
} else {
    echo "查詢失敗";
}
?>

update.php

<?php
$link = mysqli_connect("localhost", "root", "", "student") or die("數據庫連接失敗" . mysqli_connect_error());
mysqli_set_charset($link, "utf8");

$sql = "update score set username=\"{$_GET['username']}\", chinese={$_GET['chinese']}, english={$_GET['english']} , math={$_GET['math']} where id={$_GET['x']} ";
echo $sql;
$result = mysqli_query($link, $sql);
if ($result) {
    echo "<script>alert('數據修改成功!');history.go(-1);</script>";
} else {
    echo "<script>alert('數據修改失敗!');history.go(-1);</script>";
}

在這裏插入圖片描述

題目四

四、根據第一題中建立的數據庫、數據庫表,實現刪除功能。
具體要求: 當點擊main.php頁面中的“刪除”時,如果能成功刪除數據,則給出提示“數據刪除成功!”,如圖 8 所示,並將網頁轉至main.php頁面。數據刪除失敗也給出提示“數據刪除失敗!”。
備註: 內容必須與第一題中自己建立的數據庫表字段對應。

delete.php

<?php
$link = mysqli_connect("localhost", "root", "", "student") or die("數據庫連接失敗" . mysqli_connect_error());
mysqli_set_charset($link, "utf8");

$sql = "delete from score where id={$_GET['x']};";
$result = mysqli_query($link, $sql);
if ($result) {
    echo "<script>alert('數據刪除成功!');history.go(-1);</script>";
} else {
    echo "<>alert('數據刪除失敗!');history.go(-1);</script>";
}

在這裏插入圖片描述


人工智能課程

Google開發專家帶你學 AI:入門到實戰(Keras/Tensorflow)(附源碼)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章