douzhuangxuan3268 2018-04-14 21:34
浏览 200
已采纳

php预处理语句抛出null错误的问题

The issue I am having appears to be an error with the way i'm creating my prepared sql statement to insert some data. My first thought was that something was null, but I know the data is there because i've tested it with an echo. My thought is it has to be with the way the statement is written but I can't figure out what could be wrong with it.

Code:

// Set up the sql command
    $stmt = $mysqli->prepare("INSERT INTO tblcomments (imgpath, author, comment) VALUES(?, ?, ?)");
    // Bind the data
    $stmt->bind_param("imgpath", $imagePath);
    $stmt->bind_param("author", $author);
    $stmt->bind_param("comment", $comment);
    // Execute the sql
    $stmt->execute();
    // Close the statement
    $stmt->close();

The issue is the insert statement itself, at least according to the error, which is:

Fatal error: Uncaught Error: Call to a member function prepare() on null in D:\Inetpub\vhosts\kreativecoating.com\httpdocs\commentDataPostHandler.php:55 Stack trace: #0 {main} thrown in D:\Inetpub\vhosts\kreativecoating.com\httpdocs\commentDataPostHandler.php on line 55

Line 55 being the one with the insert statement. So my question is am I doing something wrong with the prepared statement? I've checked the database fields for typing errors so it isn't that. There is an id field not listed in the sql but thats the key and auto increments, so it shouldn't need to be there

  • 写回答

2条回答 默认 最新

  • dousi7579 2018-04-14 23:09
    关注

    Bind param is all at once.

    bind_param("ssi", $string, $string, $number);
    

    S for string

    i for integer

    d for float / decimal number

    I made a query Builder that actually generates all the code. Just plugin in column names. It helps with prepared queries :)

    https://wbr.bz/QueryPro

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题