0

I am looking for help on storing data from multiple select form to MYSQL using PHP, here is my form

            <form action="" method="post">

            Choose your preferred browser
            <select name="browsers"> 
            <option value="Internet Explorer">
            <option value="Firefox">
            <option value="Chrome">
            <option value="Opera">
            <option value="Safari">
            </select>

            Choose your preferred car
            <select name="carlist">
            <option value="volvo">Volvo</option>
            <option value="saab">Saab</option>
            <option value="opel">Opel</option>
            <option value="audi">Audi</option>
            </select>

            <input type="submit">
            </form> 
3
  • Are you asking about how to actually store the data in the DB? I mean DB architecture? OR are you asking about how to do it in PHP? If latter is the case, then add your DB structure for this data, please. Commented Jul 20, 2017 at 22:46
  • Yes, storing to Mysql database.. Commented Jul 21, 2017 at 2:47
  • There are multiple options. If you don't need to search by preferred car, you can save it in one column in a json. Other option is to use N:M relation, where each user can have multiple preferred cars. If there is finite and small number of columns, like for the browsers, you can also use a separate column for each one with a bool value in it. Does this answer your question? Commented Jul 21, 2017 at 7:17

1 Answer 1

2

$_POST['browser']; and $_POST['carlist']; each hold their own different value. So you can easily fetch each of their values.

$car = $_POST['carlist'];
$browser = $_POST['browser'];

Now it's just a matter of question where you wanna take it from there. F.ex. if you'd like to save it to a database;

// Just make it a standard to secure your database inputs
$car = mysqli_real_escape_string($conn, $_POST['carlist']);
$browser = mysqli_real_escape_string($conn, $_POST['browser']);

$sql = "INSERT INTO someTable (car, browser) VALUES ('".$car."', '".$browser."')";
mysqli_query($conn, $sql);

This is very simplified of course, but I hope it gives you an idea.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.