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)(附源码)

猜你喜欢

转载自blog.csdn.net/qq_41136216/article/details/106717514