Jump to content

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. 



<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



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

//check if any option is selected

//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)









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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

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.