This is not a duplicate question. The previous question is nothing to do with PDO. I have two tables in mysql:
USERS
-------------------------------------
employeeid | name | saving | salary
-------------------------------------
12 | Bob | 100 | 1000
23 | Joe | 50 | 800
USERS table
employeeid
name
saving
salary
and:
EMPLOYEE
-----------------------------------
id | managerid | workerid
-----------------------------------
1 | 12 | 23
EMPLOYEE table
id
managerid FOREIGN KEY
workerid FOREIGN KEY
1- (both manager and worker are employee) in order to update the saving field for a workerid (say with +$10), field salary needs to be updated by -$10
2- input variable comes from PHP form as name
, so the logical flow is:
name > find employeeid (id) from USERS > find managerid (id2) from EMPLOYEE > find employeeid (id3) from USERS > update saving and salary
so the sql statement separately can be written as:
id = SELECT employeeid FROM USERS WHERE name = $name; //find id of employee in USERS
id2 = SELECT managerid FROM EMPLOYEE WHERE workerid = id; //find id of worker in EMPLOYEE
UPDATE USERS SET saving = saving + 10, salary = salary -10 WHERE employeeid = id2;
is it possible to do these 3 statements in one (in PDO format). msql PDO format of above (with PHP):
$sql = "SELECT employeeid FROM USERS WHERE name=:namepara";
$sttm = prepare($sql);
$sttm->execute(array(":namepara"=>$name));
$row=$sttm->fetch(PDO::FETCH_ASSOC);
$sql2 = "SELECT managerid FROM EMPOYEE WHERE workerid=:idpara";
$sttm2 = prepare($sql2);
$sttm2->execute(array(":idpara"=>$row['employeeid']));
$row2=$sttm2->fetch(PDO::FETCH_ASSOC);
$sql3 = "UPDATE USERS SET saving = saving + 10, salary = salary - 10 WHERE
employeeid=:id2para";
$sttm3 = prepare($sql3);
$sttm3->execute(array(":id2para"=>$row2['managerid']));
$row3=$sttm3->fetch(PDO::FETCH_ASSOC);
Any help would be appreciated!