Edit and Update Dropdown Value in PHP & MySQL

In this tutorial, You will learn to edit and update dropdown value in PHP & MySQL with some simple steps. These steps are very easy to understand and implement in web applications.

Here, I have shared source code to edit and update values of a single dropdown select option with a single text input. Once you learn it, you will easily customize it according to your project requirement.

php edit and update dropdown

Edit and Update Select Option Value in PHP & MySQL

Before getting started it’s coding, you should create the following folder structure –

codingstatus/
     |__ database.php
     |__ edit-button.php
     |__ fetch-script.php
     |__ edit-form.php
     |__ edit-script.php
     |__ update-script.php

Also, Insert select option values into database and then proceed the following steps –

2. Connect to MySQL Database

To edit and update dropdown value, you must connect PHP to MySQL database with the help of the following query.

Where –

  • $hostName – It must contain hostname.
  • $userName – It must contain username of the database.
  • $password – It must contain password of the database
  • $database – It must contain database name.

File Name – database.php

<?php
$hostName = "localhost";
$userName = "root";
$password = "";
$databaseName = "codingstatus";
 $conn = new mysqli($hostName, $userName, $password, $databaseName);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
?>

3. Fetch values from the database

In this step, Write MySQL query to fetch id, course name & full name from the “students” table to display them in a HTML table.

File Name – fetch-script.php

<?php 
   $query ="SELECT id, fullName, courseName FROM students";
   $result = $conn->query($query);
   if($result->num_rows> 0){
     $options= mysqli_fetch_all($result, MYSQLI_ASSOC);
   }else{
    $options=[];
   }
 ?>

4. Display values with Edit Button

To display values with edit button, you have to follow the following steps –

Step-1: Create a HTML table with column S.N, Full Name, Course Name & edit

Step-2: Include database.php and fetch-script.php file

Step-3: check total number of records using count() method. if it is greater then zero then implement the next step within the if block of statement

Step-4: Apply foreach loop to the $options and print the value of full name, course name & id

File Name – edit-button.php

 <?php
 include("database.php");
 include("fetch-script.php");
 ?>
<h3>Edit and Update Dropdown Value in PHP & MySQL</h3>
 <table border="1" cellspacing="0" cellpadding="5" width="40%">
    <thead>
    	<tr>
    	 <th>S.N</th>
         <th>Full Name</th>
         <th>Course Name</th>
    	 <th>Edit</th>
      </tr>
    </thead>
    	<tbody>
 <?php 
    if(count($options)> 0){
    	$sn=1;
        foreach ($options as $option) {
        
    ?>
   <tr>
     <td><?php echo $sn; ?></td>
     <td><?php echo $option['fullName']; ?></td>
     <td><?php echo $option['courseName']; ?></td>
     <td><a href="edit-form.php?id=<?php echo $option['id']; ?>">Edit</a></td>
    </tr>
    <?php 
   $sn++; }}
    ?>
    </tbody>
 </table>

5. Fetch values Based on Id

When you click the edit button then you will get a id of the current record. Now, We will fetch values from the database based on this id by using the following query –

File Name – edit-script.php

<?php
if(isset($_GET['id']) && !empty($_GET['id'])){
    $editId= $_GET['id'];
    $query ="SELECT fullName, courseName FROM students WHERE id='$editId'";
    $result = $conn->query($query);
    $editData=$result->fetch_assoc();
    $fullName= $editData['fullName'];
    $courseName= $editData['courseName'];
}

?

6. Display values  in Edit Form

When you will the edit button, It will redirect to the edit-form.php with a id of the current records. So, We have to display values in the edit form based on the getting id from the edit button.

Step-1: Include the database.php, edit-scritp.php & update-script.php

Step-2: Create a HTML form with select option &  a text input field

Step-3: Display data from the database in the select option and set the selected options value

File Name – edit-form.php

<?php
include("database.php");
include("edit-script.php");
include("update-script.php");

?>
<form action="" method="post">
<input type="text" name="fullName" value="<?php echo $fullName; ?>">
<select name="courseName">
    <option value="">Select Course</option>
    <?php 
    $query ="SELECT courseName FROM courses";
    $result = $conn->query($query);
    if($result->num_rows> 0){
        while($optionData=$result->fetch_assoc()){
        $option =$optionData['courseName'];
    ?>
    <?php
    if(!empty($courseName) && $courseName== $option){
    ?>
    <option value="<?php echo $option; ?>" selected><?php echo $option; ?> </option>
    <?php 
continue;
   }?>
    <option value="<?php echo $option; ?>" ><?php echo $option; ?> </option>
   <?php

    }}
    ?>
</select>
<input type="submit" name="update">
</form>


7. Updated Values based on Id

After change the value of select option and input field, you can update values by submitting the form. But to update values, you will have to write the following MySQL Update Query.

File Name – update-script.php

<?php 

/// update data
if(isset($_POST['update']) && !empty($_GET['id'])){

    $fullName = $_POST['fullName'];
    $courseName = $_POST['courseName'];
    $editId = $_GET['id'];
    if(!empty($courseName)){
      $query = "UPDATE students 

      SET fullName ='$fullName', 
       courseName ='$courseName' 
                WHERE id ='$editId'";
      $result = $conn->query($query);
      if($result){
        header("location:display-value.php");
      } 
    }
  }

?>