-2

为网站创建留言簿,我使用 while 循环从表中获取行,当我使用 echo 时,它返回行的名称而不是存储在其中的实际内容。我不知道我做错了什么。这是PHP:

    <?php

if(mysql_connect('localhost','root','')&& mysql_select_db('m_database')){
$time= time();
$errors =array();

if (isset($_POST['guestbook_name'],$_POST['guestbook_email'],$_POST['guestbook_message'])){

    $guestbook_name = mysql_real_escape_string(htmlentities($_POST['guestbook_name']));
    $guestbook_email = mysql_real_escape_string(htmlentities($_POST['guestbook_email']));
    $guestbook_message = mysql_real_escape_string(htmlentities($_POST['guestbook_message']));



    if ( empty($guestbook_name) || empty($guestbook_email) || empty ($guestbook_message)) {
        $errors[]='all fields must be completed.';
    }

if (strlen($guestbook_name)>25 || strlen($guestbook_email)>255 || strlen($guestbook_message)>255) {
    $errors= 'some of your fields exceeded the character limit, please double check.';
}
if ( empty($errors)){
    $insert = "INSERT INTO guestbook VALUES('','$time','$guestbook_name','$guestbook_email','$guestbook_message') ";

        if ( mysql_query ($insert)) {
            header('location: '.$_SERVER['PHP_SELF']);//redirect the user back to the same page, for spam purposes
        }else {
            $errors[] ='something is not working please try again';
        }

    }else{
    foreach ($errors as $error){
        echo'<p><strong>'.$error.'</strong></p>';
        }
    }

}
//set values form the form
//displaying the data
//using a while loop to display data on page
$entries =mysql_query("SELECT 'timestamp','name','email','message' FROM 'guestbook' ORDER BY 'timestamp'DESC");
if (mysql_num_rows($entries)==0){
    echo 'no entries found';

}else {
    while($entries_row = mysql_fetch_assoc($entries)){
        $entries_timestamp =$entries_row['timestamp'];
        $entries_name =$entries_row['name'];
        $entries_email =$entries_row['email'];
        $entries_message =$entries_row['message'];


            echo entries_name.'<br>';
                  echo $entries_message. '<br>';
                echo $entries_email. '<br>';
        }
    }


    } else {
    echo 'could not connect to database';
    }

    ?>
4

1 回答 1

2

我根据变量名猜测,因为您没有包含有关表的信息或使用的查询。您要查找的内容是否存储在“消息”列中?如果是这样,您正在回显错误的变量。尝试检查 $entries_message 变量。

更新:

现在您已经发布了完整的查询,我看到了问题。问题是您已将所需的列括在引号中。这告诉 MySQL 查询引擎返回这些字符串,但实际上并不返回列数据。从 SELECT 子句中删除引号,它应该可以按预期工作。

带有查询的行应为:

$entries = mysql_query( "SELECT timestamp, name, email, message FROM guestbook ORDER BY timestamp DESC" );
于 2012-12-31T17:02:08.413 回答