首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >bind_param()问题

bind_param()问题
EN

Stack Overflow用户
提问于 2014-01-29 21:57:11
回答 2查看 738关注 0票数 1

我遇到了bind_param函数的问题。我将张贴以下所有信息。

错误:

代码语言:javascript
复制
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错误:

代码语言:javascript
复制
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

查询:

代码语言:javascript
复制
    $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 />";
    }

知道为什么会这样吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-29 22:03:18

正如火箭黑泽马特所提到的,你只能使用问号作为绑定参数,地点持有人。你应该做一些类似的事情:

代码语言:javascript
复制
 $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

票数 6
EN

Stack Overflow用户

发布于 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);

诸若此类。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21443529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档