简体   繁体   中英

How to save a select option loaded from database in PHP

[sample][1]

Good day,

I successfully fetched the values from the database **(db) for the select option using mysqli but the problem is whenever I try save it in the database it doesn't retrieve the value selected in select the option .

Can you please give any suggestions on how to deal with this.

 <?php $conn = new mysqli('localhost', 'root', '', 'db') ; if (mysqli_connect_error()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } else if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } else { echo"db_connection.php RUNNING<br>"; } /* CONNECTION IN DATABASE*/ /* WHILE LOOP FOR SELECT OPTION IN DATABASE*/ $result = $conn->query("select b_fname from tbl_client"); echo "<html>"; echo "<body>"; echo "<select name='id'>"; while ($row = $result->fetch_assoc()) { unset($b_id, $b_fname); $b_id = $row['b_id']; $b_fname = $row['b_fname']; echo '<form action ="dropdown_demo.php" method="POST" enctype="multipart/form-data" >'; echo '<option name="b_fname" value="/'.$b_fname.'/">'.$b_fname.'.'.$b_fname.'</option>'; echo " </form>"; } echo '</select><input type="submit" name="add_drop" />'; echo "</body>"; echo "</html>"; /* SQL INSERT THE VALUE TO THE */ $sql = "INSERT INTO tbl_client (b_fname)VALUES ( '$b_fname' )"; if (mysqli_query($conn, $sql)) { echo('<script>alert("Record Added Successfully!");</script>'); // header('Refresh: 1; URL= import_addnew-Copy.php'); error_reporting(0); } else { error_reporting(0); } mysqli_close($conn); ?> 

I have made some changes in your code. pu the tag out side while loop etc... Try the below code:

   $conn = new mysqli('localhost', 'root', '', 'db') ;
        if (mysqli_connect_error())
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        else if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }else
        {
            echo"db_connection.php RUNNING<br>";
        }
      /* CONNECTION IN DATABASE*/
   /* WHILE LOOP FOR SELECT OPTION IN DATABASE*/


    $result = $conn->query("select b_fname from tbl_client");

    echo "<html>";
    echo "<body>";

        echo '<form action ="dropdown_demo.php" method="POST" enctype="multipart/form-data" >';
        echo '<select name="b_fname" >';
        while ($row = $result->fetch_assoc()) {

            unset($b_id, $b_fname);
            $b_id = $row['b_id'];
            $b_fname = $row['b_fname'];

            echo '<option value="'.$b_fname.'">'.$b_fname.'.'.$b_fname.'</option>'; 
        }
                        echo '</select><input type="submit" name="add_drop" />';
                    echo " </form>";
    echo "</body>";
    echo "</html>";

 /* SQL INSERT THE VALUE TO THE */
if(isset($_POST['add_drop'])){
    $b_fname = $_POST["b_fname"];
    $sql = "INSERT INTO tbl_client (b_fname) VALUES ( '".$b_fname."' )";
        if (mysqli_query($conn, $sql)) {

            echo('<script>alert("Record Added Successfully!");</script>');
            //  header('Refresh: 1; URL= import_addnew-Copy.php');
            error_reporting(0);

        } else {

            error_reporting(0);
        }
}
mysqli_close($conn);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM