Php bind_param()问题
我发现bind_param函数有问题。我将在下面发布所有信息 错误:Php bind_param()问题,php,mysql,Php,Mysql,我发现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 88 MySQL错误: You have an error in your SQL syntax; check the manual that corresponds to you
Fatal error: Call to a member function bind_param() on a non-object in /home4/lunar/public_html/casino/blogpost.php on line 88
MySQL错误:
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 />";
}
你知道为什么会这样吗?正如火箭危险品提到的,你只能用问号作为绑定参数的占位符。 你应该做类似的事情:
$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);
更多详细信息:正如火箭危险品所述,您只能使用问号作为绑定参数的占位符。 你应该做类似的事情:
$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);
更多详细信息:$stmt->bind_参数、$user、$title、$message、$image、$category; 在第一个参数上,s=string和i=integer。您需要指定要添加到数据库中的值的类型。如果要向数据库中添加5个字符串值,则写“sssss”。如果要插入5个整数,则写“iiiii”。如果有一些整数值和一些字符串值,则可以相应地进行调整 //因此,如果您的值都是字符串,那么这是正确的: $stmt->bind_paramssss、$user、$title、$message、$image、$category //因此,如果您的值都是整数,则这是正确的: $stmt->bind_paramiiii,$user,$title,$message,$image,$category //如果前2个是整数,其余3个是字符串,则这是正确的: $stmt->bind_paramiisss、$user、$title、$message、$image、$category
等等。$stmt->bind_参数、$user、$title、$message、$image、$category; 在第一个参数上,s=string和i=integer。您需要指定要添加到数据库中的值的类型。如果要向数据库中添加5个字符串值,则写“sssss”。如果要插入5个整数,则写“iiiii”。如果有一些整数值和一些字符串值,则可以相应地进行调整 //因此,如果您的值都是字符串,那么这是正确的: $stmt->bind_paramssss、$user、$title、$message、$image、$category //因此,如果您的值都是整数,则这是正确的: $stmt->bind_paramiiii,$user,$title,$message,$image,$category //如果前2个是整数,其余3个是字符串,则这是正确的: $stmt->bind_paramiisss、$user、$title、$message、$image、$category
等等。MySQLi不支持命名参数。如果要使用:name,则需要使用PDO。MySQLi只支持?。是的,我在网上四处寻找这个问题,我看到很多人在使用?。那么,我如何将我的PDO版本转换为一个工作的mysqli版本呢?如果你把它放在一个答案中,并且它起作用,我很乐意接受:MySQLi不支持命名参数。如果要使用:name,则需要使用PDO。MySQLi只支持?。是的,我在网上四处寻找这个问题,我看到很多人在使用?。那么,我如何将我的PDO版本转换为一个工作的mysqli版本呢?如果你把它写在一个答案中,并且它起作用,我很乐意接受: