I'm quite new to PHP and MYSQL.
I'm looking for the right way to write the code for a query on which I want to UPDATE a couple of values on a table, and then UPDATE another value on another table with an if statement.
Here is what I've tried:
public function updateSubmission($data){
$this->db->query('UPDATE submissions S, requests R
SET S.submission_title = :submission_title,
S.submission_area = :submission_area,
S.submission_link = :submission_link,
S.submission_citations = :submission_citations,
S.submission_status = :submission_status,
S.submission_updatedby = :submission_updatedby
WHERE S.submission_id = :submission_id;
UPDATE requests R
SET R.request_status = IF(R.request_status = "review2" AND :submission_status = "cancelled", "submission" )
WHERE R.request_id = :submission_requestid;
');
$this->db->bind(':submission_title', $data['submission_title']);
$this->db->bind(':submission_area', $data['submission_area']);
$this->db->bind(':submission_link', $data['submission_link']);
$this->db->bind(':submission_citations', $data['submission_citations']);
$this->db->bind(':submission_status', $data['submission_status']);
$this->db->bind(':submission_updatedby', $_SESSION['user_id']);
$this->db->bind(':submission_id', $data['submission_id']);
$this->db->bind(':submission_requestid', $data['submission_requestid']);
if($this->db->execute()){
return true;
} else {
return false;
}
}
First UPDATE part works, but second UPDATE part doesn't make any changes. I thought using UPDATE twice in a single query is not working. So I've tried another way:
$this->db->query('UPDATE submissions S, requests R
SET S.submission_title = :submission_title,
S.submission_area = :submission_area,
S.submission_link = :submission_link,
S.submission_citations = :submission_citations,
S.submission_status = :submission_status,
S.submission_updatedby = :submission_updatedby
R.request_status = IF(R.request_status = "review2" AND :submission_status = "cancelled", "submission")
WHERE S.submission_id = :submission_id AND R.request_id = :submission_requestid
');
This also didn't work.
I basically want to update a table and depending on this update, if the new S.submission_status = "cancelled" and at the same time if the R.request_status is "review2", I want to change the R.request_status to "submission".
I have a solution of using two separate queries but I just wanted to know if it is possible on one query because I wanted to get only one feedback from the function and avoid the database writing error possibility of one of the queries.
I'm using a framework and below is the database.php
<?php
/*
PDO Database Class
Connect to database
Create prepared statements
Bind values
Return rows and results
*/
class Database{
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
//private $dbport = DB_PORT;
private $dbh;
private $stmt;
private $error;
public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname . ';charset=utf8';
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create PDO instance
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
} catch(PDOException $e){
$this->error = $e->getMessage();
echo $this->error;
}
}
// Prepare statement with query
public function query($sql){
$this->stmt = $this->dbh->prepare($sql);
}
// Bind values
public function bind($param, $value, $type = null){
if(is_null($type)){
switch(true){
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
// Execute the prepared statement
public function execute(){
return $this->stmt->execute();
}
// Get result set as array of objects
public function resultSet(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_OBJ);
}
// Get single record as object
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_OBJ);
}
// Get row count
public function rowCount(){
return $this->stmt->rowCount();
}
// Returns the last inserted ID
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
}
?>
Any help is appreciated. Thanks