Home  >  Article  >  Backend Development  >  PHP Example 5: PHP+MYSQL Message Board

PHP Example 5: PHP+MYSQL Message Board

WBOY
WBOYOriginal
2016-08-08 09:31:241176browse


The database message form is:


Post the file liuyan.php

<form action = "?do=OK" method = "post">
 <table width = "350" border = "0" cellpadding = "4">
      <tr>
             <td  align = "right">标题:</td>
             <td><input type = "text" name = "title"></td>
     </tr>
      <tr>
             <td  align = "right">留言者:</td>
             <td><input type = "text" name = "author"></td>
     </tr>
     <tr>
             <td align = "right" valign = "top">留言内容:</td>
             <td><textarea name = "content" rows = "5" cols = "30"></textarea></td>
     </tr>
      <tr>
              
	     <td colspan = "2" align = "center"><input type = "submit" value = "提交">
                                   <input type = "reset" value = "重置"></td>
     </tr></table>
</form>
<?php
   @$puanduan =$_GET[&#39;do&#39;];
  if($puanduan == &#39;OK&#39;)
  {
    $link = mysql_connect(&#39;localhost:3308&#39;,&#39;root&#39;,&#39;root&#39;);

  if(!$link)
  {
     die(&#39;连接失败:&#39;.mysql_error());
  }
  //为后续的mysql扩展函数的操作选定一个默认的数据库,它相当于sql命令 use se
  mysql_select_db(&#39;se&#39;,$link) or die(&#39;不能选定数据库SE:&#39;.mysql_error());
   $insert = "insert into liuyan(liuyan_title,liuyan_name,liuyan_content) values
  (&#39;$_POST[title]&#39;,&#39;$_POST[author]&#39;,&#39;$_POST[content]&#39;)";
  //使用mysql_query()函数发送insert语句,成功返回true。
  $result = mysql_query($insert);
  /*if ($result&&mysql_affected_rows()>0)
  {
    echo "数据记录插入成功,最后一条插入的数据ID为:".mysql_insert_id()."<br>";
  }
  else
  {
    echo "数据记录插入失败,错误号:".mysql_errno().",错误原因:".mysql_error()."<br>";
  }*/
  mysql_close($link);
  }
?>

View the file show_liuyan.php

<?php     
  
            mysql_connect("localhost:3308","root","root");  
            mysql_select_db("se");  
            mysql_query("set names &#39;gbk&#39;");  ?>
          <table width=400 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef" style = "margin-top:20px;"> 
<?php 
$sql="select * from liuyan order by id"; 
$query=mysql_query($sql); 
while ($row=mysql_fetch_array($query)){ 
?> 

<tr bgcolor="#eff3ff"> 
<td>标题:<font color="red"><?=$row[&#39;liuyan_title&#39;]?></font> 用户:<font color="red"><?=$row[&#39;liuyan_name&#39;] ?></font><div align="right">
<a href="pre_edit_liuyan.php?id=<?=$row[&#39;id&#39;]?>">编辑</a>  |  <a href="delete_liuyan.php?id=<?=$row[&#39;id&#39;]?>">删除</a></div></td> 
</tr> 
<tr bgColor="#ffffff"> 
<td>内容:<?=$row[&#39;liuyan_content&#39;]?></td> 
</tr> 
<tr bgColor="#ffffff"> 
<td><div align="right">发表日期:<?php echo date("Y/m/d");
?></div></td> 
</tr> 
<?php }?> 
</table> 

Edit the file pre_edit_liuyan.php

<?php 
 mysql_connect("localhost:3308","root","root");  
            mysql_select_db("se");  
            mysql_query("set names &#39;gbk&#39;");  
 $id=$_GET[&#39;id&#39;]; 
$query="SELECT * FROM liuyan WHERE id =".$id; 
$result=mysql_query($query); 
while ($rs=mysql_fetch_array($result)){ 
?> 
<FORM METHOD="POST" ACTION="post_edit_liuyan.php"> 
<input type="hidden" name="id" value="<?=$rs[&#39;id&#39;]?>"> 
用户:<INPUT TYPE="text" NAME="liuyan_name" value="<?=$rs[&#39;liuyan_name&#39;]?>"/><br /> 
标题:<INPUT TYPE="text" NAME="liuyan_title" value="<?=$rs[&#39;liuyan_title&#39;]?>"/><br /> 
内容:<TEXTAREA NAME="liuyan_content" ROWS="8" COLS="30"><?=$rs[&#39;liuyan_content&#39;]?></TEXTAREA><br />
<INPUT TYPE="submit" name="submit" value="edit"/> 
</FORM> 
<?php }?> 

Handle editing The file of the message post_edit_liuyan.php

<?php 
  mysql_connect("localhost:3308","root","root");  
            mysql_select_db("se");  
            mysql_query("set names &#39;gbk&#39;");  
$query="update liuyan set liuyan_name=&#39;$_POST[liuyan_name]&#39;,liuyan_title=&#39;$_POST[liuyan_title]&#39;,liuyan_content=&#39;$_POST[liuyan_content]&#39; where 
id=&#39;$_POST[id]&#39;"; 
mysql_query($query); 
?> 
<?php 
//页面跳转,实现方式为javascript 
$url = "show_liuyan.php"; 
echo "<script language=&#39;javascript&#39; type=&#39;text/javascript&#39;>"; 
echo "window.location.href='$url'"; 
echo "</script>"; 
?> 

Delete the file of the message delete.php

<?php     
  
            mysql_connect("localhost:3308","root","root");  
            mysql_select_db("se");  
            mysql_query("set names &#39;gbk&#39;");  
           $id = $_GET[&#39;id&#39;]; 
$query="delete from liuyan where id=".$id; 
mysql_query($query); 
?> 
<?php 
//页面跳转,实现方式为javascript 
$url = "show_liuyan.php"; 
echo "<script language=&#39;javascript&#39; type=&#39;text/javascript&#39;>"; 
echo "window.location.href='$url'"; 
echo "</script>"; 
?> 

The above has introduced the PHP+MYSQL message board of PHP Example 5, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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