douyingbei1458 2013-12-02 18:37
浏览 59
已采纳

Mysqli INSERT未显示在数据库中且没有错误

I'm trying to insert this information into my database, I have practically the same code setup for SELECT queries and they work. Any INSERT queries I try will not work.

This function returns true, and I've echoed the query this function creates and have used PHPMyAdmin to run the SQL query where it'll show in the database but when doing it through this php code the INSERT never shows up in the database.

EDIT : I noticed that one of my columns (orderID) that is set to auto increment IS increasing every time I run this function but for whatever reason the query is not actually getting placed into the table.

This Image shows orderID of 29 is missing, I got those table results by doing 2 PHPMyAdmin INSERT queries, then one with the function and one more with PHPMyAdmin. Is there any way my function could be deleting the insert?

function add_to_cart($user_id, $product_id, $quantity) {
global $mysqli;
    if($result = $mysqli->query("INSERT INTO orders (userID, productID, quantity, orderDate) VALUES ('$user_id', '$product_id', '$quantity', NOW())")){
         return $result;
    }

return false;
}

The user connecting to the database has full privileges to the database so that shouldn't be the issue

  • 写回答

2条回答 默认 最新

  • drasebt1835 2013-12-03 03:14
    关注

    I was able to fix the problem by adding $mysqli->commit(); after $mysqli->query()

    I needed to do this because I changed the default options and had "SET AUTOCOMMIT = 0"

    Taking that out allowed me to remove $mysqli->commit()

    function add_to_cart($user_id, $product_id, $quantity) {
        global $mysqli;
        if($result = $mysqli->query("INSERT INTO orders (userID, productID, quantity, orderDate) VALUES ($user_id, $product_id, $quantity, NOW())")){
            if (!$mysqli->commit()) {
                print("Transaction commit failed
    ");
            }
            return $result;
        }
        die($mysqli->error());
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化