php留言板的简单编写

这篇博客介绍了作者初次编写PHP留言板的过程,包括留言板的工作原理、前端和后端代码实现,以及遇到并解决的问题。主要问题在于SQL查询条件的错误,最终通过按时间排序显示所有留言。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于php留言板简单的编写

小弟第一次写博客,也是第一次写关于程序的博客,写得不好的地方,请提出。

  • 原理
  • 代码
  • 我所遇到的问题

原理

简单的说就是 数据库的创建,添加数据,显示在前端上。我的程序只是简单的留言再显示。
首先写好留言的前端页面,就简单的写入作者,标题和内容。
界面:
这里写图片描述
显示留言的界面:
这里写图片描述

代码

添加留言的页面

<!DOCTYPE HTML>
    <HTML>
<Head>
    <meta  http-equiv="CONTENT-TYPE" ; content="text/html"  ; charset="UTF-8">
    <title>留言</title>
    <style type="text/css">
     .message{
         margin-top:0px;
     }
     h1{
         margin-top:200px;
     }
    </style>
</Head>
<Body>
   <h1 align="center">留言板</h1>
   <div class="message">
       <form name="addform" id="addform" method="post" action="message_handle.php">
           <table type="text" align="center" border="1px,solid">
               <input type="hidden" id="id" name="id" />
            <tr>
               <td>标题</td>
               <td><input type="text" name="title" id="title"/></td>
            </tr>
            <tr>
                <td>作者</td>
                <td><input type="text" name="author" id="author"/> </td>
            </tr>
            <tr>
                <td>内容</td>
                <td><textarea name="message" id="message" cols="60" role="15"></textarea></td>
            </tr>
            <tr>
                <td><input type="submit" name="sumbit"/></td>
                <td><input type="reset" name="reset"/></td>
            </tr>
           </table>
       </form>
   </div>
</Body>
</HTML>

留言的后台处理,把作者,标题,内容存入建好的数据库中

<?php

header("CONTENT-TYPE:text/html;charset=UTF-8");
define("HOST","127.0.0.1");
define("USERNAME","root");
define("PASSWORD","");
if($con=new mysqli(HOST,USERNAME,PASSWORD,"test")){
    echo $con->error;
}
if($con->select_db("messageboard")){
    echo $con->error;
}
if($con->query("SET NAMES utf8")){
    echo $con->error;
}

$id=$_POST["id"];
$title=$_POST["title"];
$author=$_POST["author"];
$message=$_POST["message"];
$time=date('y-m-d h:m:s');

$sql="insert into messageboard(id,title,author,message,dateline) values('$id','$title','$author','$message','$time')";

if($str=$con->query($sql)){
    echo "<script>alert('留言成功');window.location.href='show_message.php'</script>";
}
else {
    echo "<script>alert('留言失败');window.location.href='messageboard.php'</script>";
}
?>

下面是显示留言的页面代码

<?php
header("CONTENT-TYPE:text/html;charset=UTF-8");
define("HOST","127.0.0.1");
define("USERNAME","root");
define("PASSWORD","");
if($con=new mysqli(HOST,USERNAME,PASSWORD,"test")){
    echo $con->error;
}
if($con->select_db("messageboard")){
    echo $con->error;
}
if($con->query("SET NAMES utf8")){
    echo $con->error;
}
$sql="select * from messageboard ORDER BY dateline DESC ";

$str=$con->query($sql);
if($str && mysqli_num_rows($str)){
    while($row= mysqli_fetch_assoc($str)){
        $data[]=$row;
    }
}

?>
<!DOCTYPE HTML>
<HTML>
<Head>
    <meta  http-equiv="CONTENT-TYPE" ; content="text/html"  ; charset="UTF-8">
    <title>留言板</title>
    <style type="text/css">

    </style>
</Head>
<Body>
<div>
    <?php
    if(empty($data)){
        echo "当前没有留言";
    }
    else{
    foreach($data as $value) {
    ?>
    <table cellpadding="2" cellspacing="8" align="center" border="1px,solid">

        <tr>
            <td>标题</td>
            <td><?php echo $value['title']; ?></td>
        </tr>
        <tr>
            <td>作者</td>
            <td><?php echo $value['author']; ?></td>
        </tr>
        <tr>
            <td>内容</td>
            <td><?php echo $value['message']; ?></td>
        </tr>
        <tr>
            <td><?php echo $value['dateline'];;?></td>
        </tr>
    </table>
</div>
<?php
 }
}
?>
</Body>
</HTML>

所遇到的问题

刚开始显示页面上不能显示数据,找了半天原因,结果是因为在sql中写错了查询方式写成了:

select * from message where dateline desc;

用wher得有条件,得有例如:

select * from message where dateline=$date;

才能查询到。因为我的程序没有从前个页面传递数据到这,所以只能用下面这种

select * from message order by dateline;

通过时间来排序罗列出所有数据。

以上就是我写的留言板的php代码,有不足的地方请多多指教。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值