php实现简单的留言板功能(附源码)-php教程

资源魔 40 0

php完成简略的留言板性能

一、原理

简略的说就是 数据库的创立,增加数据,显示正在前端上。我的顺序只是简略的留言再显示。

起首写好留言的前端页面,就简略的写入作者,题目以及内容。

二、界面

捕获.PNG

三、显示留言的界面

20.PNG

四、代码

(1)增加留言的页面

<!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>

(2)留言的后盾解决,把作者,题目,内容存入建好的数据库中

<?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>";
}
?>

(3)上面是显示留言的页面代码

<?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;

用where患上有前提,能力查问到。患上有例如:

select * from message where dateline=$date;

由于我的顺序不畴前个页面通报数据到这,以是只能用上面这类经过工夫来排序列举出一切数据。

select * from message order by dateline;

感激各人的浏览,以上代码有有余之处请各人指出,心愿各人能够有所播种。

本文转载自:https://blog.csdn.net/jeak2015/article/details/53440522

保举教程:《PHP教程》

以上就是php完成简略的留言板性能(附源码)的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学 留言板

抱歉,评论功能暂时关闭!