Home > Article > Backend Development > PHP Example 5: PHP+MYSQL Message Board
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['do']; if($puanduan == 'OK') { $link = mysql_connect('localhost:3308','root','root'); if(!$link) { die('连接失败:'.mysql_error()); } //为后续的mysql扩展函数的操作选定一个默认的数据库,它相当于sql命令 use se mysql_select_db('se',$link) or die('不能选定数据库SE:'.mysql_error()); $insert = "insert into liuyan(liuyan_title,liuyan_name,liuyan_content) values ('$_POST[title]','$_POST[author]','$_POST[content]')"; //使用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); } ?>
<?php mysql_connect("localhost:3308","root","root"); mysql_select_db("se"); mysql_query("set names 'gbk'"); ?> <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['liuyan_title']?></font> 用户:<font color="red"><?=$row['liuyan_name'] ?></font><div align="right"> <a href="pre_edit_liuyan.php?id=<?=$row['id']?>">编辑</a> | <a href="delete_liuyan.php?id=<?=$row['id']?>">删除</a></div></td> </tr> <tr bgColor="#ffffff"> <td>内容:<?=$row['liuyan_content']?></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 'gbk'"); $id=$_GET['id']; $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['id']?>"> 用户:<INPUT TYPE="text" NAME="liuyan_name" value="<?=$rs['liuyan_name']?>"/><br /> 标题:<INPUT TYPE="text" NAME="liuyan_title" value="<?=$rs['liuyan_title']?>"/><br /> 内容:<TEXTAREA NAME="liuyan_content" ROWS="8" COLS="30"><?=$rs['liuyan_content']?></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 'gbk'"); $query="update liuyan set liuyan_name='$_POST[liuyan_name]',liuyan_title='$_POST[liuyan_title]',liuyan_content='$_POST[liuyan_content]' where id='$_POST[id]'"; mysql_query($query); ?> <?php //页面跳转,实现方式为javascript $url = "show_liuyan.php"; echo "<script language='javascript' type='text/javascript'>"; echo "window.location.href='$url'"; echo "</script>"; ?>
<?php mysql_connect("localhost:3308","root","root"); mysql_select_db("se"); mysql_query("set names 'gbk'"); $id = $_GET['id']; $query="delete from liuyan where id=".$id; mysql_query($query); ?> <?php //页面跳转,实现方式为javascript $url = "show_liuyan.php"; echo "<script language='javascript' type='text/javascript'>"; 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.