增加留言版块——在线留言板1

      今天上午自己正事没有干,倒是用php写了一个小的在线留言板,然后时间也比较紧迫,这里面就有一个站内搜索的小技术,感觉实际上也比较简单,但是还是可以顺着这个思路在以后扩展用的。
      好了,废话不多,先看看留言板有什么功能: 新建留言,评论留言,查看评论,删除留言,站内搜索,分页显示。

      这里先上增加留言版块的代码: 这里面把填写信息和留言的处理加入数据库写在一个里面去了。

      这里面获取用户的信息,判断姓名和留言信息是否为空,以及对留言信息的处理的那几个函数,是重点。

<!--add.php:将用户添写数据写入数据库--------------->
<?php
require_once("sys_conf.inc");

//判断姓名和留言信息是否为空
$errorm="";
if(isset($_POST['name']) or  isset($_POST['message'])) {
//  if (!isset($message) or $message=="" or !isset($name) or $name=="")
    if($_POST['name']=="" or $_POST['message']=="")
    {
        $errorm = "<font color=red>出错了!!!</font>姓名和留言内容必填";
    }
    else
    {
        //连接数据库
        $link_id = mysql_connect($DBHOST, $DBUSER, $DBPWD);
        mysql_select_db($DBNAME);                //选择数据库my_chat

        //获取并处理用户提交信息
        $message = $_POST['message'];
        $name = $_POST['name'];
        $homepage = $_POST['homepage'];
        $email = $_POST['email'];
        $location = $_POST['location'];


        $space = "&nbsp;";
        $time = date("Y年m月d日H小时i分");
        $ip = $_SERVER['REMOTE_ADDR'];
        $message = StripSlashes($message);        //给字符串中按照需要加上""和去掉"",对于某些数据库,必须在要查询的字符加上和去掉""之后才能够查询.
        $message = htmlspecialchars($message); //将HTML特殊字符换成它们的名字,例如"<"变成"<".
        $message = nl2br($message);                    //在HTML中的每一个回车前面加上"<BR>".
       // echo($message);
        $guestcontent = "<tr><td>留言内容:<br>".$message;
        $guestcontent = $guestcontent . "<br><font color=#6633FF>留言人大名:    </font>$name";
        if ($email != "")
            $guestcontent = $guestcontent . "<br><font color=#9900CC>电子信箱:</font><a href="mailto:$email">$email</a>" . "$space";
        if ($homepage != "http://")
            $guestcontent = $guestcontent . "<font color=#9900CC>主页:</font><a href="$homepage">$homepage</a>";
        if ($location != "")
            $guestcontent = $guestcontent . "<br><font color=#0000FF>时间:$time 来自:$location  $ip</font>";
        $guestcontent = ereg_replace(chr(10), "", $guestcontent);
        $guestcontent = $guestcontent . "<hr size=1></td></tr>
";

        //插入数据
        $str = "insert into guestbook (time,ip,name,homepage,location,email,message,content,is_delete)";
        $str = $str . "values('$time','$ip','$name','$homepage','$location','$email','$message','$guestcontent','n')";
        //echo $str;
        $result = mysql_query($str, $link_id); //执行查询
        mysql_close($link_id);
        echo "<script>alert('success!');</script>";
        header("location:booklist.php");
    }
}
?>
<html>
<head>
    <title>站内搜索留言本
    </title>
    <style>
        <!--
        A:link {text-decoration: none ; color:#0000ff}
        A:visited {text-decoration: none; color:#004080}
        A:active {text-decoration: none}
        A:hover {text-decoration: underline; color:#ff0000}
        BODY {FONT-SIZE:10pt}
        TH {FONT-SIZE:10pt}
        TD {FONT-SIZE: 10pt}
        TEXTAREA
        {
            FONT-FAMILY: "宋体";
            FONT-SIZE: 10pt;
        }
        -->
    </style>
</head>
<body bgcolor=#FFFFFD>
<center>
    <?php include  "head.html"?>
    <h2>发表留言</h2>
    <table width="68%" border="1" cellpadding="3" cellspacing="0" bordercolor="#E3E3E3">
        <form method="POST" action="add.php">
            <?php
            //如果输入的用户名或留言信息为空,则报错
            if ($errorm)
            {
                echo "<tr>";
                echo "<td colspan=3 height=32> ";
                echo "$errorm";
                echo "</td>";
                echo "</tr>";
            }
            ?>
            <tr>
                <td width="22%" bgcolor="#F0F0F0"><font color="#000000">姓名<font color="#FF0033">(必填)</font></font></td>
                <td colspan="2" width="78%" bgcolor="#F0F0F0"><font color="#00FF00">
                        <input type="text" name="name" size="40">
                    </font></td>
            </tr>
            <tr>
                <td width="22%" height="29">主页:</td>
                <td colspan="2" height="29" width="78%">
                    <input type="text" name="homepage" size="40" value="http://">
                </td>
            </tr>
            <tr>
                <td width="22%" height="27" bgcolor="#F0F0F0">来自:</td>
                <td colspan="2" height="27" width="78%" bgcolor="#F0F0F0">
                    <input type="text" name="location" size=40">
                </td>
            </tr>
            <tr>
                <td width="22%" height="20">Email:</td>
                <td colspan="2" height="20" width="78%"><font color="#00FF00">
                        <input type="text" name="email" size="40">
                    </font></td>
            </tr>
            <tr>
                <td colspan="3" valign="middle" align="left">
                    <div align="center"><font color="#000000">请留言</font><font color="#FF0033">(必填)</font><font color="#00FF00"><br>
                            <textarea rows="6" name="message" cols="55" wrap="VIRTUAL"></textarea>
                        </font></div>
                </td>
            </tr>
            <tr bgcolor="#F0F0F0">
                <td colspan="3" height="24">
                    <div align="center"><font color="#00FF00">
                            <input type="submit" value="提   交" name="ok">
                            &nbsp;&nbsp;&nbsp;
                            <input type="reset" value="重 写" name="cencel">
                        </font>
                </td>
            </tr>
        </form>
    </table>

</center>
</body>
</html>

      1.这个:

string ereg_replace ( string $pattern , string $replacement , string $string )

      本函数在 string 中扫描与 pattern 匹配的部分,并将其替换为 replacement。

      返回替换后的字符串。(如果没有可供替换的匹配项则会返回原字符串。)

      如果 pattern 包含有括号内的子串,则 replacement 可以包含形如 digit 的子串,这些子串将被替换为数字表示的的第几个括号内的子串; 则包含了字符串的整个内容。最多可以用九个子串。括号可以嵌套,此情形下以左圆括号来计算顺序。

      如果未在 string 中找到匹配项,则 string 将原样返回。

      例如,下面的代码片断输出 “This was a test” 三次:

      Example #1 ereg_replace() 例子:

<?php
$string = "This is a test";
echo str_replace(" is", " was", $string);
echo ereg_replace("( )is", "\1was", $string);
echo ereg_replace("(( )is)", "\2was", $string);
?>

      2.mysql_fetch_row() 从和结果标识 data 关联的结果集中取得一行数据并作为数组返回。每个结果的列储存在一个数组的单元中,偏移量从 0 开始。

      3.PHP stripslashes() 函数
      注释:该函数用于清理从数据库或 HTML 表单中取回的数据。

原文地址:https://www.cnblogs.com/zjunet/p/4559921.html