将多个值从下拉菜单保存到数据库中

时间:2014-11-06 09:58:22

标签: php mysql mysqli sql-insert

我有一个包含下拉列表的表单。我希望该用户应该能够从该下拉列表中选择多个值

 <form class="form-horizontal" role="form" action="add.php" enctype="multipart/form-data" method="post">
        <div class="form-group">
            <label class="col-lg-4 control-label">Name</label>
                <div class="col-lg-6">
                    <input class="form-control" value="" type="text" name="name" >
                </div>
        </div>

        <div class="form-group">
            <label class="col-lg-4 control-label">Address</label>
                <div class="col-lg-6">
                    <input class="form-control" value="" type="text" name="address" >
                </div>
        </div>

         <div class="form-group">
            <?php
            $servername = "localhost";
            $username = "root";
            $password = "";
            $dbname = "db";

            // Create connection
            $con = mysqli_connect($servername, $username, $password, $dbname);
            // Check connection
            if (!$con) {
                die("Connection failed: " . mysqli_connect_error());
            }
            echo "<label class='col-lg-4 control-label'>Student</label>";
            echo "<div class='col-lg-6'>";
            $sql = "SELECT student FROM student";
            $result = $con->query($sql);

            echo "<select class='form-control' name='student' multiple>";
            while($row = $result->fetch_assoc()) {
            echo "<option value='" . $row['student'] . "'>" . $row['student'] . "</option>";
            }
            echo "</select>";
            echo"</div>";
        ?>
        </div>

        <div class="form-group">
            <?php
                $servername = "localhost";
                $username = "root";
                $password = "";
                $dbname = "db";

                // Create connection
                $con = mysqli_connect($servername, $username, $password, $dbname);
                // Check connection
                if (!$con) {
                    die("Connection failed: " . mysqli_connect_error());
                }
                echo "<label class='col-lg-4 control-label'>Subject</label>";
                echo "<div class='col-lg-6'>";
                $sql = "SELECT subject FROM subject";
                $result = $con->query($sql);

                echo "<select class='form-control' name='subject' multiple>";
                while($row = $result->fetch_assoc()) {
                echo "<option value='" . $row['subject'] . "'>" . $row['subject'] . "</option>";
                }
                echo "</select>";
                echo"</div>";
            ?>
        </div>

        <div class="form-group">
            <?php
                $servername = "localhost";
                $username = "root";
                $password = "";
                $dbname = "db";

                // Create connection
                $con = mysqli_connect($servername, $username, $password, $dbname);
                // Check connection
                if (!$con) {
                    die("Connection failed: " . mysqli_connect_error());
                }
                echo "<label class='col-lg-4 control-label'>Hobby</label>";
                echo "<div class='col-lg-6'>";
                $sql = "SELECT hobby FROM hobby";
                $result = $con->query($sql);

                echo "<select class='form-control' name='hobby' multiple>";
                while($row = $result->fetch_assoc()) {
                echo "<option value='" . $row['hobby'] . "'>" . $row['hobby'] . "</option>";
                }
                echo "</select>";
                echo"</div>";
            ?>
        </div>

        <div class="form-group">
            <label class="col-md-3 control-label"></label>
                <div class="col-md-8">
                    <input class="btn btn-primary" value="Save Changes" type="submit" name="submit">
                </div>  
        </div>
</form>

add.php

<?php
include('admin_session.php');

$con=mysqli_connect("localhost","root","","db");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


$name = mysqli_real_escape_string($con, $_POST['name']);
$address = mysqli_real_escape_string($con, $_POST['address']);
$student = mysqli_real_escape_string($con, $_POST['student']);
$subject = mysqli_real_escape_string($con, $_POST['subject']);
$hobby = mysqli_real_escape_string($con, $_POST['hobby']);


$sql="INSERT INTO class (name,address,student,subject,hobby) VALUES ('$name','$address','$student','$subject''$hobby')";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
header("Location: list.php");
mysqli_close($con);


  exit;
?>

我希望在从下拉列表中选择多个值后,它们应该保存在具有名为class的表的数据库中。表名为class的视图是

id name  address student subject hobby
1   a        s     t     y       j
2   b        d     i     g       d

问题是虽然我能够选择多个值,但只有单个值存储在数据库中

P.S我想我应该以更好的方式解释我的问题,以便更新我的帖子

1 个答案:

答案 0 :(得分:1)

将值存储在选择选项

中的数组中
echo "<select class='form-control' name='student[]' multiple>"//it will store selected value in array

在循环中运行查询

foreach ($_POST['student'] as $students)

{
$student = mysqli_real_escape_string($con, $students);//use mysqli escape here

$sql="INSERT INTO class (students) VALUES ('$student')";        

}

在关闭form之前和while loop

之后添加此行
<input type="submit" name="submit" value="submit">

并记住,当您使用mysqli时使用bindparam以及mysqli并不会自动保护您。

点击此链接MySQL vs MySQLi when using PHP