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条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算