• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar

CodingStatus

- Free Source Code for Web Applications

  • programs
  • JavaScript
  • jQuery
  • ReactJS
  • Ajax
  • Node.js
  • PHP
  • SQL
  • Interview Questions

How to Insert Select Option Value in Database Using PHP & MySQL

March 23, 2022 By Md Nurullah

In this tutorial, You will learn to insert select option values in the database using PHP & MySQL with some simple steps. These steps are very easy to understand and implement in web applications.

Here, I have taken only a single dropdown input field to store select option values in the database. Once you learn it, you will easily customize it according to your project requirement.

php insert select option in database

How to Store Dropdown Value in Database in PHP

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

codingstatus/
     |__database.php
     |__ select-option.php
     |__ insert-option.php

Learn Also –

How to write Start Pattern Programs in PHP

Insert Data into Database using PHP & MySQL

Delete Multiple Records with  Checkbox in PHP & MySQL

Integrate Google ReCAPTCHA in PHP

Now, let’s start to store dropdown values in the database step by step –

1. Create SQL Database & Table

First of all, You will have to create a database with the name of “codingstatus”.

Database Name – codingstatus

CREATE DATABASE codingstatus;

After that, create a table with the name of “courses”  in the database “codingstatus.

Table Name – courseses

CREATE TABLE `courses` (
`id` int(10) UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
`courseName` varchar(255) DEFAULT NULL,
);

2. Connect PHP to MySQL

To insert select option value in the database, 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. Create Input Field for Select Option

Here, I have created a single dropdown input field with some select options that contain some course name.

File Name – select-option.php

<form action="" method="post">
<select name="courseName">
  <option value="">Select Course</option>
  <option value="web Designing">Web Designing</option>
  <option value="web Development">Web Development</option>
  <option value="app Development">app development</option>
  <option value="game development">Game Development</option>
  <option value="graphic Designing">Graphic Desiging</option>
  <option value="digital marketing">Digital Marketing</option>
</select>
<input type="submit" name="submit">
</form>

4. Insert Select Option in Database

To insert select option in the database, you will have to implement the following steps to write its code –

Step-1: First of all, apply if condition withisset($_POST[‘submit’]) to check form is set or not

Step-2: Assign course name input to the variable $courseName

Step-3: check course name is empty or not using empty() with if statement. if it is true then follow the next step

Step-4: write MySQL insert query to insert the select option value in the “courses” table

Step-5: if select option is inserted into database successfully then print a success message

File Name – insert-option.php

<?php 
if(isset($_POST['submit'])){
  $courseName = $_POST['courseName'];
  if(!empty($courseName)){
      $query = "INSERT INTO courses (courseName) VALUES('$courseName')";
      $result = $conn->query($query);
      if($result){
      	echo "Course is inserted successfully";
      }	
    }
  }

?>

Insert Select Option Value with another Input Value using PHP & MySQL

Now, You will learn to insert a section option value with another input field like fullName into another table. In the previous step, we have inserted static option values. But In this step, We will display select option values from a database and then insert them into another table of the same database

Before getting started, You will have to create the following two files –

  • insert-form.php
  • insert-script.php

Also, Create another table Name – students with the help of the following query –

CREATE TABLE `students` (
`id` int(10) UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
`fullName` varchar(255) DEFAULT NULL,
`courseName` varchar(255) DEFAULT NULL,
);

Create a form and display select option values

First of all, Include database.php and insert-script.php file

Then create an HTML form with a select option & text input field and display data from the database in the select option

File Name – insert-form.php

<?php
include("database.php");
include("insert-script.php");
?>
<form action="" method="post">
<input type="text" name="fullName" value="">
<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
    //selected option
    if(!empty($courseName) && $courseName== $option){
    // selected option
    ?>
    <option value="<?php echo $option; ?>" selected><?php echo $option; ?> </option>
    <?php 
continue;
   }?>
    <option value="<?php echo $option; ?>" ><?php echo $option; ?> </option>
   <?php

    }}
    ?>
</select>
<br>

<input type="submit" name="submit">
</form>


Insert Select Option value & Text Input Value

In this step, Write a MySQL query to insert select option value and text input value into the dabase.

File Name – insert-script.php

<?php 
if(isset($_POST['submit'])){

  $fullName = $_POST['fullName'];
  $courseName = $_POST['courseName'];

     if(!empty($fullName) && !empty($courseName)){
      $query = "INSERT INTO students (fullName, courseName) VALUES('$fullName', '$courseName')";
      $result = $conn->query($query);
     
      if($result){
      	echo "Student detail is inserted successfully";
      }	
    }
  }

?>

 

 

Related posts:

  1. How to Create Facebook App for App Id and Secret Key
  2. How to Create Category and Subcategory in PHP
  3. Compress Image Size in PHP Without Losing Quality
  4. Insert Data into Database Using PHP & MySQL

Filed Under: PHP

Hey there, Welcome to CodingStatus. My Name is Md Nurullah from Bihar, India. I'm a Software Engineer. I have been working in the Web Technology field for 4 years. Here, I blog about Web Development & Designing. Even I help developers to build the best Web Applications.

Primary Sidebar

Latest Tutorials

  • Login with ajax in PHP, MySQL
  • Load Records on Select Box using Ajax, PHP & MySQL
  • Swapping of Two Numbers in python
  • How to Install Express Application Using Express Generator Tool
  • Registration Form using Ajax, PHP & MySQL

Popular Tutorials

  • Filter Data with Checkboxes in PHP & MySQL
  • Filter Data by Category in PHP & MySQL
  • Filter Prices From Low to High in PHP, MySQL
  • To Do List Using PHP and MySQL
  • Upload Multiple Files to Store in MySQL Database Using PHP

Categories

  • Ajax (12)
  • Django (5)
  • HTML (4)
  • Installation (3)
  • Interview Questions (5)
  • JavaScript (20)
  • jQuery (11)
  • Laravel (2)
  • Node.js (23)
  • PHP (42)
  • programs (1)
  • Python Programs (1)
  • ReactJS (35)
  • SQL (12)
  • Tips (7)
  • Home
  • About Us
  • Privacy Policy
  • Disclaimer
  • Terms & Conditions
  • Sitemap
  • Contact Us

Copyright © 2023 CodingStatus - All Rights Reserved