Your Bub
Your Bub

Reputation: 43

PHP MySQL - Check if a specifc column was updated

I am trying to see the best approach for this scenario - i want to send an email alert whenever a user updates a specific column. The column name is rep. If the rep column isnt updated, do not send an email.

Here's my attempt:

    <?php

    include_once("connection.php");

    if(isset($_POST['update'])) {   
        $id = mysqli_real_escape_string($mysqli, $_POST['record_update']);
        $record_update = mysqli_real_escape_string($mysqli, $_POST['record_update']);
        $comment = mysqli_real_escape_string($mysqli, $_POST['comment']);
        $status = mysqli_real_escape_string($mysqli, $_POST['status']);
        $rt = mysqli_real_escape_string($mysqli, $_POST['rt']);
        $reason = mysqli_real_escape_string($mysqli, $_POST['reason']);
        $username = mysqli_real_escape_string($mysqli, $_POST['username']);
        $rep = mysqli_real_escape_string($mysqli, $_POST['rep']);


       if(empty($record_update)  ) {

        if(empty($record_update)) {
            echo "<script type='text/javascript'>alert('Date/Time field is blank.');window.location.href='dashboard.php';</script>";
        }


} else { 

      $result = mysqli_query($mysqli, "UPDATE employee SET record_update='$record_update', comment='$comment', status='$status', rt='$rt', reason='$reason', username='$username', rep='$rep'  WHERE id='$id'");

      if($rep->(success() == true)) {

         //do email
      } 




    }
 ?>

so would it look like this?

    <?php

    include_once("connection.php");

    if(isset($_POST['update'])) {   
        $id = mysqli_real_escape_string($mysqli, $_POST['record_update']);
        $record_update = mysqli_real_escape_string($mysqli, $_POST['record_update']);
        $comment = mysqli_real_escape_string($mysqli, $_POST['comment']);
        $status = mysqli_real_escape_string($mysqli, $_POST['status']);
        $rt = mysqli_real_escape_string($mysqli, $_POST['rt']);
        $reason = mysqli_real_escape_string($mysqli, $_POST['reason']);
        $username = mysqli_real_escape_string($mysqli, $_POST['username']);
        $rep = mysqli_real_escape_string($mysqli, $_POST['rep']);


       if(empty($record_update)  ) {

        if(empty($record_update)) {
            echo "<script type='text/javascript'>alert('Date/Time field is blank.');window.location.href='dashboard.php';</script>";
        }


} else { 


    $query = mysqli_query($mysqli, "SELECT rep FROM employee WHERE id='$id'");
    $row = $query->fetch_assoc()[0];
    if($row['rep'] != $_POST['rep']) {
        //do nothing
    } else {
       //do email
    }



      $result = mysqli_query($mysqli, "UPDATE employee SET record_update='$record_update', comment='$comment', status='$status', rt='$rt', reason='$reason', username='$username', rep='$rep'  WHERE id='$id'");






    }

?>

Upvotes: 2

Views: 1956

Answers (2)

S4NDM4N
S4NDM4N

Reputation: 924

This might not be the best answer but I like to suggest that you capture the date and time of the first insert and then the update record them in a table columns and the compare the time or both when an update happens to the same data row.

$query = mysqli_query($mysqli, "SELECT time, date FROM employee WHERE id='$id'");
$row = $query->fetch_assoc()[0];
if($row['time'] > $_POST['time'] || $row['date'] > $_POST['date'])
  $record_update = true;

Upvotes: 0

N. Hamelink
N. Hamelink

Reputation: 603

Select the current value, and compare it to the inserted value, if it's different it needs to be updated?

$query = mysqli_query($mysqli, "SELECT rep FROM employee WHERE id='$id'");
$row = $query->fetch_assoc()[0];
if($row['rep'] != $_POST['rep'])
  $record_update = true;

Upvotes: 2

Related Questions