【问题标题】:Putting checkbox and text value into database将复选框和文本值放入数据库
【发布时间】:2017-04-12 06:29:29
【问题描述】:

所以我使用的是 html、php 和 phpmyadmin。

我在 HTML 中拥有的是多个复选框 + 一个文本字段。

我想要的是无论选择哪个复选框以及用户输入什么文本,它都会保存到数据库中,以便他们可以在编辑和删除时检索它。

这是我的复选框和文本字段的 HTML 代码 sn-p (如果您需要更多代码,请告诉我,我会添加它,但没有太多其他代码)

    <div class="add"> <label for:="txtAddress">Grocery List for:</label>
        <input type="text" name="txtAddress" size="50">
    </div>
    <label><input type="checkbox" /> Beans </label>
    <label><input type="checkbox" /> Banana</label>
    <label><input type="checkbox" /> Apples</label>

当用户点击提交时,它会将该列表添加到我已经拥有并制作的 phpmyadmin 中的数据库中。

我想知道我该怎么做。任何一般的想法或建议将不胜感激。我想我可能需要使用 PHP,但我将如何让它提交到我的数据库

【问题讨论】:

标签: php html phpmyadmin


【解决方案1】:

在复选框中设置名称和值,然后为复选框创建 3 列,例如 beans、banana、apples。

<div class="add"> <label for:="txtAddress">Grocery List for:</label>
        <input type="text" name="txtAddress" size="50">
    </div>
    <label><input type="checkbox" name="Beans" value="1"> Beans </label>
    <label><input type="checkbox" name="Banana" value="1"> Banana</label>
    <label><input type="checkbox" name="Apples" value="1"> Apples</label>

然后将请求数据 bean、banana 和 apples 数据放入该 db 列。 检索列是否包含数据 1 表示已检查,否则不检查。

【讨论】:

    【解决方案2】:

    您可以使用以下方式在数据库中插入复选框值。

    <html>  
    <body>  
    <form action="" method="post" enctype="multipart/form-data">  
    <div style="width:200px;border-radius:6px;margin:0px auto">  
    <table border="1">  
    <tr>  
      <td colspan="2">Select Fruit:</td>  
    </tr>  
    <tr>  
       <td>Beans</td>  
      <td><input type="checkbox" name="LisCh[]" value="Beans"></td>  
     </tr>  
     <tr>  
      <td>Banana</td>  
      <td><input type="checkbox" name="LisCh[]" value="Banana"></td>  
     </tr>  
     <tr>  
      <td>Apple</td>  
      <td><input type="checkbox" name="LisCh[]" value="Apple"></td>  
     </tr>  
    
     <tr>  
      <td colspan="2" align="center"><input type="submit" value="submit" name="sub"></td>  
     </tr>  
     </table>  
     </div>  
    </form>  
    
     <?php  
        if(isset($_POST['sub']))  
        {  
          $host="localhost";//host name  
          $username="root"; //database username  
          $word="";//database word  
          $db_name="sub_db";//database name  
          $tbl_name="list"; //table name  
          $con=mysqli_connect("$host", "$username", "$word","$db_name")or die("cannot connect");//connection string  
          $checkbox1=$_POST['LisCh'];  
          $chk="";  
          foreach($checkbox1 as $chk1)  
          {  
             $chk .= $chk1.",";  
           }  
          $in_ch=mysqli_query($con,"insert into list(checked) values ('$chk')");  
           if($in_ch==1)  
          {  
             echo'<script>alert("Inserted Successfully")</script>';  
           }  
        else  
       {  
          echo'<script>alert("Failed To Insert")</script>';  
       }  
      }  
     ?>  
      </body>  
      </html>  
    

    希望对你有帮助。

    【讨论】:

    • 120,当我加载您的 html 代码时,网站会显示表格并在其下方显示“alert("Inserted Successfully")'; } else { echo''; } } ?>” 我想您的代码可能有错误。
    • 我点击提交,提示插入失败
    【解决方案3】:

    每当用户在文本框中提交值并选中任何复选框时,如果您想将它们存储在单个字段中,请制作数组或其他东西。顺便说一句,您没有将复选框设置为数组,请修复此问题。

    一旦您从文本框中获得了复选框和字符串数组,您就可以以任何您想要的方式对其进行操作。

    【讨论】:

      猜你喜欢
      • 2014-05-22
      • 2012-07-16
      • 1970-01-01
      • 2010-09-19
      • 2021-07-14
      相关资源
      最近更新 更多