PHP 与 HTML 混写
首先我们将《PHP丨前端网页是怎么跟后台进行数据交互的(实战)》中的 HTML 代码另存为名为 index.php 的文件;
从而实现 PHP 与 HTML 混写:
//预先定义数据库连接参数
$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;
}
?>
留言本.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;} /*留言内容宽度,内间距*/
用 foreach 循环对留言内容处理
//预先定义数据库连接参数
$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;
}
?>
留言本.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;} /*留言内容宽度,内间距*/
foreach ( $rows as $row ) {
?>
}
?>
关于上面 date 函数的语法大家可以自己去 PHP 手册中了解。
浏览器输出:
用 header 函数对跳转页面的处理
header() 函数用于发送原生的 HTTP 头。
关于 HTTP 头的更多信息请参考 PHP 手册。
其实当作完上面一步时已经可以发表留言了,但是,当点击 “发表留言” 后会跳转到另一个页面(也就是 save.php 页面),现在我们要处理的就是这个问题。
我们把《将网页中的留言写进数据库(实战)》中最后保存的 save.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");
?>
杀青!This is not the end.It is not even the beginning of the end.But it is,perhaps,the end of the beginning.
更多分享三小时学会 HTML 网页编辑
1万+




被折叠的 条评论
为什么被折叠?



