为网站创建留言簿,我使用 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';
}
?>