PHP 与 HTML 混写
首先我们将《PHP丨前端网页是怎么跟后台进行数据交互的(实战)》中的 HTML 代码另存为名为 index.php 的文件;
再将我们之前在《MySQL丨PHP 获取查询结果》中的代码也加入进去;
从而实现 PHP 与 HTML 混写:
<?php
//预先定义数据库连接参数
$host = '127.0.0.1';
$user = 'root';
$pwd = 'root';
$dbname = 'php';
//连接到数据库
$db = new mysqli($host, $user, $pwd, $dbname);
//检查连接是否成功
if ( $db->connect_errno <> 0 ) {
echo "链接失败";
echo $db->connect_error;
}
//编写SQL
$sql = "SELECT * FROM msg ORDER BY id DESC";
//执行SQL
$is = $db->query( $sql ); //获取返回值
//对返回值进行判断
if ( $is === false) {
echo "SQL错误";
exit;
}
//得到mysqli_result对象后调用fetch_array()方法
$rows = [];
while ( $row = $is->fetch_array( MYSQLI_ASSOC )) {
$rows[] = $row;
}
?>
<!DOCTYPE html>
<html lang="zh-cmn">
<head>
<meta charset="UTF-8">
<title>留言本</title>
<style>
.wrap{width: 600px;margin: 0px auto;}
.add{overflow: hidden;}
.add .content{width: 598px;margin: 0;padding: 0;}
.add .user{float: left;}
.add .btn{float: right;}
.msg{margin: 20px 0px;background: #ccc;padding: 5px;} /*留言内容外间距,背景色,内间距*/
.msg .info{overflow: hidden;} /*清除浮动所带来的影响*/
.msg .user{float: left;color: red;} /*用户名浮动,字体颜色*/
.msg .time{float: right;color: blue;} /*时间浮动,字体颜色*/
.msg .contet{width: 100%;padding: :5px;} /*留言内容宽度,内间距*/
</style>
</head>
<body>
<div class="wrap">
<!-- 发表留言区 -->
<div class="add">
<form action="save.php" method="post">
<textarea name="content" class="content" cols="50" rows="5"></textarea>
<br/>
<input name="user" class="user" type="text"/>
<input class="btn" type="submit" value="发表留言"/>
</form>
</div>
</div>
</body>
</html>
用 foreach 循环对留言内容处理
用 foreach 循环(参考《PHP丨流程控制》)对留言内容(参考《三小时学会网页设计——使用 HTML 编辑网页》、《三小时学会网页设计——使用 CSS 美化网页》)进行处理:
<?php
//预先定义数据库连接参数
$host = '127.0.0.1';
$user = 'root';
$pwd = 'root';
$dbname = 'php';
//连接到数据库
$db = new mysqli($host, $user, $pwd, $dbname);
//检查连接是否成功
if ( $db->connect_errno <> 0 ) {
echo "链接失败";
echo $db->connect_error;
}
//编写SQL
$sql = "SELECT * FROM msg ORDER BY id DESC";
//执行SQL
$is = $db->query( $sql ); //获取返回值
//对返回值进行判断
if ( $is === false) {
echo "SQL错误";
exit;
}
//得到mysqli_result对象后调用fetch_array()方法
$rows = [];
while ( $row = $is->fetch_array( MYSQLI_ASSOC )) {
$rows[] = $row;
}
?>
<!DOCTYPE html>
<html lang="zh-cmn">
<head>
<meta charset="UTF-8">
<title>留言本</title>
<style>
.wrap{width: 600px;margin: 0px auto;}
.add{overflow: hidden;}
.add .content{width: 598px;margin: 0;padding: 0;}
.add .user{float: left;}
.add .btn{float: right;}
.msg{margin: 20px 0px;background: #ccc;padding: 5px;} /*留言内容外间距,背景色,内间距*/
.msg .info{overflow: hidden;} /*清除浮动所带来的影响*/
.msg .user{float: left;color: red;} /*用户名浮动,字体颜色*/
.msg .time{float: right;color: blue;} /*时间浮动,字体颜色*/
.msg .contet{width: 100%;padding: :5px;} /*留言内容宽度,内间距*/
</style>
</head>
<body>
<div class="wrap">
<!-- 发表留言区 -->
<div class="add">
<textarea class="content" cols="50" rows="5"></textarea>
<br/>
<input class="user" type="text"/>
<input class="btn" type="submit" value="发表留言"/>
</div>
<?php
foreach ( $rows as $row ) {
?>
<!-- 查看留言内容区 -->
<div class="msg">
<div class="info">
<span class="user"><?php echo $row['user'];?></span>
<span class="time"><?php echo date( "Y-m-d H:i:s", $row['intime'] );?></span>
</div>
<div class="content"><?php echo $row['content];?>
</div>
</div>
<?php
}
?>
</div>
</body>
</html>
关于上面 date 函数的语法大家可以自己去 PHP 手册中了解。
浏览器输出:
用 header 函数对跳转页面的处理
header() 函数用于发送原生的 HTTP 头。
关于 HTTP 头的更多信息请参考 PHP 手册。
其实当作完上面一步时已经可以发表留言了,但是,当点击 “发表留言” 后会跳转到另一个页面(也就是 save.php 页面),现在我们要处理的就是这个问题。
我们把《将网页中的留言写进数据库(实战)》中最后保存的 save.php 文件代码拿过来:
<?php
header("Content-type: text/html; charset=utf-8"); //如果浏览器输出乱码就加上这一句
//加载类
include('input.php');
//获得数据
$content = $_POST['content'];
$user = $_POST['user'];
//实例化类
$input = new input();
//调用函数,检查留言内容
$is = $input->post( $content );
if ( $is == false ) {
die('留言内容不能为空');
}
//调用函数,检查留言人
$is = $input->post( $user );
if ( $is == false ) {
die('用户名输入不正确');
}
//将数据入库
//编写SQL
$time = time(); //这个time是PHP内置函数
$sql = "insert into msg (content, user, intime) values ('{$content}' , '{$user}' , '{$time}')";
//执行SQL
$is = $db->query($sql);
//发送原生 HTTP 头
header("location: index.php");
?>
杀青!
更多分享
三小时学会 HTML 网页编辑
三小时学会 CSS 网页美化
PHP 网站开发
数据库基础
效率工具
小众电影
小程序开发
联系客服