PHP Introduction to the Message Board program implementation code

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags beginner can directly code code follows content create data delete

The message board program needs to be implemented by PHP + database This tutorial is mainly about the PHP MySQL message system implementation process, including the addition of modified deletion and editing work, is a beginner to learn from PHP good data.

Message board SQL files can be imported directly to MySQL

The code is as follows
Create Database form; Use form; CREATE TABLE ' message ' (' ID ' tinyint (1) is not NULL auto_increment, ' user ' varchar is not null, ' title ' varchar is not NULL, ' Content ' Tinytext not null, ' lastdate ' date not NULL, PRIMARY KEY (' id ')) engine=innodb DEFAULT CHARSET=GBK auto_increment=1 ;


conn.php Database Connection File

The code is as follows
<?php
$conn = @ mysql_connect ("localhost", "root", "") or Die ("Database link error");
mysql_select_db ("form", $conn);
mysql_query ("Set names ' GBK '");
?>


Add.php saved to the database based on the message submitted by the user

  code is as follows  
<?php
include ' conn.php ';
if ($_post[' submit ']) {
$sql = INSERT into message (id,user,title,content,lastdate) VALUES (NULL, ' $_post[user] ' , ' $_post[title] ', ' $_post[content ', now ());
mysql_query ($sql);
//Page jump, implemented as JavaScript
$url = "list.php";
echo "<script language= ' javascript ' type= ' text/javascript ' >";
echo "window.location.href= ' $url '";
Echo </script> 12}

<script type= "Text/javascript"
function Checkpost () {
if (addform.user.value== "") {
Alert ("Please enter username");
AddForm.user.focus ();
return false;
 }
if (addform.title.value.length<5) {
 alert ("title cannot be less than 5 characters");
AddForm.title.focus ();
return false;
 }
}
 </script>
<form name= "AddForm method=" POST "action=" add.php "onsubmit=" return checkpost ();" >
  User: <input type= "text" name= "user"/><br/>
Title: <input type= "Text" NAME= "title"/><br/>
Content: <textarea name= "Content" rows= "8" cols= "></textarea><br/>
<input type= "Submit" name= "Submit" value= "Add"/></FORM>
 

List.php output messages as a list

  Code follows  
<?php
include ' conn.php ';

<?php
Echo <div align= ' center ' > <a href= ' add.php ' > continue to add </a></div>;

 <table width=500 border= "0" align= "center" cellpadding= "5" cellspacing= "1" bgcolor= "#add3ef"
 <?php
$sql = "SELECT * from Message order by ID";
  $query =mysql_query ($sql);
  while ($row =mysql_fetch_array ($query)) {
 ?>
 
   <tr bgcolor= "#eff3ff"
   <td> title: <font color= "Red" ><?= $row [title]?></font> Users: <font color= "Red" > <?= $row [user]? ></font><div align= "right" ><a href= "preedit.php?id=<?= $row [id]?>" > Edit </a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href= "delete.php?id=<?= $row [id]?>" > Delete </a ></div></td>
  </TR>
   <tr bgcolor= "#ffffff"
   <td> contents: <?= $row [content]?></td>
   </tr>
   <tr bgcolor= "#ffffff"
   <td ><div align= "Right" > Published date: <?= $row [Lastdate]?></div></td>
   </tr>
   <?php}?>
 </table>
 

delete.php Delete the message, according to the user submitted data, we get the message content ID to delete operations

The code is as follows
<?php include ' conn.php ';
$id = $_get[' id '];
$query = "Delete from message where id=". $id;
mysql_query ($query);?>
<?php//Page jump, the way to achieve javascript $url = "list.php";
echo "<script language= ' javascript ' type= ' text/javascript ' >"; echo "window.location.href= ' $url '"; echo "</script>";?>

preedit.php editors use Update to update user resubmitted data and replace previous records with IDs uniquely identified


  Code follows  
<?php
 include ' conn.php ';
  $id =$_get[id];
$query = "SELECT * FROM Message WHERE id = ". $id;
  $result =mysql_query ($query);
  while ($rs =mysql_fetch_array ($result)) {
 
 <form method= POST action= postedit.php ""
     <input type= "hidden" name= "id" value= "<?= $rs [id]?>"
     User: <input type= "text" name= "user" value= "<?= $rs [user]?>"/><br/>
     title: <input type= "text" name= "title" value= "<?= $rs [title]?>"/><br/>
     content: <textarea name= "Content" rows= "8" cols= "><?= $rs [Content]?></textarea ><BR/>
     <input type= "Submit" name= "Submit" value= "edit"/>
 </ Form>
 <?php}?>
postedit.php

<?php
Include ' conn.php ';
$query = "Update message set user= ' $_post[user]", title= ' $_post[title] ', content= ' $_post[content] ' where id= ' $_post[id ' '";
mysql_query ($query);
?>
<?php
Page jumps, implemented as JavaScript
$url = "list.php";
echo "<script language= ' javascript ' type= ' text/javascript ' >";
echo "window.location.href= ' $url '";
echo "</script>";
?>

Summary

This is a complete message board system, as long as the above prompts to save the file can be very good to realize the message function, the main message board has read the data, pagination and data deletion, editing, we are divided into SQL Delete,update,insert These three kinds of statements, relatively basic knowledge.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.