3

我正在使用 XAMPP 在 windows7 上遵循这个php 教程。
这是一个留言簿教程,将用户条目保存到 mysql 并显示来自 db 的条目。当我将数据输入表单并提交时,浏览器显示此错误消息

禁止访问! 您无权访问请求的对象。它要么是读保护的,要么是服务器不可读的。如果您认为这是服务器错误,请联系网站管理员。 错误 403 localhost Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7

更新

留言板.php

连接到数据库代码

<?php
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "";
$dbDatabase = "myDB";

// Connect to DB

$li = new mysqli('localhost', 'root', '', 'myDB') or 
      die("Could not connect". mysqli_connect_error());
//mysql_select_db($dbDatabase, $li) or 
      die ("could not select DB". mysql_error());
?>

变量初始化

<?php
// initiate some vars

$gb_str = "";   
// $gb_str is the string we'll append entries to
$pgeTitle = "View and Sign Guestbook";

发布请求处理

// If form is submitted, then insert into DB
if (!empty($_POST["submit"])) {
    $name = $_POST["frmName"];
    $email = $_POST["frmEmail"];
    $comment = $_POST["frmComment"];
    $date = Date("Y-m-d h:i:s");

    $gb_query =     "insert into guestbook
            values(0, '$name', '$email', '$comment', '$date')";
    // Performs the $sql query on the server to insert the values
    if ($li->query($gb_query) === TRUE) {
        echo 'users entry saved successfully';
    }
    else {
        echo 'Error: '. $li->error;
    }
    /*
    $sql = mysql_query($gb_query);
    $res = mysql_affected_rows($sql);

    // See if insert was successful or not
    if($res > 0) {
        $ret_str="Your guestbook entry was successfully added.";
    } else {
        $ret_str = "Your guestbook entry was NOT successfully added.";
    }

    // Append success/failure message
    $gb_str .= "<span class=\"ret\">$ret_str</span><BR>";
    */
}
?>

留言簿列表

<?php

$get_query = "select gbName, gbEmail, gbComment, 
              DATE_FORMAT(gbDateAdded, '%m-%d-%y %H:%i') gbDateAdded
              from guestbook";

$result = $li->query($get_query);
$gb_str .= "<hr size=\"1\">";

if ($result->num_rows > 0) {
    // output data of each row from $result
    while($row = $result->fetch_assoc()) {
    $name = $row["gbName"];
    $email = $row["gbEmail"];
    $comment = $row["gbComment"];
    $date = $row["gbDateAdded"];

    if(!empty($name)) {
        // If name exists and email exists, link name to email
        if(!empty($email)) {
            $name="by <a href=\"mailto:$email\">$name</a>";
        }
    // If name does exist and email exists, link email to email     
    } else if (!empty($email)) {
        $name = "by <a href=\"mailto:$email\">$email</a>";
    } else {
        $name = "";
    }

    // Append to string we'll print later on
    $gb_str .= "<br>$comment<p class=\"small\">
                posted on $date $name<hr size=\"1\">";
}}

?>

HTML 页面

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML>
<HEAD>
<TITLE>Guestbook</TITLE>
<SCRIPT language="javascript">
<!--

/* This function is pulled from a generic validation file from
some other site (probably developer.netscape.com) and strips out
characters you don't want */

function stripCharsInBag (s, bag) {
    var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

// This function just makes sure the comment field is not empty

function valForm(frm) {
    badChars = "<[]>{}";
    if(frm.frmComment.value == "") {
        alert("Please fill in your comments for the guestbook.");
        return false;
    } else {
        frm.frmComment.value = stripCharsInBag(frm.frmComment.value, badChars);
        // These values may be empty, but strip chars in case they're not
        frm.frmName.value = stripCharsInBag(frm.frmName.value, badChars);
        frm.frmEmail.value = stripCharsInBag(frm.frmEmail.value, badChars);
        return true;
    }
}

-->
</SCRIPT>
</HEAD>

<BODY bgcolor="#FFFFFF">
<?php echo $gb_str; ?>

<form name="gb" action="<? echo $PHP_SELF;?>" method="post">
<table cellpadding="3" cellspacing="0" border="0">
  <tr>
    <td class="tdhead" valign="top" align="right">Name</td>
    <td valign="top">
      <input type="text" name="frmName" value="" size="30" 
             maxlength="50">
      </td>
  </tr>
  <tr>
    <td class="tdhead" valign="top" align="right">Email</td>
    <td valign="top">
      <input type="text" name="frmEmail" value="" size="30" 
             maxlength="100">
    </td>
  </tr>
  <tr>
    <td class="tdhead" valign="top" align="right">Comment</td>
    <td valign="top">
        <textarea name="frmComment" rows="5" cols="30"></textarea>
    </td>
  </tr>
  <tr>
    <td> </td>
    <td><input type="submit" name="submit" value="submit" 
               onClick="return valForm(document.gb)">
        <input type="reset" name="reset" value="reset">
    </td>
  </tr>
</table>
</form>

</BODY>
</HTML>

<?php
// Close MySQL Connection
$li->close();
?>
4

2 回答 2

1

第二个问题的答案,您没有将查询结果分配给变量并且mysql_affected_rows也是空的。

$gb_query =     "insert into guestbook
        values(0, '$name', '$email', '$comment', '$date')";

$sql = mysql_query($gb_query);
$res = mysql_affected_rows($sql);
于 2012-10-10T12:30:44.763 回答
1

听起来错误是由 Xampp 配置阻止目录引起的。如果您在开发机器上并且不关心安全性,请在 xampp 下的 apache/conf 目录中查找名为 httpd.conf 的文件。

您会在其中找到与此类似的指令

<Directory />
    Options FollowSymLinks
    AllowOverride none
    Order deny,allow
    Allow from none
    Deny from all
</Directory>

换成这个

<Directory />
    Options FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
    Deny from none
</Directory>

这使得网络服务器可以访问根驱动器上的每个文件,这在开发机器上很好(尽管有些人肯定会争论这一点),但在生产中根本不是一个好主意。

于 2012-10-11T09:30:50.383 回答