Home >Backend Development >PHP Tutorial >PHP implements simple message board function (source code attached)

PHP implements simple message board function (source code attached)

烟雨青岚
烟雨青岚forward
2020-06-09 09:48:2310924browse

PHP implements simple message board function (source code attached)

php implements simple message board function

1. Principle

Simply put, it means creating the database, adding data, and displaying it on the front end. My program is simply to leave a message and then display it.

First write the front-end page of the message, and simply write the author, title and content.

2. Interface :

PHP implements simple message board function (source code attached)

3. Interface to display messages :

PHP implements simple message board function (source code attached)

4. Code

(1) Page to add a message

<!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) Background processing of messages, storing the author, title, and content into the built database

<?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(&#39;y-m-d h:m:s&#39;);
$sql="insert into messageboard(id,title,author,message,dateline) values(&#39;$id&#39;,&#39;$title&#39;,&#39;$author&#39;,&#39;$message&#39;,&#39;$time&#39;)";
if($str=$con->query($sql)){
    echo "<script>alert(&#39;留言成功&#39;);window.location.href=&#39;show_message.php&#39;</script>";
}
else {
    echo "<script>alert(&#39;留言失败&#39;);window.location.href=&#39;messageboard.php&#39;</script>";
}
?>

(3) The following is the page code for displaying messages

<?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[&#39;title&#39;]; ?></td>
        </tr>
        <tr>
            <td>作者</td>
            <td><?php echo $value[&#39;author&#39;]; ?></td>
        </tr>
        <tr>
            <td>内容</td>
            <td><?php echo $value[&#39;message&#39;]; ?></td>
        </tr>
        <tr>
            <td><?php echo $value[&#39;dateline&#39;];;?></td>
        </tr>
    </table>
</div>
<?php
 }
}
?>
</Body>
</HTML>

5. Problems encountered

The data cannot be displayed on the display page at the beginning. After searching for a long time, it turned out that the wrong query was written in the sql. The method is written as:

select * from message where dateline desc;

You must use where to query it if there are conditions. For example:

select * from message where dateline=$date;

Because my program does not transfer data to this from the previous page, I can only use the following method to sort and list all the data by time.

select * from message order by dateline;

Thank you for reading. Please point out any shortcomings in the above code. I hope you can gain something.

This article is reproduced from: https://blog.csdn.net/jeak2015/article/details/53440522

Recommended tutorial: "PHP Tutorial"

The above is the detailed content of PHP implements simple message board function (source code attached). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete