Jump to content

Mendez

New Members
  • Posts

    1
  • Joined

  • Last visited

Posts posted by Mendez

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

    ?>

     

     



     

     

×
×
  • 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.