1

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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
mtchdev
  • 66
  • 11
  • Where's your PHP and attempt and MySQL? We won't write it all for you. – Kirk Beard Jun 29 '17 at 07:52
  • 3
    your select won't get submitted to the server because it doesn't have a "name" attribute. It's the ` – ADyson Jun 29 '17 at 07:52
  • add `name="exampleSelect1"` attribute to select and use it in sessionreq.php file like : `$_POST['exampleSelect1'] ` – Jigar Shah Jun 29 '17 at 07:53

1 Answers1

4

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();
Ali Rasheed
  • 2,765
  • 2
  • 18
  • 31
  • you probably need to add values to the – Ivo P Jun 29 '17 at 08:07
  • 1
    @MitchMewett `?` is a placeholder used in prepared statements. to protect against sql injections read more about [prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – Masivuye Cokile Jun 29 '17 at 08:17
  • @MitchMewett the value of `$_POST['training']` goes into the ? when the statement is executed (because of the bind_param command). You can have as many ?s as you like in your query, and then you run bind_param the same number of times, passing each separate parameter you want to use to match the relevant ? (in sequential order). This is known as parameter binding and is much safer that just joining strings together - it's a key measure to protect your data against SQL Injection attacks. – ADyson Jun 29 '17 at 09:29
  • @Ali this form will not post the values to the db – Masivuye Cokile Jun 29 '17 at 09:45
  • I want to post the values into the database depending on the user selection... Like I said in the question, if they select 'Example 1', I want 'Example 1' to be in the `method` column. If this answer cannot achieve that, it's completely irrelevant to my question – mtchdev Jun 29 '17 at 09:49
  • OMG! I didn't see... @MitchMewett you have invalid HTML. Please check new HTML. In `option` element you have to give value. se – Ali Rasheed Jun 29 '17 at 17:26
  • Solved, https://stackoverflow.com/questions/17139501/using-post-to-get-select-option-value-from-html – mtchdev Jun 30 '17 at 07:22