PHP Insert Drop Down List selected value in MySQL database

How to store/submit drop down list selected item to phpmyadmin MySQL db using PHP script code.

In this tutorial we are simply creating an html from with select name and label tags. This tags create drop down list in html. Now after that we are submitting the current drop down list selected item directly into MySQL db table on Submit button click. So here is the complete step by step tutorial for PHP Insert Drop Down List selected value in MySQL database.

android-project-download-code-button

PHP Insert Drop Down List selected value in MySQL database.

Code for drop-down-list.php file.

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PHP Insert Drop Down List selected value in MySQL database</title>
</head>

<body>

<form method="post" action="">
 
 <label >Please Select Subject</label>
 
 <select name="subject_names">
 <option>---Select Subject--</option>
 <option value="Android">Android</option>
 <option value="PHP">PHP</option>
 <option value="Java">Java</option>
 <option value="JavaScript">JavaScript</option>
 <option value="Maths">Maths</option>
 
 </select>

 <button type="submit" name="submit" >Submit</button>
 
 </form>

<?php
if(isset($_POST["submit"]))
{
 
 //Including dbconfig file.
include 'dbconfig.php';
 
$subjectName=$_POST["subject_names"];

mysql_query("INSERT INTO demo_table_6 (subject) VALUES ('$subjectName')"); 

echo " Added Successfully ";

}

 ?>

</body>
</html>

Code for dbconfig.php file.

 <?php

//Define your host here.
$hostname = "localhost";

//Define your database username here.
$username = "root";

//Define your database password here.
$password = "";

//Define your database name here.
$dbname = "test";

 $conn = mysql_connect($hostname, $username, $password);
 
 if (!$conn)
 
 {
 
 die('Could not connect: ' . mysql_error());
 
 }
 
 mysql_select_db($dbname, $conn);


?>

Screenshots:

PHP Insert Drop Down List selected value in MySQL database

drop down list db

Click here to download PHP Insert Drop Down List selected value in MySQL database project with source code + MySQL db file included.

Leave a Reply

Your email address will not be published. Required fields are marked *