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 –

     |__ 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

$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

   $query ="SELECT id, fullName, courseName FROM students";
   $result = $conn->query($query);
   if($result->num_rows> 0){
     $options= mysqli_fetch_all($result, MYSQLI_ASSOC);

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

<h3>Edit and Update Dropdown Value in PHP & MySQL</h3>
 <table border="1" cellspacing="0" cellpadding="5" width="40%">
         <th>Full Name</th>
         <th>Course Name</th>
    if(count($options)> 0){
        foreach ($options as $option) {
     <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>
   $sn++; }}

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

if(isset($_GET['id']) && !empty($_GET['id'])){
    $editId= $_GET['id'];
    $query ="SELECT fullName, courseName FROM students WHERE id='$editId'";
    $result = $conn->query($query);
    $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


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

<input type="submit" name="update">

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


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

    $fullName = $_POST['fullName'];
    $courseName = $_POST['courseName'];
    $editId = $_GET['id'];
      $query = "UPDATE students 

      SET fullName ='$fullName', 
       courseName ='$courseName' 
                WHERE id ='$editId'";
      $result = $conn->query($query);