Jump to content

Multiple Option HTML insert into MySQL


Mendez

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

?>

 

 



 

 

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
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.