Drop down selection not showing up in MySQL. How do I get the selection from the MySQL-populated drop down list to be a value I can use when creating a new record?

            <?php
            include_once 'dbcon.php';

            if(isset($_POST['save_mul']))
            {       
                $total = $_POST['total'];

                for($i=1; $i<=$total; $i++)
                {
                    $sname = $_POST["sname$i"];
                    $sql="INSERT INTO users(speaker) VALUES ('".$sname."')"; 
                    $sql = $MySQLiconn->query($sql);        
                }

                if($sql)
                {
                    ?> <script>
                    alert('<?php echo $total." records was inserted !!!"; ?>');
                    window.location.href='index.php';
                    </script> <?php
                }
                else
                {
                    ?> <script>
                    alert('error while inserting , TRY AGAIN');
                    </script> <?php
                }
            }
            ?> <html> <td><select name="room<?php echo $i; ?>" class="form-control action"> <?Php 
                        $mysqli = new mysqli("localhost","root","","challenge_db");
                        $sqlSelect="SELECT DISTINCT room_name FROM ref_room";
                        $result = $mysqli -> query ($sqlSelect);

                        echo '<OPTION value="">'; 
                        echo "Select an option</OPTION>"; 

                        while ($row = mysqli_fetch_array($result)) {
                            $rname = $row["room_name"];
                            echo "<OPTION value=''>$rname</OPTION>";
                        }
                        ?> </select></td>
  1. Do not need make new connection in line 30 - use existing
  2. If you want to post multiple "room" define field name as array <select name="room[]"> or <select name="room[$i]">
  3. Set option value e.g. <option value="$rname">$rname</option>
  4. I recommend you use filter_input(INPUT_POST, 'room') instead of $_POST['room'] e.g.

    $room = filter_input(INPUT_POST, 'room', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY);
  5. use prepared statement instead of lines 8-13, e.g.

    $room = filter_input(INPUT_POST, 'room', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY);
    $sql="INSERT INTO users(speaker) VALUES (?)";
    $stmt = $MySQLiconn->prepare($sql);
    foreach($room as $row){
            $stmt->bind_param("s",$row);
            $stmt->execute();
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.