简体   繁体   中英

Inserting data into MySQL from a dropdown

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?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM