PHP插入查询重新调整成功,但未将数据插入表中 [英] PHP Insert query retuning success but not inserting data to the table

查看:65
本文介绍了PHP插入查询重新调整成功,但未将数据插入表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将代码移植到的数据库上的插入查询时遇到问题.首先,我必须将所有内容从mysqli更改为PDO,从而解决了除此以外的所有问题.

我尝试使用准备好的语句,我尝试对输入值进行硬编码.我还验证了权限不是问题,那还会返回我认为的失败?

此查询始终对所有条目返回成功,但不向数据库输入任何内容,但是如果我已经在其中找到一行,那么我有一个用相同脚本编写的更新查询,所以我知道变量已被填充. >

我对可能导致问题的原因不知所措

下面是代码:

$currKey = 1;

             foreach ($answer as $a)
            {
                $insertquery = "INSERT INTO table(userid, step_number, question_number, answer) VALUES ($userid, $stepNumber, $currKey, '$a')";

                if (!$stmt = $db->query($insertquery))
                {
                    $response = array();
                    $response["success"] = 0;
                    $response["message"] = "Error at insert query";
                }
                else
                {
                    $response = array();
                    $response["success"] = 1;
                    $resppnse["message"] = "success";

                    $currKey += 1;
                }
            }


            echo json_encode($response);

解决方案

这是一个多管齐下的问题,一个人可能永远都不会拥有自己,但是希望这会给人们一些工作时需要考虑的事情和动议在这种情况下.

第一个主要问题是我的文本编辑器决定进行罢工.这意味着无论我尝试调试如何,都没有任何反应.

接下来,一旦我移到另一台机器上继续工作.我设法分辨出$rows = $stmt->fetchAll()总是> 0,但返回布尔值,与mysqli中的$stmt->get_result->fetch_assoc()不同,后者返回一个数组,因此我可以得到一个计数.我意识到自己的插入查询从未被击中(请使用良好的描述性调试消息),从而弄清楚了这一点.

之后,我更改了"INSERT INTO table(userid, step_number, question_number, answer) VALUES ($userid, $stepNumber, $currKey, '$a')";"INSERT INTO table(userid, step_number, question_number, answer) VALUES ('$userid', '$stepNumber', '$currKey', '$a')";,我们就可以了.

感谢所有的帮助,这真是令人沮丧的2天.

I'm having issues with insert queries on a database that I am porting my code over to. I have first of all had to change everything from mysqli to PDO which fixed every problem except for this one.

I have tried using prepared statements, I have tried hardcoding the entry values. I have also verified that permissions are not the issue, plus that would return a fail I assume?

This query always returns success on all entries but does not enter anything into the database, however I have an update query written in the same script if I find a row already there so I know the variables are being populated.

I am at a loss for what could be causing the issue

here is the code:

$currKey = 1;

             foreach ($answer as $a)
            {
                $insertquery = "INSERT INTO table(userid, step_number, question_number, answer) VALUES ($userid, $stepNumber, $currKey, '$a')";

                if (!$stmt = $db->query($insertquery))
                {
                    $response = array();
                    $response["success"] = 0;
                    $response["message"] = "Error at insert query";
                }
                else
                {
                    $response = array();
                    $response["success"] = 1;
                    $resppnse["message"] = "success";

                    $currKey += 1;
                }
            }


            echo json_encode($response);

解决方案

This is a multi-pronged issue and one people will probably never have themselves, but hopefully this will give people some things to consider and motions to go through when working with this kind of situation.

So the first major issue is that my text editor decided to go on strike. Meaning no matter what I did in an attempt to debug, nothing was happening.

Next, once I had moved over to another machine to continue my work. I had managed to discern that $rows = $stmt->fetchAll() will always be > 0 but returns a bool, unlike $stmt->get_result->fetch_assoc() from mysqli which returns an array and therefore I can get a count. I figured this out by realizing that my insert query was never being hit (please use good and descriptive debug messages).

After that I changed "INSERT INTO table(userid, step_number, question_number, answer) VALUES ($userid, $stepNumber, $currKey, '$a')"; to "INSERT INTO table(userid, step_number, question_number, answer) VALUES ('$userid', '$stepNumber', '$currKey', '$a')"; and there we have it.

Thanks to all that helped, this has been a frustrating 2 days.

这篇关于PHP插入查询重新调整成功,但未将数据插入表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆