简单的php+mysql留言本源码

作者: 小默 分类: php开发 发布时间: 2016-03-30 15:06 ė
Warning: Use of undefined constant the_views - assumed 'the_views' (this will throw an Error in a future version of PHP) in /www/wwwroot/www.seohave.com/wp-content/themes/TangStyle/single.php on line 19
14,076 人访问
66条评论

贴出代码希望能和大家一起学习php的留言本的精髓,有什么想法尽管留言,定回复
共3个文件
IncDB.php数据库连接
index.php首页
InsetToDB.php数据库操作
数据库lguestbook里面建表
复制代码代码如下:

CREATE TABLE `intd` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) character set utf8 collate utf8_bin NOT NULL,
`text` text character set utf8 collate utf8_bin NOT NULL,
`datetime` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gb2312 AUTO_INCREMENT=11 ; //此句参考下面的扩展内容; engin=myisam default charset = gb2312 auto_increment=11

IncDB.php数据库连接

<?php
$link=mysql_connect(‘localhost’,’root’,’root’);
if(!$link)
{
die(“<center>出错啦:1!</center>”);
}
if(!mysql_select_db(‘guestbook’,$link))
{
die(“<center>出错 www.hthrt.com啦:2!</center>”);
}
?>

 

index.php代码

代码www.seohave.com如下:
<meta http-equiv=”Content-Type” content=”text/html; charset=gbk”>
<?php
include(“IncDB.php”);
$result=mysql_query(“SELECT * FROM intd”,$link);
$row=mysql_fetch_row($result);
while($row)
{
echo “ID: “.$row[0].” 姓www.yiwenxiang.com.cn名: “.$row[1].” 时间: “.$row[3].”<br>”;
echo $row[2];
echo “<hr><br>”;
$row=mysql_fetch_row($result);
}
mysql_close($link);
?>
<form method=”POST” action=”InsetToDB.php”>
昵称:<input type=”text” size=”8″; name=”name”>
<p>内www.dd951.com容:<textarea rows=”5″ name=”text” cols=”60″></textarea>
</p>
<p><input type=”submit” value=”提交” name=”B1″><input type=”reset” value=”重置” name=”B2″></p>
</form>

 

 

InsetToDB.php的代码:

代码如下:
<?php
include(“IncDB.php”);
$name=addslashes($_POST[‘name’]); //addslashes — 字符串www.fuweitek.com加入斜线string addslashes(string str);
$text=addslashes($_POST[‘text’]);
$sql = “INSERT INTO `intd` (`id`, `name`, `text`, `datetime`) VALUES (NULL, ‘$name’, ‘$text’, now());”;
//$sql=”INSERT INTO `intd` ( , `name` , `text`,`datetime` ) VALUES ( ,’$name’,’$text’,now())”;
if(mysql_query($sql,$link))
{
echo “留www.kshyf.com言成www.szhrtz.com功!”;
echo “<meta http-equiv=\”refresh\” content=\”1;URL=index.php\”>”;
}
else
echo “留www.penqiang.net言失败!”;
mysql_close($link);
?>

 

 

 

PHP 中的 addslashes 函数

addslashes — 字符串加入斜线。

语法: string addslashes(string str);

内容说明

本函数使需要让数据库处理的字符串中引号的部份加上斜线,以供数据库查询 (query) 能顺利运作。这些会被改的字符包括单引号 (‘)、双引号 (“)、反斜线 backslash () 以及空字符 NUL (the null byte)。

1,表单提交中addslashes的表现。

首先要看get_magic_quotes_gpc()的值,一般为 1 。这时候从 <TEXTAREA> 提交的内容会自动加上斜线。
比如输入 ‘ 变成 ‘ , ” 变成 ” , 变成

<html><head><title>test</title></head>
<body>
<FORM action=”” method=post>
<TEXTAREA name=message rows=”18″ cols=”55″ >default text</TEXTAREA>
<INPUT type=submit value=Submit name=submit></FORM>
<?php
echo get_magic_quotes_gpc().
” A “.$_POST[‘message’].
” B “.stripslashes($_POST[‘message’]);
?>
</body></html>

输入:include(‘/home/me/myfile’);
输出:1 A include(‘/home/me/myfile’); B include(‘/home/me/myfile’);

总结:get_magic_quotes_gpc()等于1的情况下,如果不输入数据库,那你得到的结果是加了斜线的。

2,提交输入数据库时addslashes的表现。

<html><head><title>test</title></head>
<body>
<FORM action=”” method=post>
<TEXTAREA name=message rows=”18″ cols=”55″ >default text</TEXTAREA>
<INPUT type=submit value=Submit name=submit></FORM>
<?php
require_once(‘includes/common.php’);
$db->query(“INSERT INTO `testtable` ( id , content ) VALUES (‘1’ , ‘”.$_POST[‘message’].”‘)”);
$query=$db->query(“select * from `testtable` where `id`= 1;”);
$Result=$db->fetch_array($query);
echo get_magic_quotes_gpc().
” A “.$_POST[‘message’].
” B “.$Result[‘content’];
?>
</body></html>

输入:include(‘/home/me/myfile’);
输出:1 A include(‘/home/me/myfile’); B include(‘/home/me/myfile’);

总结:get_magic_quotes_gpc()等于1的情况下,如果输入数据库后,再从数据库直接读取的时候,你不做任何修改就可以得到输入的字符串。

3, get_magic_quotes_gpc()

get_magic_quotes_gpc()在服务器是的设置是不能runtime修改的,也就是说,你必须在你的网页代码中预先考虑好不同的情况,不然,当你提交数据的时候,你还不知道服务器给你加了斜线没有。以下两个网上流行的函数可能是大家需要的,个人喜欢第二个:
function my_addslashes( $message ){
if(get_magic_quotes_gpc()== 1 ){
return $message;
}else{
if(is_array($message)==true){
while(list($key,$value)=each($message)){
$message[$key]=my_addslashes($value);
}
return $message;
}else{
return addslashes($message);
}
}
}

function my_addslashes($data){
if(!get_magic_quotes_gpc()) {
return is_array($data)?array_map(‘AddSlashes’,$data):addslashes($data);
} else {
Return $data;
}
}

简单的解释就是,如果get_magic_quotes_gpc()等于 1 (服务器默认设置为 1 ),那我们的字符串是可以直接入库的,不修改。不然,我们才用addslashes函数。

本次代码php的留言板的代码就全部贴出来了,大家看看有什么不足之处提醒我

1

本文出自 建站seo运营,转载时请注明出处及相应链接。

本文永久链接: https://www.seohave.com/359.html

6条评论

  1. 跨境电商 2016年5月3日 上午 5:36

    这个文章必须顶。。

  2. 易路营销 2016年4月24日 上午 8:43

    不错,不错,看看了!

  3. RIGHT!!! 2016年4月20日 下午 8:54

    Wonderful!值得学习!很好!

  4. 2650194355 2016年4月16日 上午 10:02

    我又来了,您高兴吗?!

  5. 刘江强 2016年4月8日 下午 10:40

    我用主机屋的免费主机很久了,很稳定,我会坚持用下去

  6. zengda 2016年4月7日 上午 11:17

    不错,不错,看看了!

发表回复

Ɣ回顶部