Jump to content
Mendez

Multiple Option HTML insert into MySQL

Recommended Posts

I’m trying to insert from a multiple option form into MySQL. I’ve tried numerous methods and have had issues such as blank entries being inserted for each specified column, to the last value selected from the form being inserted multiple times, but I never get the desired outcome. Any guidance on how to insert from this type of HTML form? I know that there are issues regarding SQL injection - I'll worry about that later. I'd like to understand how to insert data from this kind of HTML form into MySQL, as i can't seem to find any guidance. There's plenty on how to insert from a standard HTML form i.e. where either every value is selected or left blank, but each entry corresponds to a column in MySQL which either receives a value or NULL. I'm really struggling with when a user can select more than one option from a list. 

 

Quote

<form action ='insert.php' method='POST'>
<select name= 'choices[]' multiple ="multiple"> 
<option value ="Red" >Red</option>
<option value ="Blue" >Blue</option>
<option value ="Green" >Green</option>
<input type="submit" name="send" value="submit" /> </form>

 

Here’s one option I’ve tried for PHP

 

Quote

<?php
$servername ="localhost";
$username = "test";
$password = "test";
$dbname = "Test_DB";

$conn = new mysqli ($servername, $username, $password, $dbname);

if($conn-> connect_error)
  { die("Connection failed: " . $conn->connect_error);
}

{
if(isset($_POST['submit']))
//check if any option is selected

if(isset($_POST['choices']))
//Retrieving each selected option

foreach ($_POST['choices'] as $choice)

$sql = "INSERT INTO Colours_test (Choice1, Choice2, Choice3, Choice4) VALUES ('$choice','$choice', '$choice','$choice')";

if ($conn->query($sql) === TRUE)

{

}

$conn->close();

?>

 

 



 

 

Share this post


Link to post
Share on other sites

Don't store the multiple choices in the same record - each should be in a separated record.

See this example

Share this post


Link to post
Share on other sites

There are many issues with the code you've posted, but I think it'd be best to start by reconsidering your database structure. Learn about normalization. Once you've got a usable database structure, you can decide if you want to use mysqli (probably not) or PDO (probably), and then learn about prepared statements.

Share this post


Link to post
Share on other sites

And think about how you are collecting the choice values.  Your loop is creating the same variable each time thru so how will you have 4 values at the end?

PS - there is no 'submit' element in the $_POST array.  I think you meant to say $_POST['send']. 

You also need to add an end tag for your select tag. 

And as a practice ( a good one IMHO) don't place multiple tags on the same line.  Makes it easier to read thru code quickly when you don't have to find things at the end of another tag's long line of attributes.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.