Reputation: 66
I'm trying to make a dropdown list that allows users to select a training program that matches their needs, so when they select it, the option will go into a table in the MySQL database.
This is my HTML form:
<form action="sessionreq.php" method="POST">
<div class="form-group">
<label for="exampleSelect1">Category</label>
<select class="form-control" id="exampleSelect1">
<option name='eg1'>Example option</option>
<option name='eg2'>Example option 2</option>
<option name='eg3'>Example option 3</option>
</select>
</div>
<button type="submit" class="button button-block" name="delete">Request</button>
<br>
</form>
What I need is an SQL query that will go with it, so when they select 'Example option 3', it will be entered into the database in a column called method
. I should be inserted into method
with the values (depending on their option in the dropdown). For example, if the user selected Example option 2
it would be inserted into method
with the values Example option 2
.
How do I populate database tables/columns with dropdowns?
Upvotes: 1
Views: 1299
Reputation: 2817
First of all give name to your select
. In this case I have given training
<form action="sessionreq.php" method="POST">
<div class="form-group">
<label for="exampleSelect1">Category</label>
<select class="form-control" id="exampleSelect1" name="training">
<option value='Example 1'>Example option 1</option>
<option value='Example 2'>Example option 2</option>
<option value='Example 3'>Example option 3</option>
</select>
</div>
<button type="submit" class="button button-block" name="delete">Request</button>
<br>
</form>
Now in your sessionreq.php
you can fetch the value through $_POST
like
$_POST['training']; //You can manipulate the data anyway you like.
Now you can run the query to insert. Given is a prepared statement (prefered)
$stmt = $connection->prepare("INSERT INTO table (column) VALUES (?)");
$stmt->bind_param("s",$_POST['training']);
$stmt->execute();
$stmt->close();
Upvotes: 4