如何使用 PHP 将 html 复选框值保存到 MS-SQL 数据库?

我正在开发一个 html 表单,其数据被保存到 MS-SQL 服务器数据库中。我能够将所有其他字段保存到数据库中它们各自的列中,但是复选框字段将值保留为空。我对这个概念不是很熟悉。

这是代码:

表单.php

<form class="cmxform" action ='functions/Form.php'  method="post">

<div class="form-row">


                   <h3> Contact Information</h3>

                    <div class="form-group col-md-4">

                        <label for="fName">First Name </label>

                        <input type="text" class="form-control" id="fName" name="fName">

                    </div>

                    <div class="form-group col-md-4">

                        <label for="lName">Last Name</label>

                        <input type="text" class="form-control" id="lName" name="lName">

                    </div>

                    <div class="form-group col-md-4">

                        <label for="email">Email </label>

                        <input type="email" class="form-control" id="email" name="email">

                    </div>

                </div>

<div class="form-group col-md-12">

<h3>Category: Please choose one of the following: </h3>

     <div class="form-check">

   <input class="form-check-input" type="checkbox" value="" id="defaultCheck1">

       <label class="form-check-label" for="defaultCheck1">

           Vegetarian

     </div>

<div class="form-check">

         <input class="form-check-input" type="checkbox" value="" id="defaultCheck1">

         <label class="form-check-label" for="defaultCheck1">

        Ham

         </label> 

     </div>

<div class="form-check">

    <input class="form-check-input" type="checkbox" value="" id="defaultCheck1">

    <label class="form-check-label" for="defaultCheck1">

         Turkey

         </label>

</div>

<div class="form-check">

    <input class="form-check-input" type="checkbox" value="" id="defaultCheck1">

         <label class="form-check-label" for="defaultCheck1">

        Other

    </label>

</div>

这是我在将数据发送到数据库时遇到的唯一问题。休息一切正常。



慕莱坞森
浏览 103回答 2
2回答

红糖糍粑

<div class="form-check">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<input class="form-check-input" type="checkbox" value="" **id="defaultCheck1"**>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<label class="form-check-label" for="defaultCheck1">&nbsp; &nbsp; &nbsp; &nbsp; Ham&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</label>&nbsp;&nbsp; &nbsp; &nbsp;</div><div class="form-check">&nbsp; &nbsp; <input class="form-check-input" type="checkbox" value="" **id="defaultCheck1"**>&nbsp; &nbsp; <label class="form-check-label" for="defaultCheck1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Turkey&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</label></div>&nbsp;首先,您要复制 ID。在任何情况下,这都不合适,因为元素 ID 必须是唯一的。其次,表单仅提交具有名称的字段元素的值。他们还需要值,您将其全部保留为“”。这段代码中的“Ham”和“Turkey”只是文本字符串,不要随表单一起提交。请参阅HTML 表单 - 是否需要名称和 ID?编辑 1:PHP-除此之外,您的 PHP 还需要一些改进:$tempCheck = filter_input(INPUT_POST, "defaultCheck1", FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);$defaultCheck1 = (is_array($tempCheck)) ? implode(',', $tempCheck1) : null;这些值不在数组中。假设您有三个复选框,分别命名为“a”、“b”和“c”。如果选中“a”的复选框,我将在 $_POST['a'] 中获取它的值。如果没有选择“b”,$_POST['b'] 将不存在。因此,对于您要处理的每个复选框值,您需要 a) 检查它是否存在于 $_POST 中,如果存在,b) 使用该值(即,将其添加到以逗号分隔的字符串中)。

精慕HU

这是因为您的复选框字段没有名称,但我想您只想允许选择一个类别,在这种情况下,您必须在 HTML 标记中使用单选字段:<div class="form-check">&nbsp; &nbsp; <input class="form-check-input" type="radio" name="category" value="vegetarian" id="defaultCheck1">&nbsp; &nbsp; <label class="form-check-label" for="defaultCheck1">&nbsp; &nbsp; &nbsp; &nbsp; Vegetarian</div><div class="form-check">&nbsp; &nbsp; <input class="form-check-input" type="radio" name="category" value="ham" id="defaultCheck2">&nbsp; &nbsp; <label class="form-check-label" for="defaultCheck1">&nbsp; &nbsp; &nbsp; &nbsp; Ham&nbsp; &nbsp; </label></div><div class="form-check">&nbsp; &nbsp; <input class="form-check-input" type="radio" name="category" value="turkey" id="defaultCheck3">&nbsp; &nbsp; <label class="form-check-label" for="defaultCheck1">&nbsp; &nbsp; &nbsp; &nbsp; Turkey&nbsp; &nbsp; </label></div><div class="form-check">&nbsp; &nbsp; <input class="form-check-input" type="radio" name="category" value="other" id="defaultCheck4">&nbsp; &nbsp; <label class="form-check-label" for="defaultCheck1">&nbsp; &nbsp; &nbsp; &nbsp; Other&nbsp; &nbsp; </label></div>我还注意到您在多个地方使用了相同的 id 属性,id 用于指定元素的唯一性,它不会破坏功能,但请记住这一点。更新如果你想有多个表示相同事物的类别,你可以将它们命名为 categories[0]、categories[1]、categories[2] ...<div class="form-check">&nbsp; &nbsp; <input class="form-check-input" type="checkbox" name="categories[0]" value="vegetarian" id="defaultCheck1">&nbsp; &nbsp; <label class="form-check-label" for="defaultCheck1">&nbsp; &nbsp; &nbsp; &nbsp; Vegetarian</div><div class="form-check">&nbsp; &nbsp; <input class="form-check-input" type="checkbox" value="ham" name="categories[1]" id="defaultCheck2">&nbsp; &nbsp; <label class="form-check-label" for="defaultCheck2">&nbsp; &nbsp; &nbsp; &nbsp; Ham&nbsp; &nbsp; </label></div><div class="form-check">&nbsp; &nbsp; <input class="form-check-input" type="checkbox" value="turkey" name="categories[2]" id="defaultCheck3">&nbsp; &nbsp; <label class="form-check-label" for="defaultCheck3">&nbsp; &nbsp; &nbsp; &nbsp; Turkey&nbsp; &nbsp; </label></div><div class="form-check">&nbsp; &nbsp; <input class="form-check-input" type="checkbox" value="other" name="categories[3]" id="defaultCheck4">&nbsp; &nbsp; <label class="form-check-label" for="defaultCheck4">&nbsp; &nbsp; &nbsp; &nbsp; Other&nbsp; &nbsp; </label></div>然后在 PHP 中更新这个字段:$tempCheck = filter_input(INPUT_POST, "categories", FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);$stmt->bindParam(':defaultCheck1', implode(',', $tempCheck));
打开App,查看更多内容
随时随地看视频慕课网APP