如何在PHP中保存从数据库加载的选择选项

时间:2017-01-19 05:28:23

标签: php mysql mysqli

[sample][1]

美好的一天,

我使用mysqli成功从数据库**(db)中获取了select选项的值 但 问题是每当我尝试将其保存在数据库中时,它都不会检索在选择选项中选择的值。

您能否就如何处理此问题提出任何建议。

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

1 个答案:

答案 0 :(得分:0)

我在代码中做了一些更改。 pu标签出边而循环等... 请尝试以下代码:
    

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