I have this query:
$stmt = $dbh->prepare("INSERT INTO `tap_sajobs` (
`ja_jid`,
`ja_reference`,
`ja_datePosted`,
`ja_dateUpdated`,
`ja_title`,
`ja_summary`,
`ja_description`,
`ja_location`,
`ja_email`,
`ja_url`,
`ja_salaryperiod`,
`ja_salarymin`,
`ja_salarymax`
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
The values are filled by the array $ja
. I can simulate the query with this:
$query = "INSERT INTO `tap_sajobs` (`ja_jid`,`ja_reference`,`ja_datePosted`,`ja_dateUpdated`,`ja_title`,`ja_summary`,`ja_description`,`ja_location`,`ja_email`,`ja_url`,`ja_salaryperiod`,`ja_salarymin`,`ja_salarymax`)
VALUES (".implode(",",$ja).");";
echo $query;
Which outputs the following:
INSERT INTO `tap_sajobs` (`ja_jid`,`ja_reference`,`ja_datePosted`,`ja_dateUpdated`,`ja_title`,`ja_summary`,`ja_description`,`ja_location`,`ja_email`,`ja_url`,`ja_salaryperiod`,`ja_salarymin`,`ja_salarymax`)
VALUES ('2049216', '2091046', '2015-07-23', '2015-08-20T06:49:30Z', 'Consultant', 'Will consider a graduate or a fully qualified fellow.', 'Test', 'No Location Set', 'test@foo.com', 'https://test.foo.com/3207/2049216/vh6zpeigfx6edggjrhshwulvvy?site=live', '0', '0', '0')
The actual code to execute the query is:
if(!$stmt->execute($ja)) {
print_r($stmt->errorInfo());
print_r($dbh->errorInfo());
exit("Error communicating with database.");
}
When executing the query via code, I see the error "Error communicating with database" and $dbh->errorInfo()
outputs:
Array ( [0] => 00000 [1] => [2] => ) 1
However, executing the query as outputted by the PHP code directly into phpMyAdmin executes without issue.
What is the source of this issue, or how can I find it?
SQLSTATE
code 00000
tells you that no error is occurred in the last executed statement. The following line will return 0
if no row is affected by the sql query.
$stmt->execute($ja);
So, you will go inside the if
block even if there are no errors, because !$stmt->execute($ja)
will be equal to 1
. Therefore to catch error, I think it is better to use,
$result = $stmt->execute($ja);
if($result == false) {
// put the error handling logic here.
}
You are executing "ja", not "query":
if(!$stmt->execute($ja))
Execute the query instead:
if(!$stmt->execute($query))
If
$query = "INSERT INTO `tap_sajobs` (`ja_jid`, `ja_reference`, `ja_datePosted`, `ja_dateUpdated`, `ja_title`, `ja_summary`, `ja_description`, `ja_location`, `ja_email`, `ja_url`, `ja_salaryperiod`, `ja_salarymin`, `ja_salarymax`)
VALUES (".implode(",", $ja).");";
echo $query;
yields
INSERT INTO `tap_sajobs` (`ja_jid`, `ja_reference`, `ja_datePosted`, `ja_dateUpdated`, `ja_title`, `ja_summary`, `ja_description`, `ja_location`, `ja_email`, `ja_url`, `ja_salaryperiod`, `ja_salarymin`, `ja_salarymax`)
VALUES ('2049216', '2091046', '2015-07-23', '2015-08-20T06:49:30Z', 'Consultant', 'Will consider a graduate or a fully qualified fellow.', 'Test', 'No Location Set', 'test@foo.com', 'https://test.foo.com/3207/2049216/vh6zpeigfx6edggjrhshwulvvy?site=live', '0', '0', '0')
then it seems that all the values in $ja
are already escaped, so when you use the array to prepare the statement, they would get double escaped, hence the error.
I think you have to delete quotes on your table
'tap_sajobs'
try this
tap_sajobs