Home >Backend Development >PHP Tutorial >Message board for newcomers

Message board for newcomers

WBOY
WBOYOriginal
2016-07-25 08:49:24958browse
A message board for newcomers to get in touch with "add, delete, modify, and query" in MySQL more quickly. Please give me some advice. There are still many shortcomings. I hope you can learn more during the improvement process and share it together.
The table name of the database is weibo, which contains id (remember to select auto_increment in phpmyadmin for automatic growth), nickname (alias varchar), dates (dateTime), content (content, text), hits (int)
  1. include("conn.php");
  2. mysql_set_charset('utf8');
  3. if (!empty($_POST['sub'])) {
  4. $name = $_POST ['nickname']; //The obtained fields are converted into variables
  5. $content = $_POST['content'];
  6. $sql = "INSERT INTO `weibo` (`nickname`,`dates`,`content`) VALUES ('$name',now(),'$content')"; //Database statements correspond to nickname, dates (display publication time) in the database, content
  7. //Execute the mysql statement, if the execution is successful, Return message notification.
  8. if (mysql_query($sql)) {
  9. echo("Insertion successful");
  10. }else {//Same as failure
  11. echo("Insertion failed").mysql_error();
  12. }
  13. }
  14. ?>
  15. Message page</title> ;<li> </head></li> <li> <body></li> <li> <table></li> <li> <form method="post" action="add.php"></li> <li> <tr></li> <li> <td>Your nickname:< ;/td></li> <li> <td><input type="text" name="nickname" value="" /></td></li> <li> </tr></li> <li> <tr></li> <li> <td> Talk about:</td></li> <li> <td><textarea name="content" rows="5" cols="50"></textarea></td></li> <li> </tr></li> <li> <tr></li> <li> <td align="center"><input type="submit" name="sub" value="Submit" /></li> <li> </td></li> <li> </tr></li> <li> < ;/form></li> <li> </table></li> <li> </li> <li> </body></li> <li> </html></li> </ol></div> <em onclick="copycode($('code_qTN'));">Copy code</em> </div> <div class="blockcode"> <div id="code_IOr"><ol><li><?php <li>//Set the administrator, password, and connected data table to connect to the database<li> $local = 'localhost';<li> $dbname = 'root';<li> $passwd = '3363064';<li> $db = 'weibo';<li> <li><li> mysql_connect($local,$dbname,$passwd) or die("falie");<li> mysql_select_db($db);<li><li>//Test the connection. If the database connection is successful, output yes , fail fail<li>// if ($query) {<li>// echo("yes");<li>// }else {<li>// echo "faile";<li>// }<li>?></li></ol></div> <em onclick="copycode($('code_IOr'));">Copy code</em> </div> <div class="blockcode"> <div id="code_jvD"><ol><li><?php <li> //Delete page<li> include("conn.php");<li> //Delete database words<li> if (!empty($_GET['del'])) { //Use get Get the fields of del<li> $del = $_GET['del']; //Convert the obtained fields into variables<li> $sql = "delete from `weibo` where id='$del'"; //Delete mysql statement , delete from table name, the condition is that id is equal to 'del' obtained from index.php<li> $query=mysql_query($sql);<li> if ($query) {//Determine whether the mysql statement is executed successfully, and prompt. <li> echo("Deletion successful");<li> header("refresh:5;url='index.php'");<li> }else {<li> echo("Failed...").mysql_error();<li> }<li> <li> }<li><li> ?></li></ol></div> <em onclick="copycode($('code_jvD'));">Copy code</em> </div> <div class="blockcode"> <div id="code_sch"><ol> <li><?php <li><li> include("conn.php");<li> if (!empty($_GET['id'])) { //If the obtained id is not empty, start executing the following statement <li> $sql = "select *from weibo where id = '".$_GET['id']."'";<li> $query = mysql_query($sql);<li> $rs = mysql_fetch_array($query);<li> }<li> <li> if (!empty($_POST['sub'])) { //If the sub obtained from the form is not empty, execute the following statement<li> <li> $name = $_POST['nickname']; //The obtained fields Convert to variables<li> $content = $_POST['content'];<li> $hid = $_POST['hid']; <li> $mysql = "UPDATE `WEIBO` SET `nickname`='$name',`content`= '$content' where id='$hid'";<li> <li> if (mysql_query($mysql)) {<li> echo("insert successfully");<li> header("refresh:3;url=index.php");<li> }else {<li> echo("Insertion failed").mysql_error();<li> }<li> }<li> <li> ?></li> <li><html></li> <li><head></li> <li> <meta http-equiv="content-type" content= "text/html" charset="utf8" /></li> <li> <title>Message page
  16. < tr>
  17. < ;td align="center">
  18. < ;/table>
  19. Copy code
    1. Message content

    2. include("conn.php");
    3. mysql_set_charset("utf8");
    4. $sql = "select * from `weibo` ";
    5. $query = mysql_query($sql);
    6. while ($rs = mysql_fetch_array($query)) {
    7. ?>

    Your nickname:
    Talk about:
  20. Username: < a href="del.php?del=">delete   Edit
    Date:< ;/td>
  21. Content:
  22. }
  23. ?>
Copy code
  1. include("conn.php"); //Connect to the database
  2. if (!empty($_GET['id'])) { //Get the id in the index, use get
  3. $sql = "select *from weibo where id = '".$_GET['id']."'"; //Execute the database statement, select the database as weibo, and the condition is that the id is equal to the id obtained from index.php
  4. $query = mysql_query($sql);
  5. $rs = mysql_fetch_array($query); //Read the table in the database in array form.
  6. //The following is a database statement for clicks. Hits are added to the table, and hits are equal to one visit per visit + 1.
  7. $sqlhits = "update weibo set hits=hits+1 where id = '".$_GET[' id']."'";
  8. mysql_query($sqlhits);
  9. }
  10. ?>
  11. Username:

  12. Published time:

  13. Clicks:


  14. Content :
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn