制作PHP+MySQL留言板

jopen 10年前發布 | 42K 次閱讀 PHP MySQL PHP開發

創建一個數據庫

CREATE TABLE `message` (
  `id` tinyint(1) NOT NULL auto_increment,
  `user` varchar(25) NOT NULL,
  `title` varchar(50) NOT NULL,
  `content` tinytext NOT NULL,
  `lastdate` date NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk AUTO_INCREMENT=1 ;

 

創意一個文件,用作鏈接數據庫

<?php
$conn = @mysql_connect("localhost", "root", "") or die("數據庫鏈接錯誤");
mysql_select_db("bbs", $conn);
mysql_query("set names 'GBK'"); //使用GBK中文編碼;
?>

 

創建提交留言到數據庫的文件

<?php
error_reporting(E_ALL & ~E_NOTICE);
    include("conn.php");
    if($_POST['submit']){
        echo  $sql="insert into message (id,user,title,content,lastdate)" .
        "value('','$_POST[user]','$_POST[title]','$_POST[content]',now())";
        mysql_query($sql);
        echo "發表成功";
    }
?>
<form action="add.php" method="post">
用戶:<input type="text" size="10" name="user"><br>
標題:<input type="text" size="10" name="title"><br>
內容:<textarea name="content"></textarea><br>
<input type="submit" name="submit" value="發布留言">
</form>

最好不要用第二行  error_reporting(E_ALL & ~E_NOTICE);

而是把 if($_POST['submit']) 條件語句修改為 if(isset($_POST['submit']) && $_POST['submit'])


創建一張預覽頁面

<?php
include("conn.php");
?>
<table width=500 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef">
  <?php
  $sql="select * from message order by id desc";
  $query=mysql_query($sql,$conn);
  while ($row=mysql_fetch_array($query)){
  ?>
  <tr bgcolor="#eff3ff">
  <td>標題:<?php echo $row['title'];?> 用戶:<?php echo $row['user'];?></td>
  </tr>
  <tr bgColor="#ffffff">
  <td>內容:<?php echo $row['content'];?></td>
  </tr>

  <?php
  }  print_r($row);
 
  ?>
  </table>

來自:http://my.oschina.net/u/925242/blog/289748

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!