PHP+MySQL 制作留言板

表单 index.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>留言板</title>
</head>
<body>
<span>留言板</span>
<div>
<form action="liuyan.php" method="post">
<table>
    <tr>
    <td><input type="text" name="user" placeholder="网名"/></td>
    </tr>
    <tr>
    <td><textarea name="contern" cols="50" rows="5" placeholder="留言内容"></textarea></td>
    </tr>
<input type="submit" value="发表留言"/>
</form>
</div>
<div>
<?php
$cc=mysqli_connect("localhost","root","");  //连接MySQL数据库
$db=mysqli_select_db($cc,"test");   //选择数据库
$sql="select * from liuyan";  //查看liuyan表所有的数据
$re=mysqli_query($cc,$sql);  //执行SQL语句
echo "<table width='800'>";
echo "<tr><th>留言用户</th><th>留言内容</th><th>留言时间</th></tr>";
while($r=mysqli_fetch_array($re,MYSQLI_BOTH)){  //循环输出数据
echo "<tr><td>".$r['name']."</td><td>".$r['content']."</td><td>".$r['time']."</td></tr>";
}
echo "</table>";
?>
</div>
</body>
</html>

MySQL 数据库表内容

 PHP处理页面 liuyan.php

<?php
date_default_timezone_set("Asia/Shanghai");  //设置时区:东八区
$c=$_POST["contern"]; //获取表单提交过来的contern的值
if(empty($c)){  //如果$c为空
echo "<script type='text/javascript'>alert('留言内容不能为空');history.back();</script>";  //弹窗
}else{
    $u=$_POST["user"];  //获取表单提交过来的user的值
if(empty($u)){   //如果$u为空
echo "<script type='text/javascript'>alert('留言用户不能为空');history.back();</script>";  //弹窗
}else{
$t=date("Y-m-d,H:m:s");  //获取时间
$c=trim($c);  //去掉两端的空格
$c=htmlspecialchars($c);  //把html标签转换为实体
$c=addslashes($c);  //转义
$cc=mysqli_connect("localhost","root","");  //连接MySQL数据库
$db=mysqli_select_db($cc,"test");  //选择test数据库
$sql="insert into liuyan (name,content,time) values ('$u','$c','$t')";
$r=mysqli_query($cc,$sql); //执行SQL语句
if($r){  //如果SQL语句执行成功
echo "<script type='text/javascript'>alert('留言成功!');history.back();</script>";  //弹窗
}else{
echo "<script type='text/javascript'>alert('留言失败');history.back();</script>";  //弹窗
}
mysqli_close($cc); //关闭数据库的连接
}
}
?>

注意!两份php页面都要修改数据库连接信息!

 

 

 

  • 8
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值