php - update page - update all records with the same data

I've small php project to list Covid-Test Labs, When you press the Update button for one of the records in the list of records, the data modification window opens, and when you enter it, the entries of other records are updated with the same information

How to solve this issue ?

records list Update one record

    <!-- Update Form -->
    <form  style = "font-size : 30px">
   

      <input type="text" id="lab_name" class="fadeIn second" name="lab_name" placeholder="Laboratory Name" required>
      <input type="text" id="test_price" class="fadeIn second" name="test_price" placeholder="Test Price" required>
      <input type="text" id="lat" class="fadeIn second" name="lat" placeholder="Lat" required>
      <input type="text" id="lon" class="fadeIn second" name="lon" placeholder="Lon" required>
      <div class="dropdown">
            <select name="lab_city" class="dropdown" required>
                <option value="">--- Select City---</option>
                <option  value="Khartoum">Dubai</option>
                 <option  value="Bahri">Bahri</option>
                 <option value="Omdurman">Paris</option>
               </select>

               <input type="file" name="image" id="image" required>
         
      </div>




      <input type="submit" id="submit" class="fadeIn fourth" value="Update">

   

  </div>
</div>

</form>
        

<section>
  
<?php
  $servername = "localhost";
$username = "root";
$password = "";
$dbname = "covidsd";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}


$lab_id = $_GET['lab_id'];
$lab_name= mysqli_real_escape_string($link, $_REQUEST['lab_name']);


  $lab_city = mysqli_real_escape_string($link, $_REQUEST['lab_city']);

  $test_price= mysqli_real_escape_string($link, $_REQUEST['test_price']);
  $lat= mysqli_real_escape_string($link, $_REQUEST['lat']);
  $lon= mysqli_real_escape_string($link, $_REQUEST['lon']);
  $image= mysqli_real_escape_string($link, $_REQUEST['image']);



//$ServerURL = "http://192.168.43.236/AndroidUploadImage/$ImagePath";

  $sql = "UPDATE laboratories SET lab_name ='$lab_name',lab_city ='$lab_city', test_price= '$test_price', lat='$lat',  lon= '$lon', image= '$image' ". "WHERE lab_id='$lab_id' ";

if(mysqli_query($link, $sql)){

    $message = "Laboratory has been updated successfully";
 echo "<script type='text/javascript'>alert('$message');</script>";
  } else{

      echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

  }

   

  // close connection

  mysqli_close($link);

?>
 

I tried many solutions but not working

Answer

Solution:

Each record in your table must have a unique id field. You will retrieve this field when loading your list. When modifying a record, you will send that id field again to PHP. Inside PHP you will use it in your SQL WHERE IdField=$ID.

You are using variable $lab_id In your PHP code $lab_id = $_GET['lab_id']; and this means that you have an input on your form with name lab_id. I can't see that input it in your HTML so you need to add it like that :

<form  style = "font-size : 30px">

  <input type="hidden" id="lab_id" name="lab_id" Value = "$lab_id"> 

  <input type="text" id="lab_name" class="fadeIn second" name="lab_name" placeholder="Laboratory Name" required>
...............
...............

</form>

In your PHP code you must decide what kind of request you will accept either POST or GET. Now you are using a mix of $_GET and $_REQUEST. If you are modifying or inserting data, it will better to use $_POST. Getting variables is PHP code will looks like :

$lab_id = $_POST['lab_id'];
$lab_name= mysqli_real_escape_string($link, $_POST['lab_name']);

If you are using a framework which do the request, you can check it to make sure if it is sending lab_id, if it sends lab_id you will not need it inside form. You may also adjust the request type in your framework if you are using a one.

Source