我遇到了bind_param函数的问题。我将张贴以下所有信息。
错误:
Fatal error: Call to a member function bind_param() on a non-object in /home4/lunar/public_html/casino/blogpost.php on line 88MySQL错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':user, :title, :message, :image, :category, NOW())' at line 1查询:
$user = $_COOKIE['user'];
$title = $_POST['title'];
$message = $_POST['message'];
$image = $_POST['image'];
$category = $_POST['category'];
$stmt = $mysqli->prepare("INSERT INTO `lunar_casino`.`posts` (`id`, `by`, `title`, `message`, `image`, `category`, `date`) VALUES(NULL, :user, :title, :message, :image, :category, NOW())");
echo $mysqli->error;
$stmt->bind_param(":user", $user);
$stmt->bind_param(":title", $title);
$stmt->bind_param(":message", $message);
$stmt->bind_param(":image", $image);
$stmt->bind_param(":category", $category);
$stmt->execute();
if(!$stmt){
echo "<font color='red'><b>There has been an error with our database! Please contact the website administrator!</b></font><br /><br />";
echo $mysqli->error;
} else {
echo "<font color='green'><b>You have successfully added a blog post!</b></font><br /><br />";
}知道为什么会这样吗?
发布于 2014-01-29 22:03:18
正如火箭黑泽马特所提到的,你只能使用问号作为绑定参数,地点持有人。你应该做一些类似的事情:
$stmt = $mysqli->prepare("INSERT INTO `lunar_casino`.`posts` (`id`, `by`, `title`, `message`, `image`, `category`, `date`) VALUES(NULL, ?, ?, ?, ?, ?, NOW())");
$stmt->bind_param("sssss", $user, $title, $message, $image, $category);更多细节:http://www.php.net/manual/en/mysqli-stmt.bind-param.php
发布于 2014-01-30 02:22:44
$stmt->bind_param(“ssss”,$user,$title,$message,$image,$category);在第一个参数中,s= string和i=整数。您需要指定要添加到数据库的值的类型。如果要向数据库中添加5个字符串值,那么如果要插入5个整数,则编写'sssss‘,然后编写'iiiii’,如果您有一些整数值和一些字符串值,则可以相应地进行调整。
//如果您的值都是字符串,那么这将是正确的:$stmt->bind_param(“ssss”,$user,$title,$message,$image,$category);
//如果您的值都是整数,那么这将是正确的:$stmt->bind_param("iiiii",$user,$title,$message,$image,$category);
//如果前两个是整数,其他三个字符串则是正确的:$stmt->bind_param("iisss",$user,$title,$message,$image,$category);
诸若此类。
https://stackoverflow.com/questions/21443529
复制相似问题