0

当我通过 php 表单提交留言簿详细信息时,它以“成功”结尾,但是当我查看 MySQL 数据库时,它只是创建了新条目,但除了自动 ID 和日期之外,没有任何详细信息。

这是表格:guestbook.php

 <form id="form1" name="form1" method="post" action="addguestbook.php">
 <td><input name="name" type="text" id="name" size="40" /></td>
 <td><input name="email" type="text" id="email" size="40" /></td>
 <td><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td>
 <td><input type="submit" name="Submit" value="Submit" /> <input type="reset" 
 name="Submit2" value="Reset" /></td>

这是脚本:addguestbook.php

 $host="address"; // Host name 
 $username="username"; // Mysql username 
 $password="password"; // Mysql password 
 $db_name="guestdb"; // 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 $tbl_name(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();
4

3 回答 3

4

在准备 sql 查询 ( $sql) 之前,您必须使用数据填充$name,$email$commentvars $_POST

<?php

// ..

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

$sql = "INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email',     
'$comment', '$datetime')";
$result = mysql_query($sql);

// ..

奖励:使用PDO

于 2012-11-14T15:03:14.300 回答
2

我可以看到,你还没有定义你的变量..在上面添加这个$datetime=da...

$name = isset($_POST['name']) ? $_POST['name'] : false;
$email = isset($_POST['email']) ? $_POST['email'] : false;
$comment = isset($_POST['comment']) ? $_POST['comment'] : false;

除此之外:

于 2012-11-14T15:03:47.903 回答
1

这可能有多种原因。

我建议将您的查询更改为:

INSERT INTO $tbl_name set name='$name', email='$email', comment='$comment', datetime='$datetime'
于 2012-11-14T15:06:12.177 回答