我正在尝试通过网站在我的留言簿上添加一些东西。但问题是:我无法插入到数据库中。使用我的WAMP服务器,我可以使用下面的查询直接插入到其中。
我还测试了$name变量,它们都被正确地填充了。我换了$con和$sql。并且我尝试以不同的方式格式化查询,不使用{}和{}。但似乎没有一个选择是可行的。与我的DBS的连接工作正常(我在这里没有包括这一部分)。我试过mysql_query和mysqlI_query。
我的WAMP服务器启用了php_mysqli。当我运行它时,它没有给出任何错误。
你们知道我做错了什么吗?
$con = mysqli_connect($host, $username, $password)or die("cannot connect");
mysqli_select_db($con, $db_name)or die("cannot select DB");
$datetime=date("y-m-d h:i:s"); //date time
$name= $_POST['name'];
$email= $_POST['email'];
$comment= $_POST['comment'];
$sql="INSERT INTO guestbook(name, email, comment, datetime) VALUES('{$name}','${email}','{$comment}','{$datetime}')";
mysql_query($sql,$con);//查看留言簿页眉链接(‘location:viewguestbook.php’);
发布于 2015-03-01 19:32:00
请尝试这样做,下次您必须搜索mysqli预准备语句。
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$mysqli->query("insert...");发布于 2015-03-01 20:23:29
here is the problem in this statement
mysql_query($sql,$con);
problem 1: you are using mysqli and in above statement your are querying mysql.
problem 2: $con(connection variable is must be the first parameter)
Write this statement as folloing. that will solve the problem
mysqli_query($con,$sql);发布于 2015-03-01 19:34:24
我不知道你得到了什么错误。您可以这样做:
mysqli_query($con, $query) or die(mysqli_error($con));https://stackoverflow.com/questions/28792996
复制相似问题