【问题标题】:HTML Checkbox and Mysql in PHP scriptPHP 脚本中的 HTML Checkbox 和 Mysql
【发布时间】:2016-05-28 00:16:21
【问题描述】:

我在 mysql 的一个表中存储了多个用户的信息。这些应该分为5个标签,A,B,C,D和E。信息存储在表格中如下:

  1. 可能上面提到的所有标签都被使用了
  2. 可能'A'被多次使用,与B、C等相同。
  3. 可能只使用了几个标签,而其他标签从未用于分类

我在由 php 脚本生成的 html 中为这些标签提供复选框选项,当用户检查部分或全部选项时,它应该从表中检索选中的标签信息并执行某些操作。

但在我的情况下,当检查了一些标签并且这些标签没有存储在表中时,我收到了一个错误(因为它们显然在表中没有标签信息)。在那种情况下,我应该如何编程,以便即使我检查了一些标签,并且它们的信息不在 mysql 表中,程序应该忽略它们并显示与其他检查的标签相关的信息。我无法使用 MySQL 查询用 PHP 编写程序。它可以是一个复选框或一个选择。

如果问题不清楚,请告诉我,因为这里用文字解释有点复杂。

【问题讨论】:

  • 确实不是很清楚。我可以建议您通过绘制表格向我们展示一些(模拟)数据(提示,将其包装在<pre> 标签中)。描述你得到的结果和你期望的结果。给我们看一些代码,一个最小的例子stackoverflow.com/help/mcve 当你说你不能用 PHP 或 MySQL 写代码时,你是什么意思?
  • 我们不能盲目地帮助你。您需要向我们展示一些代码和更好的解释,因为您的问题无法理解
  • 好吧,我不知道t get if fully but why dont 你只是显示相关的复选框并存储在你的表中?
  • @Pevara :我正在解决这个问题,我想要的是:如何将多个值从复选框传递到 mysql 查询但给出“或”条件。我在终端上的查询如下:“从表名中选择 id,其中 tag= 'A' or 'B' or 'C';”我想在数据库中实现这个查询,其中通过 OR 条件给出的值是每次在复选框中动态选择的值。
  • @A.Ilazi : 请看我上面的评论

标签: php html mysql checkbox drop-down-menu


【解决方案1】:

据我最终了解,您想根据用户通过复选框选择的选项来查询您的数据库。但是,如果用户不能选择多个选项,那么我建议您使用单选按钮,为所有单选按钮提供相同的name,以便用户只能选择一个选项并像这样运行您的代码:

<form action="page.php" method="post">
    <input type="radio" name="tag" value="A" />
    <input type="radio" name="tag" value="B" />
    <input type="radio" name="tag" value="C" />
    <input type="submit" name="submit" />
</form>

那么你的php页面应该是这样的:

<?php

    //your db connection here

    if(isset($_POST['submit'])){
        $tag = $_POST['tag'];

        // remember you should use mysqli because mysql is deprecated and $c0n will be the variable assigned to your db connection
        $query = mysqli_query($con, "SELECT id FROM table WHERE tag='".$tag."'");

    }   

?>

但如果用户可以选择多个选项,那么您可以使用复选框并像这样运行您的代码:

<form action="page.php" method="post">
    <input type="checkbox" name="a" />
    <input type="checkbox" name="b" />
    <input type="checkbox" name="c" />
    <input type="submit" name="submit" />
</form>

这应该是你的 php 页面:

<?php

    //your db connection here

    if(isset($_POST['submit'])){
        $tag1 = $_POST['a'];
        $tag2 = $_POST['b'];
        $tag3 = $_POST['c'];

        //here you assign variables to each checkbox if they are checked
        //but if they aren't you assign the variable to be null or anything
        //you wish as long as it won't tamper with your query

        if($tag1 == "on"){
            $a = "A";
        } else {
            $a = ""; //assign any wrong value here
        }
        if($tag2 == "on"){
            $b = "B";
        } else {
            $b = ""; //assign any wrong value here
        }
        if($tag3 == "on"){
            $c = "C";
        } else {
            $c = ""; //assign any wrong value here
        }

        // remember you should use mysqli because mysql is deprecated and $c0n will be the variable assigned to you db connection
        $query = mysqli_query($con, "SELECT id FROM table WHERE tag='".$a."' OR tag='".$c."' OR tag='".$c."'");

    }   

?>

希望对你有帮助

【讨论】:

  • 不客气!标准的复制/粘贴错误,我很了解;-)
猜你喜欢
  • 1970-01-01
  • 2013-06-17
  • 1970-01-01
  • 2011-02-28
  • 1970-01-01
  • 2021-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多