php闪现留言进去数据库 php留言板教程

用php将数据写进数据库中如果php闪现留言进去数据库你会点php基础php闪现留言进去数据库我可以给你点参考意见
一个是php闪现留言进去数据库 , 你这个文件肯定是txt格式的
$fp = fopen('xxx.txt','r');//用只读方式打开文件
$getsource = fgets($fp);//读取第一行,主要是用于去除第一行
while(!feof($fp)){
$getsource = fgets($fp);//获取有用数据
$insource = explode(" ", $getsource);//把一行文字变成数组 , 按符号分割
/*---------------下面写数据存储的sql-------------------*/
......
/*---------------上面写数据存储的sql-------------------*/
}
php闪现留言进去数据库我默认为你对php和mysql是熟悉的 , 不然你也不会打算用php写这个程序,我只提供你点思路,php对文件操作不熟悉的话你可以参考下这篇文章
;id=77class=2
希望对你有帮助
php写入数据库PHP向MySQL数据库中写入数据有三个步骤php闪现留言进去数据库:
1php闪现留言进去数据库,PHP和MySQL建立连接关系
2php闪现留言进去数据库,打开MySQL数据库
3,接受页面数据,PHP录入到指定php闪现留言进去数据库的表中
1、2两步可直接使用一个数据库链接文件即可:conn.php
代码如下
?php
mysql_connect("localhost","root","");//连接MySQL
mysql_select_db("hello");//选择数据库
?
当然,前提是已经安装WEB服务器、PHP和MySQL , 并且建立MySQL表“cnbruce”
mysql_connect()中三个参数分别为MySQL地址、MySQL用户名和MySQL密码
然后就是通过WEB页面传递数据,让PHP通过SQL语句将数据写入MySQL数据库指定的表中,比如新建文件 post.php
代码如下
?php
require_once("conn.php");//引用数据库链接文件
$uname = $_GET['n'];//GET方法为URL参数传递
$psw = $_GET['p'];
$psw=md5($psw);//直接使用MD5加密
$sql = "insert into members(username,password) values ('$uname','$psw')";
mysql_query($sql);//借SQL语句插入数据
mysql_close();//关闭MySQL连接
echo "成功录入数据";
?
测试页面: ;p=i0514
即可向MySQL数据库hello的members表中插入新的数据“cnbruce”到username字段、“i0514”到password字段
补充:读取表
读取表中的内容,这里我们用while,可以根据具体情况,用for 或其php闪现留言进去数据库他的.
代码如下
while($row = mysql_fetch_array($result))
{
echo "div style="height:24px; line-height:24px; font-weight:bold;""; //排版代码
echo $row['Topic'] . "br/";
echo "/div"; //排版代码
php怎么在mysql创建留言板步骤工具:
Dreamweaver
php、mysql服务器
步骤/方法
首先是确定自己的留言板需求.例如:名字,邮件及留言内容.
一. 建立一个数据库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=MyISAMDEFAULT CHARSET=utf8 AUTO_INCREMENT=3;
二. 新建config.php
? php
$q = mysql_connect("服务器","数据库用户","数据库密码");
if(!$q)
{
die('Could not connect: ' . mysql_error());
}
mysql_query("set names utf8");//以utf8读取数据
mysql_select_db("guestbook",$q);//数据库
?
三. 新建index.php
?php
include("config.php");//引入数据库连接文件
$sql = "select * from content";//搜索数据表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="https://www.04ip.com/post/index.php"首页 /a |a href="https://www.04ip.com/post/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="https://www.04ip.com/post/index.php"首页 /a |a href="https://www.04ip.com/post/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="https://www.04ip.com/post/提交"
input type="reset" name="button2" id="button2" value="https://www.04ip.com/post/重置"
/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='https://www.04ip.com/post/index.php'; /script";
?
这样已经成功的写出一个留言板了 。
第二部分
此次将在上面版本上加多管理 , 回复等功能 。
首先在sql中字节 。
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="https://www.04ip.com/post/index.php"首页 /a |a href="https://www.04ip.com/post/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="https://www.04ip.com/post/提交"
input type="reset" name="button2" id="button2" value="https://www.04ip.com/post/重置"
/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='https://www.04ip.com/post/index.php'; /script";
}else{
echo " scriptalert('错误!');location.href='https://www.04ip.com/post/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="https://www.04ip.com/post/index.php"首页 /a |a href="https://www.04ip.com/post/liuyan.php"留言 /a /td
td width="80"
// 新增管理员登录
?php
if (isset($_SESSION['adminname'])$_SESSION["adminname"] == "admin"){
echo " a href='https://www.04ip.com/post/logout.php'登出 /a";
}else{
echo " a href='https://www.04ip.com/post/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='https://www.04ip.com/post/huifu.php?id=" . $row[0] . "'回复 /a";
echo " | " . " a href='https://www.04ip.com/post/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='https://www.04ip.com/post/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="https://www.04ip.com/post/index.php"首页 /a |a href="https://www.04ip.com/post/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="https://www.04ip.com/post/回复" /
input type="reset" name="button2" id="button2" value="https://www.04ip.com/post/重置" /
/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='https://www.04ip.com/post/index.php'; /script";
?
这样就可以建立出一个简单的管理.管理帐号都是admin 因为只是判别输入的是不是admin 是的话就把值输入进session中.
自己用PHP写的留言板只有全部输入数字才能写入数据库并显示出来,而输入字母和汉字就不行,是怎么的?。?/h2>还有一种可能是编码错误,包括页面编码及数据库编码 。
可能导致录入数据乱码 。
求用PHP写一个简单基本的留言板代码,有点急数据库的创建
CREATE DATABASE GBOOK;
CREATE TABLE `gbook` (
`id` INT( 255 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 10 ) NOT NULL ,
`sex` TINYINT( 1 ) UNSIGNED NOT NULL ,
`email` VARCHAR( 255 ) NOT NULL ,
`info` TEXT NOT NULL ,
`ip` VARCHAR( 15 ) NOT NULL ,
`time_at` DATETIME NOT NULL
)
主要页面有:
input.html为初始页
insert.php为把输入内容送入数据库
show.php显示留言板内容
change.php修改留言板内容的界面
del.php删除留言板内容
change_ok.php修改留言板内容并送入数据库
input.html
html
head
meta http-equiv="content-type" content="text/html; charset=GB2312" /
title留言版/title
/head
body
form name="form1" method="post" action="insert.php"
p你的名字:input type="text" name="name" size="20" //p
p你的性别:input type="radio" value="https://www.04ip.com/post/1" name="sex" checked="checked" /男生input type="radio" value="https://www.04ip.com/post/0" name="sex" /女生 /p
p你的E-mail:input type="text" name="email" size="20" //p
p你的留言内容:/p
ptextarea rows="9" name="info" cols="35"/textarea/p
pinput type="submit" value="https://www.04ip.com/post/提交" name="B1" /input type="reset" value="https://www.04ip.com/post/重设" name="B2" //p
/form
/body
/html
insert.php
?php
$mysql_server_name = "localhost";
$mysql_username= "root";
$mysql_password= "123456";
$mysql_database= "gbook";
$ip = getenv('REMOTE_ADDR');
$conn = mysql_connect("localhost","root","123456");
mysql_select_db("gbook");
$sql = "INSERT INTO `gbook` ( `id` , `name` , `sex` , `email` , `info` , `ip` , `time_at` )
VALUES (NULL , '$name', '$sex', '$email', '$info', '$ip', NOW( ))";
$result = mysql_query($sql,$conn);
$id = mysql_insert_id();
mysql_close($conn);
?
p留言成功/p
pa href="https://www.04ip.com/post/show.php"去留言页 /a/p
show.php
?
$mysql_server_name = "localhost";
$mysql_username= "root";
$mysql_password= "123456";
$mysql_database= "gbook";
$sql = "SELECT * FROM gbook ORDER BY 'id' DESC"; //排序 后留言的在前面显示
$conn = mysql_connect($mysql_server_name,$mysql_username,$mysql_password);
mysql_select_db($mysql_database,$conn);
$result = mysql_query($sql);
while($row = mysql_fetch_row($result))
{// ----if语句判断男女------
【php闪现留言进去数据库 php留言板教程】if($row[2]==1)
{$gender = '男';}
else
{$gender = '女'; }
?
table width="752" border="1"
tr
td height="32"p?=$row[6]? ?=$row[5]? /p
p?=$row[1]?(?=$gender?)?=$row[3]?/p/td
/tr
tr
td height="45"?=nl2br($row[4])?pa href="https://www.04ip.com/post/change.php?id=?=$row[0]?"[修改]/a a href="https://www.04ip.com/post/del.php?id=?=$row[0]?"[删除]/a/p/td
/tr
/table
hr /
?
}
mysql_free_result($result);
?
change.php
?
$mysql_server_name = "localhost";
$mysql_username= "root";
$mysql_password= "123456";
$mysql_database= "gbook";
$sql = "SELECT name, sex, email, info FROM gbook WHERE id = '$id' ";
$conn = mysql_connect($mysql_server_name,$mysql_username,$mysql_password);
mysql_select_db($mysql_database,$conn);
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
?
html
head
meta http-equiv="content-type" content="text/html; charset=GB2312" /
title留言版/title
/head
body
form name="form1" method="post" action="change_ok.php?id?=$id?"
p你的名字:input type="text" name="name" size="20" value="https://www.04ip.com/post/?=$row[0]?"//p
?
// -----if语句判断男女 1为男 0为女-------
if($row[1]==1)
echo ' p你的性别:input type="radio" value="https://www.04ip.com/post/1" name="sex" checked="checked" /男input type="radio" value="https://www.04ip.com/post/0" name="sex" /女 /p';
else
echo ' p你的性别:input type="radio" value="https://www.04ip.com/post/1" name="sex" /男input type="radio" value="https://www.04ip.com/post/0" name="sex" checked="checked" /女 /p';
?
p你的E-mail:input type="text" name="email" size="20" value="https://www.04ip.com/post/?=$row[2]?"//p
p你的留言内容:/p
ptextarea rows="9" name="info" cols="35"?=$row[3]?/textarea/p
pinput type="submit" value="https://www.04ip.com/post/提交"/input type="reset" value="https://www.04ip.com/post/重设" //p
/form
/body
/html
del.php
?
$mysql_server_name = "localhost";
$mysql_username= "root";
$mysql_password= "123456";
$mysql_database= "gbook";
$sql = "DELETE FROM gbook WHERE id = '$id' ";
$conn = mysql_connect($mysql_server_name,$mysql_username,$mysql_password);
mysql_select_db($mysql_database,$conn);
$result = mysql_query($sql);
mysql_close($conn);
?
pa href="https://www.04ip.com/post/show.php"[返回]/a/p
change_ok.php
?
$mysql_server_name = "localhost";
$mysql_username= "root";
$mysql_password= "123456";
$mysql_database= "gbook";
$sql = "UPDATE `gbook` SET `name` = '$name',`sex` = '$sex',`email` = '$email',`info` = '$info' WHERE `id` ='$id' ";
$conn = mysql_connect($mysql_server_name,$mysql_username,$mysql_password);
mysql_select_db($mysql_database,$conn);
$result = mysql_query($sql);
mysql_close($conn);
?
pa href="https://www.04ip.com/post/show.php"[返回]/a/p
关于php闪现留言进去数据库和php留言板教程的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

    推荐阅读