


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 :
3. Interface to display messages :
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 id="留言板">留言板</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('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) 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['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>
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!

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
