php留言板結合數(shù)據(jù)庫 php實現(xiàn)留言板

php中,用Dreamweaver制作了個留言本,怎么連接數(shù)據(jù)庫呢?用的是easyphp的軟件。

創(chuàng)建php文件

創(chuàng)新互聯(lián)專注于新樂企業(yè)網(wǎng)站建設,響應式網(wǎng)站開發(fā),商城開發(fā)。新樂網(wǎng)站建設公司,為新樂等地區(qū)提供建站服務。全流程按需求定制制作,專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務

代碼如下

?php

mysql_connect('localhost','mysql賬號','mysql密碼') or die('數(shù)據(jù)庫連接失敗');

$sql="CREATE DATABASE `mydb`";

$result = mysql_query($sql);

if($result==true){

echo '數(shù)據(jù)庫創(chuàng)建成功';

}

mysql_select_db('mydb');

$sql="CREATE TABLE myTable (`id` INT(10) AUTO_INCREAMENT PRIMARY KEY,

`something` VARCHAR(10) NOT NULL

)";

$result = mysql_query($sql);

if($result==true){

echo '表創(chuàng)建成功';

}

//至此,數(shù)據(jù)庫,表創(chuàng)建成功,你就可以寫自己的sql語句經(jīng)行數(shù)據(jù)庫操作了

?

用php些一個可注冊登陸的留言板,和數(shù)據(jù)庫相連sql語句怎樣寫啊

$conn =mysqli_connect ([ string $host [, string $username [, string $passwd [, string $dbname [, int $port [, string $socket ]]]]]] );

一個參數(shù)是 主機名 第二個是 用戶名 第三個 是密碼 第四個是 數(shù)據(jù)庫名; 后面的不用填了;

然后

$reshult=$conn-query('查詢語句');

php留言板可以把數(shù)據(jù)提交到數(shù)據(jù)庫,但是讀取不了數(shù)據(jù)怎么辦?

1:可以新增數(shù)據(jù)的話表示數(shù)據(jù)庫連接沒有問題

2:請檢查mysql里面

asp

表有沒有數(shù)據(jù)

,有的話

code

:

$_sql

=

$conn-query("select

*

from

asp

where

1=1

order

by

date

desc

limit

0,5");

if($_sql

$_sql-num_rows0){

while($_rs

=

$_sql-fetch_array(MYSQL_ASSOC)){

echo

'內(nèi)容:'.$_rs['content'];

echo

'時間:'.$_rs['date'];

//打印全部

print_r($_rs);

}else{

echo

'沒有任何數(shù)據(jù)';

}

}

//分頁的話建議采用分頁類:

php怎么在mysql創(chuàng)建留言板步驟

工具:

Dreamweaver

php、mysql服務器

步驟/方法

首先是確定自己的留言板需求.例如:名字,郵件及留言內(nèi)容.

一. 建立一個數(shù)據(jù)庫guestbook。

CREATE TABLE IF NOT EXISTS `content` (

`id` int(11) NOT NULL auto_increment,

`name` varchar(20) NOT NULL,

`email` varchar(50) NOT NULL,

`content` varchar(200) NOT NULL,

PRIMARY KEY (`id`))

ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3;

二. 新建config.php

? php

$q = mysql_connect("服務器","數(shù)據(jù)庫用戶","數(shù)據(jù)庫密碼");

if(!$q)

{

die('Could not connect: ' . mysql_error());

}

mysql_query("set names utf8"); //以utf8讀取數(shù)據(jù)

mysql_select_db("guestbook",$q); //數(shù)據(jù)庫

?

三. 新建index.php

?php

include("config.php"); //引入數(shù)據(jù)庫連接文件

$sql = "select * from content"; //搜索數(shù)據(jù)表content

$resule = mysql_query($sql,$q);

?

html

meta http-equiv="Content-Type" content="text/html; charset=utf-8" /

body

table width="678" align="center"

tr

td colspan="2" h1留言本 /h1 /td

/tr

tr

td width="586" a href="index.php"首頁 /a | a href="liuyan.php"留言 /a /td

/tr

/table

p

?

while($row=mysql_fetch_array($resule))

{

?

/p

table width="678" border="1" align="center" cellpadding="1" cellspacing="1"

tr

td width="178"Name: ? echo $row[1] ? /td

td width="223"Email: ? echo $row[2] ? /td

/tr

tr

td colspan="4" ? echo $row[3] ? /td

/tr

tr

/table

?

}

?

/body

/html

四. 新建liuyan.php

html

body

meta http-equiv="Content-Type" content="text/html; charset=utf-8" /

table width="678" align="center"

tr

td colspan="2" h1留言本 /h1 /td

/tr

tr

td width="586" a href="index.php"首頁 /a | a href="liuyan.php"留言 /a /td

/tr

/table

table align="center" width="678"

tr

td

form name="form1" method="post" action="post.php"

p

Name:

input name="name" type="text" id="name"

/p

pEmail: input type="test" name="email" id="email" /p

p

留言:

/p

p

textarea name="content" id="content" cols="45" rows="5" /textarea

/p

p

input type="submit" name="button" id="button" value="提交"

input type="reset" name="button2" id="button2" value="重置"

/p

/form

/td

/tr

/table

/body

/html

五. 新建post.php

?php

header("content-Type: text/html; charset=utf-8");

include("config.php");

$name= $_POST['name'];

$email= $_POST['email'];

$patch = $_POST['content'];

$content = str_replace("

"," br /",$patch);

$sql = "insert into content (name,email,content) values ('$name','$email','$content')";

mysql_query($sql);

echo " scriptalert('提交成功!返回首頁。');location.href='index.php'; /script";

?

這樣已經(jīng)成功的寫出一個留言板了。

第二部分

此次將在上面版本上加多管理,回復等功能。

首先在sql中字節(jié)。

ALTER TABLE `content` ADD `reply` VARCHAR( 200 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL AFTER `content`

一. 新建login.php

html xmlns=""

meta http-equiv="Content-Type" content="text/html; charset=utf-8" /

title無標題文檔 /title

/head

body table width="678" align="center"

tr

td colspan="2" h1留言本 /h1 /td

/tr

tr

td width="586" a href="index.php"首頁 /a | a href="liuyan.php"留言 /a /td

/tr

/table

table align="center" width="678"

tr

td align="center"

form name="form1" method="post" action="login2.php"

label for="textfield" /label

p /p

p帳號:

input type="text" name="name" id="name"

/p

p密碼:

input type="password" name="pw" id="pw"

/p

p

input type="submit" name="button" id="button" value="提交"

input type="reset" name="button2" id="button2" value="重置"

/p

/form /td

/tr

/table

/body

/html

二.login2.php

?

session_start();

header("content-Type: text/html; charset=utf-8");

$name = $_POST['name'];

$pw = $_POST['pw'];

if($name == "admin" $pw == "admin"){

$_SESSION["adminname"] = $name;

echo " scriptalert('登錄完成,返回首頁!');location.href='index.php'; /script";

}else{

echo " scriptalert('錯誤!');location.href='login.php'; /script";

}

?

三. 在原有的index.php上添加

?php

session_start();

include("config.php");

$sql = "select * from content";

$resule = mysql_query($sql,$q);

?

html

meta http-equiv="Content-Type" content="text/html; charset=utf-8" /

body

table width="678" align="center"

tr

td colspan="2" h1留言本 /h1 /td

/tr

tr

td width="586" a href="index.php"首頁 /a | a href="liuyan.php"留言 /a /td

td width="80"

// 新增管理員登錄

?php

if (isset($_SESSION['adminname']) $_SESSION["adminname"] == "admin"){

echo " a href='logout.php'登出 /a";

}else{

echo " a href='login.php'管理員登錄 /a";

}

?

/td

/tr

/table

p

?

while($row=mysql_fetch_array($resule))

{

?

/p

table width="678" border="1" align="center" cellpadding="1" cellspacing="1"

tr

td width="178"Name: ? echo $row[1] ? /td

td width="223"Email: ? echo $row[2] ? /td

td width="100"

?php

if(isset($_SESSION['adminname']) $_SESSION["adminname"] == "admin"){

echo " a href='huifu.php?id=" . $row[0] . "'回復 /a";

echo " | " . " a href='delete.php?id=" . $row[0] . "'刪除 /a";

} else {

echo "";

}

?

/td

/tr

tr

td colspan="4" ? echo $row[3] ? /td

/tr

tr

td colspan="4" ?

if($row[4] == ""){

?

? echo "暫無回復。";?

? }else {echo "管理員回復:". $row[4]; } ? /td

/tr

/table

?

}

?

/body

/html

四. 新建huifu.php

?php

include("config.php");

$sql = "select * from content where id=".$_GET["id"];

$resule = mysql_query($sql,$q);

SetCookie("id",$_GET["id"]);

session_start();

header("content-Type: text/html; charset=utf-8");

if(empty($_SESSION["adminname"])){

exit(" script language='javascript'alert('您尚未登錄后臺,或登錄已超時,請重新登錄!');window.location.href='login.php'; /script");

}

?

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""

html xmlns=""

head

meta http-equiv="Content-Type" content="text/html; charset=utf-8" /

title無標題文檔 /title

/head

body

table width="678" align="center"

tr

td colspan="2" h1留言本 /h1 /td

/tr

tr

td width="586" a href="index.php"首頁 /a | a href="liuyan.php"留言 /a /td

/tr

/table

table align="center" width="678"

tr

td

form name="reply" method="post" action="reply.php"

p回復:

?

while($row=mysql_fetch_array($resule))

{

echo $row[3];

}

?

/p

p

textarea name="reply" id="reply" cols="45" rows="5" /textarea

/p

p

input type="submit" name="button" id="button" value="回復" /

input type="reset" name="button2" id="button2" value="重置" /

/p

/form

/td

/tr

/table

/body

/html

五. reply.php 回復留言提交頁面.

?

include("config.php");

$id = $_COOKIE["id"];

$sql = "select * from content";

header("content-Type: text/html; charset=utf-8");

$patch = $_POST["reply"];

$reply = str_replace("

"," br /",$patch);

$resule = mysql_query("UPDATE `2`.`content` SET `reply` = '$reply' WHERE `content`.`id` ="."$id");

echo " scriptalert('回復成功!');location.href='index.php'; /script";

?

這樣就可以建立出一個簡單的管理.管理帳號都是admin 因為只是判別輸入的是不是admin 是的話就把值輸入進session中.

如何用php在一個頁面中實現(xiàn)留言板,并連接數(shù)據(jù)庫。

連接數(shù)據(jù)庫和分頁都可以通過調(diào)用外部類來實現(xiàn),如果把類寫在頁面中會顯得很亂,容易出錯。

文章標題:php留言板結合數(shù)據(jù)庫 php實現(xiàn)留言板
標題網(wǎng)址:http://muchs.cn/article34/hjcppe.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、做網(wǎng)站網(wǎng)站導航、外貿(mào)建站、小程序開發(fā)、云服務器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)