簡(jiǎn)單的PHP留言本實(shí)例代碼
更新時(shí)間:2010年05月09日 10:54:23 作者:
對(duì)于學(xué)習(xí)php的朋友,開始做個(gè)留言板對(duì)于php+mysql的操作有個(gè)簡(jiǎn)單的過(guò)程。學(xué)會(huì)了這個(gè)基本上php開始入門了。
config.php
<?php
$conn = @mysql_connect("localhost","root","") or die("數(shù)據(jù)庫(kù)連接出錯(cuò)!");
mysql_select_db("gb",$conn);
mysql_query("set names 'GBK'");
?>
add.php
<?php
include("config.php");
if($_POST['submit']){
//在這里的時(shí)候,忘記message里還有個(gè)字段lastdate沒(méi)有寫,導(dǎo)致插入數(shù)據(jù)不成功。找了好久才找出錯(cuò)誤。
$sql="insert into message (id,user,title,content,lastdate) values ('','$_POST[user]','$_POST[title]','$_POST[content]',now())";
mysql_query($sql);
echo "成功";
}
?>
<form action="add.php" method="post">
用戶:<input type="text" name="user" /><br>
標(biāo)題:<input type="text" name="title" /><br />
內(nèi)容:<textarea name="content"></textarea><br />
<input type="submit" name="submit" value="提交" />
</form>
view.php
<?php
include("config.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);
while($row=mysql_fetch_array($query)){
?>
//NND。我在wampserver默認(rèn)環(huán)境下,使用<?=$row[title]?>這種語(yǔ)法,就是讀取不出內(nèi)容來(lái)。非要用這種才可以。郁悶。又是好久才琢磨出來(lái)
<tr bgcolor="#eff3ff">
<td>標(biāo)題:<?php echo $row[title];?> 用戶:<?php echo $row[user];?></td>
</tr>
<tr bgColor="#ffffff">
<td>內(nèi)容:<?php echo $row[content];?></td>
</tr>
<?php
}
?>
</table>
然后還有個(gè)數(shù)據(jù)庫(kù)的SQL。
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 ;
復(fù)制代碼 代碼如下:
<?php
$conn = @mysql_connect("localhost","root","") or die("數(shù)據(jù)庫(kù)連接出錯(cuò)!");
mysql_select_db("gb",$conn);
mysql_query("set names 'GBK'");
?>
add.php
復(fù)制代碼 代碼如下:
<?php
include("config.php");
if($_POST['submit']){
//在這里的時(shí)候,忘記message里還有個(gè)字段lastdate沒(méi)有寫,導(dǎo)致插入數(shù)據(jù)不成功。找了好久才找出錯(cuò)誤。
$sql="insert into message (id,user,title,content,lastdate) values ('','$_POST[user]','$_POST[title]','$_POST[content]',now())";
mysql_query($sql);
echo "成功";
}
?>
<form action="add.php" method="post">
用戶:<input type="text" name="user" /><br>
標(biāo)題:<input type="text" name="title" /><br />
內(nèi)容:<textarea name="content"></textarea><br />
<input type="submit" name="submit" value="提交" />
</form>
view.php
復(fù)制代碼 代碼如下:
<?php
include("config.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);
while($row=mysql_fetch_array($query)){
?>
//NND。我在wampserver默認(rèn)環(huán)境下,使用<?=$row[title]?>這種語(yǔ)法,就是讀取不出內(nèi)容來(lái)。非要用這種才可以。郁悶。又是好久才琢磨出來(lái)
<tr bgcolor="#eff3ff">
<td>標(biāo)題:<?php echo $row[title];?> 用戶:<?php echo $row[user];?></td>
</tr>
<tr bgColor="#ffffff">
<td>內(nèi)容:<?php echo $row[content];?></td>
</tr>
<?php
}
?>
</table>
然后還有個(gè)數(shù)據(jù)庫(kù)的SQL。
復(fù)制代碼 代碼如下:
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 ;
您可能感興趣的文章:
相關(guān)文章
實(shí)例介紹PHP中zip_open()函數(shù)用法
在本篇內(nèi)容里小編給大家分享了關(guān)于PHP中zip_open()函數(shù)用法的相關(guān)知識(shí)點(diǎn),有需要的朋友們跟著學(xué)習(xí)下。2019-02-02
php opendir()列出目錄下所有文件的實(shí)例代碼
這篇文章主要介紹了php opendir()列出目錄下所有文件的實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10
thinkphp實(shí)現(xiàn)數(shù)組分頁(yè)示例
這篇文章主要介紹了thinkphp實(shí)現(xiàn)數(shù)組分頁(yè)示例,需要的朋友可以參考下2014-04-04
Yii中的relations數(shù)據(jù)關(guān)聯(lián)查詢及統(tǒng)計(jì)功能用法詳解
這篇文章主要介紹了Yii中的relations數(shù)據(jù)關(guān)聯(lián)查詢及統(tǒng)計(jì)功能用法,結(jié)合實(shí)例形式分析了關(guān)聯(lián)查詢命名空間及評(píng)論統(tǒng)計(jì)功能相關(guān)技巧,需要的朋友可以參考下2016-07-07

