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 关于#python#的问题,请各位专家解答!
  • ¥20 Hbase启动失败,无法启动HMaster
  • ¥15 sqpingboot在父模块已经引入了mybatisplus 在子组件不写版本号还是报错
  • ¥20 Lumerical FDTD solutions 中模型的相对阻抗,有效介电常数和有效磁导率的实部和虚部的数据如何获得?
  • ¥100 sql reporting service 远程smtp服务器配置支持
  • ¥15 ppyoloe_r带角度目标检测,loss_cls没法收敛
  • ¥15 淘宝交易指数如何解读,其关联的数据指标是什么
  • ¥15 Linux下安装搜狗拼音不能显示中文
  • ¥15 事件7000导致的自动重启如何解决
  • ¥15 (标签-matlab|关键词-m3)