mardi 16 février 2021

How can I check used data from other table?

I have a data which is registered in my first table, then it will be used and be registered in my second table.

My goal is this, I want to make the specific data which is registered in my first table unable to be modified or be editable inside my form "if" the specific data is already been registered in my second table.

What I did was to make a condition where I will check and compare if the specific data from my first table is equivalent to the specific data from my second table, and if it is equivalent it will show an error that will say data cannot be modified.

The problem is when I edit data from inside my form it is fine, it cannot be edited, but when I edit that specified data that I don't want to modified it updates.

Question is how will I trap it?

Here is my code in PHP

<?php 

include('../include/connect.php');

    if(isset($_POST['updateSpec']))
    {
        $id = $_POST['id'];
        $tools_id = $_POST['tools_id'];
        $update_model_num = mysqli_real_escape_string($con, strtoupper($_POST['update_model_num']));
        $update_sect_value = strtoupper($_POST['update_value']);
        $update_sect_days = strtoupper($_POST['update_num_days']);
        $update_stats = $_POST['status'];
        $update_reason = $_POST['reason_input_elem'];

        // var_dump($_POST);
        // exit();

        $_is_valid_query = "
SELECT r.tools_id AS toolsreg_id
     , s.tools_id as toolspec_id
     , s.model_num AS model_num
     , r.reg_tools_spec AS reg_model_num
  FROM tools_spec s
  LEFT 
  JOIN tools_registration r
    ON s.model_num = r.reg_tools_spec
 WHERE r.reg_tools_spec = '$update_model_num'
";
        $con->next_result();
        $is_valid_result = mysqli_query($con, $_is_valid_query);
        $rows = mysqli_fetch_assoc($is_valid_result);

        // $ts_id = $rows['toolspec_id'];
        $tr_id = $rows['toolsreg_id'];
        // $ts_model = $rows['model_num'];
        $tr_model = $rows['reg_model_num'];

        if(mysqli_num_rows($is_valid_result)>0)
        {
            echo $tools_id . " " . $tr_id;
            echo "<br>";
            if($tools_id == $tr_id)
            {
                if($update_model_num == $tr_model)
                {
                    echo $update_model_num . " and " . $tr_model;
                    echo "<script> $('#existing_modal').modal('show'); </script>";
                    exit();
                }
            }
        }
        
        else
        {
            
            $query = "
UPDATE tools_spec
   SET model_num = '$update_model_num'
     , model_num_val = '$update_sect_value'
     , no_of_days = '$update_sect_days'
     , status = '$update_stats'
     , reason_stats = '$update_reason'
 WHERE ID = '$id'
";
            
            $con->next_result();
            $result = mysqli_query($con, $query);

            if(!$result)
            {
                echo "error cannot update";
            }
            else
            {
                echo "<script> $('#update_ts_Modal').modal('show'); </script>";
            }
            // var_dump($query);
            // exit();
                   
        }
    }

?>

Here is the registered data in my first table

enter image description here

Here is the registered data from first table and used in second table

enter image description here

Here is my form when I will edit the used data

enter image description here

Whenever I change the input(Value, Calibration Schedule and Status), it is working, the data cannot be modified but if I tried changing the specified data that I want not to update named (Model No.), it is updating even if I have a comparison

Aucun commentaire:

Enregistrer un commentaire