I’m trying to get a query to run if a variable changes
I have one table (review) and if the worker record changes, another table (worker) which has a review_count field and id like to increment it by 1, like…
<?php
$id = $_POST['id'];
$r_date = date('Y-m-d', strtotime(str_replace('-', '/', $_POST['r_date'])));
$p_id = $_POST['p_id'];
$rating = $_POST['rating'];
$review = $_POST['review'];
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$sql = "UPDATE review SET
r_date = :r_date,
p_id = :p_id,
rating = :rating,
review = :review,
WHERE id = :id";
//run query on worker table to add 1 to review
//count field where p_id is the primary key of the worker table
$stmt = $dbh->prepare($sql);
$stmt->execute(array(
id => $id,
:r_date => $r_date,
:p_id => $p_id,
:rating => $rating,
:review => $review,
));
header("Location: reviews.php");
exit;
} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
?>
$sql = "
SELECT count(*)
FROM worker
WHERE p_id = :id -- there is no == in MySql
";
$result = $con->prepare($sql);
$result->execute(array(':id' => $id));
$number_of_rows = $result->fetchColumn();
echo $num_of_rows;