geolaw
geolaw

Reputation: 452

PHP Insert query retuning success but not inserting data to the table

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

Upvotes: 0

Views: 3151

Answers (2)

geolaw
geolaw

Reputation: 452

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.

Upvotes: 1

Sadok Mtir
Sadok Mtir

Reputation: 615

So to execute a select query you need the query() function: PDO::query

And In order to perform CUD operations you need to pass by exec() function: PDO::exec

So here try to change if (!$stmt = $db->query($insertquery)) to if(!$results= $db->exec($insertquery))

Upvotes: 1

Related Questions