Wednesday, 23 March 2016

prepared statement - Advanced MySQLi query syntax

I have a fairly complex mysqli insert statement to do, as I need to gather data from two other tables and also use some variables available to me in php all in one statement.



It's confusing me because of the order of the variables for the prepared statements. - I am only inserting four values, but in order to get the values for t1 and t2, I am referencing these three additional variables too in the bind_param statement.




I should be populating jobRoleID,companyID,departmentID,competenceID with $newJobRoleID,$newCompanyID,t1.id,t2.id



$addJobCompetencies = $con->prepare("
INSERT INTO jobRoleCompetencies (jobRoleID,companyID,departmentID,competenceID)
WITH t1 AS (SELECT id FROM departments WHERE companyID = ? AND department = ?),
t2 AS (SELECT id FROM competencies WHERE companyID = ?)
SELECT ?,?,t1.id,t2.id FROM t1,t2");

if($addJobCompetencies) {

$addJobCompetencies->bind_param('isiii', $newCompanyID,$row['department'],$newCompanyID,$newJobRoleID,$newCompanyID);
$addJobCompetencies->execute();
} else {
echo $con->error;
}
$addJobCompetencies->free_result();


I am getting a syntax error but I'm not 100% sure why. Can anyone help?




Thanks
Dan

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...