Mendez Posted May 12, 2019 Share Posted May 12, 2019 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/308693-multiple-option-html-insert-into-mysql/ Share on other sites More sharing options...
Barand Posted May 12, 2019 Share Posted May 12, 2019 Don't store the multiple choices in the same record - each should be in a separated record. See this example Quote Link to comment https://forums.phpfreaks.com/topic/308693-multiple-option-html-insert-into-mysql/#findComment-1566612 Share on other sites More sharing options...
maxxd Posted May 12, 2019 Share Posted May 12, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/308693-multiple-option-html-insert-into-mysql/#findComment-1566620 Share on other sites More sharing options...
ginerjm Posted May 12, 2019 Share Posted May 12, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/308693-multiple-option-html-insert-into-mysql/#findComment-1566621 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.