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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵