【问题标题】:All fields in my guestbook show up blank我的留言簿中的所有字段都显示为空白
【发布时间】:2015-01-15 03:45:01
【问题描述】:

我一直在尝试使用教程来创建留言簿,但实际传输到我的留言簿的唯一数据是日期和时间。

我有一个数据库设置为:

Id|姓名|电子邮件|评论|日期时间

我有 3 页:

addguestbook.php

<?php
$host="localhost"; // Host name 
$username="foo"; // Mysql username 
$password="bar"; // Mysql password 
$db_name="foo"; // Database name 
$tbl_name="guestbook"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server "); 
mysql_select_db("$db_name")or die("cannot select DB");

$datetime=date("y-m-d h:i:s"); //date time

$sql="INSERT INTO guestbook(Name, Email, Comment, Datetime)VALUES('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);

//check if query successful 
if($result){
echo "Successful";
echo "<BR>";

// link to view guestbook page
echo "<a href='viewguestbook.php'>View guestbook</a>";
}

else {
echo "ERROR";
}
mysql_close();
?>

guestbook.php

<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>Test Sign Guestbook </strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post" action="addguestbook.php">
<td>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><input name='name' type="text" id="name" size="40" /></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name='email' type="text" id="email" size="40" /></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><textarea name='comment' cols="40" rows="3" id="comment"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong><a href="viewguestbook.php">View Guestbook</a> </strong></td>
</tr>
</table>

&viewguestbook.php

<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>View Guestbook | <a href="guestbook.php">Sign Guestbook</a> </strong></td>
</tr>
</table>
<br>

<?php

$host="localhost"; // Host name 
$username="foo"; // Mysql username 
$password="bar"; // Mysql password 
$db_name="foo"; // Database name 
$tbl_name="guestbook"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server "); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>

<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td>ID</td>
<td>:</td>
<td><? echo $rows['id']; ?></td>
</tr>
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><? echo $rows['Name']; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><? echo $rows['Email']; ?></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><? echo $rows['Comment']; ?></td>
</tr>
<tr>
<td valign="top">Date/Time </td>
<td valign="top">:</td>
<td><? echo $rows['Datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>

<?php
}
mysql_close(); //close database
?>

viewguestbook.php 工作正常,只是除了日期之外没有数据实际上进入我的数据库..

【问题讨论】:

  • 您从未设置过$name 等,这意味着您可能假设register_globals 已打开。可怕的假设。 register_globals 是一个非常愚蠢的想法,不应该超越鸡尾酒餐巾纸的阶段,并且理所当然地被 PHP 淘汰了。它目前在被点燃之前被切成非常非常小的碎片。
  • 这也意味着你很容易受到sql injection attacks
  • 你的&lt;form&gt;从哪里开始?

标签: php mysql


【解决方案1】:

当您插入表单字段时,您不会检索它们:

$datetime=date("y-m-d h:i:s"); //date time
$sql="INSERT INTO guestbook(Name, Email, Comment, Datetime)VALUES('$name', '$email', '$comment', '$datetime')";

日期时间进入的唯一原因是因为您实际上首先得到它。您需要从表单中获取 $name、$email 和 $comment,例如:

$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];

在插入之前。附带说明一下,您的表格/表单的 HTML 无效。你不能只在表格中间注入一个表格。

【讨论】:

  • 谢谢!上帝我有时是个白痴,就是这样。现在都修好了,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
相关资源
最近更新 更多