从选择框中插入多个值以分隔行

时间:2018-12-21 09:16:34

标签: php mysql

我为当地的钓鱼俱乐部准备了一个“渔获量报告”表格,目前用户一次只能提交一个渔获量,因此经常不得不多次返回该表格来报告一天的渔获量。

我正在尝试对其进行调整,以使“物种”列表变为多选选择框。

每个catch必须在数据库中成为自己的行。

这是面向用户的表单,我已从“选择”更改为“选择多个”:

.loader:after {
  content: "Ładowanie...";
  position: fixed;
  width: 100%;
  top: 50%;
  left: 0;
  z-index: 1001;
  color:white;
  text-align:center;
  font-weight:bold;
  font-size:1.5rem;        
}

.loader:before {
  content: '';
  background: #000000cc;
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: 1000;
}

然后是addspecies.php的相关部分:

<form action="addspecies.php" method="post" enctype="multipart/form-data">
    <input type="hidden" name="datecaught" id="todayDate"/>
    <script type="text/javascript">
    function getDate()
    {
        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth()+1; //January is 0!
        var yyyy = today.getFullYear();
        if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm}
        today = yyyy+""+mm+""+dd;
        document.getElementById("todayDate").value = today;
    }
    //call getDate() when loading the page
    getDate();
    </script>
    <!--Date caught: <input type="date" name="datecaught"><p>-->
    <table width="600" cellpadding="5">
    <tr>
    <td>Boat Name:</td>
        <td>
            <select name="boatname">
                <option value = "">---Select---</option>
                <?php
                $queryusers = "SELECT BoatName FROM SpeciesHuntBoats WHERE CatchYear2=$year ORDER BY BoatName ASC";
                $db = mysqli_query($db, $queryusers);
                while ( $d=mysqli_fetch_assoc($db)) {
                    echo "<option value='".$d['BoatName']."'>".$d['BoatName']."</option>";
                }
                ?>
            </select>
        </td>
    </tr>
    <tr>
        <td>Species:</td>
        <td>
            <select name="species" multiple size="5">
                <option value = "">---Select---</option><br>
                <?php
                $queryspecies = "Select Species from SpeciesHuntSpecies ORDER BY Species ASC";
                $db2 = mysqli_query($db2, $queryspecies);
                while ( $s=mysqli_fetch_assoc($db2)) {
                    echo "<option value='".$s['Species']."'>".$s['Species']."</option>";
                }
                ?>
            </select>
        </td>
    </tr>
    <tr><td>Angler's Name:</td><td><input type="text" name="angler" size="40"></td></tr>
    <tr><td>Notes:</td><td><textarea rows="2" cols="40" name="notes"></textarea></td></tr>
    <tr>
        <td>Photo (optional):</td>
        <td><input type="file" name="file"></td>
    </tr>
    <tr><td></td><td><input type="submit" value="Submit"></td></tr></table>
</form>

我将如何做到这样,以便用户可以选择4个物种,并且仅提交一次,但是在数据库中创建4个单独的条目?

1 个答案:

答案 0 :(得分:2)

多项选择应具有数组样式名称:

<select name="species[]" multiple size="5">

然后在PHP中,$_POST['species']将是一个包含所有选择的数组。您可以遍历数组并插入每个数组。

foreach ($species as $s) {
    $query_rsCatch = "INSERT INTO SpeciesHunt 
        (DateCaught, CatchYear, BoatName, BoatMake, Species, Angler, Skipper, Notes, PhotoName, Photo) 
        VALUES('$datecaught','$year','$boatname','$boatmake','$s','$angler','$skipper','$notes','$imagename','$imagetmp')";
    $rsCatch = mysql_query($query_rsCatch, $webdb) or die(mysql_error());
}

您还应该停止使用mysql_*函数。它们在很多年前就已被弃用,最后在PHP 7中被完全删除。转换为PDO或mysqli,还学会使用准备好的语句来防止SQL注入。