【问题标题】:Insert query in DB from checkbox count根据复选框计数在数据库中插入查询
【发布时间】:2016-07-25 21:53:10
【问题描述】:

我的 HTML 页面中有一些复选框。例如,当用户选择六个复选框时,我想将其保存到数据库中,例如第一个复选框为 1,第二个复选框为 2....,最后一个复选框为 6:

foreach ($tape as $tap) {
    //some variables
    // I have tried this but it gives me 1 everytime. 
    $i = 1;
    $i = sprintf('%02d', $i);
    $array = array($the_date, $i);
    $ref = implode(".Tape.", $array); // for 2016_73_98.Tape.01

    $stmt = $pdo->prepare(
            "INSERT INTO test (server, tape, 'date', reference, tape_name) 
         VALUES (:server, :tape, :the_date, :reference, :tape_name)"
    );
    $stmt->bindParam(':server', $server);
    $stmt->bindParam(':tape', $tap);
    $stmt->bindParam(':the_date', $the_date);
    $stmt->bindParam(':reference', $my_tape);
    $stmt->bindParam(':tape_name', $ref);

    $result = $stmt->execute();
    $i++;
}

我尝试了一个带有 $count 的 for 循环,它计算有多少已检查元素,但它为 6 个输入中的每一个插入了 6 次相同的选择。 image

我想将“tape_name”列中的 01 替换为(01 表示第一个 MA0001,02 表示 MA0002 ....)它与参考没有任何关系,它必须跟随数字选中的复选框。

<?php

for ($i = 1; $i < 101; $i++): //adjust this number to whatever number of checkboxes you want
    echo '<div class="each_checkboxes">';
    echo '<label for="checkbox' . $i . '">' . $i . '</label>';
    echo '<input type="checkbox" name="tape[]" id="checkbox' . $i . '" value="' . $i . '"/>';

    echo '</div>';

endfor;
?>

【问题讨论】:

  • 你想如何将检查的值存储到表中?如果用户选择前三个复选框,那么您想将 1、2、3 存储到表中吗?像那样?或者您想插入三个不同的记录/行,每个记录/行用于选定的值?
  • 所以我有那个“tape_name”,它是表单中选中的复选框,例如 X1,我有应该保存为 Tape 1/2/3 的“$ref”... /7(假设 7 是最大值)。当我选择磁带 X43、X55、X76 时,我希望它作为 X43 的磁带 01、X55 的磁带 02、X76 的磁带 03 进入数据库。清楚还是我应该更明确?基本上我想使用“计数”表示选中框的数量,然后将其存储在独立列中。 PS:我在帖子中添加了一张图片
  • 你能发布你的 HTML 代码吗?
  • 根据我对您的查询的了解。只需将$i = 1; 放在foreach 循环之前,它就会正确递增。
  • 是的,谢谢,我错过了那个,应该放在前面。但是现在我需要确保第一个选定的项目是获得磁带 01 的那个。现在不尊重选定元素的顺序......我该怎么做?

标签: php html sql checkbox


【解决方案1】:

您甚至不需要在这里使用$i 增量,因为您已经将增量数字存储在复选框中。

你只需要像这样获取选中的复选框值。

foreach ($tape as $tap) {
    //some variables
    // I have tried this but it gives me 1 everytime. 
    $i = sprintf('%02d', $tap);
    $array = array($the_date, $i);
    $ref = implode(".Tape.", $array); // for 2016_73_98.Tape.01

    $stmt = $pdo->prepare(
            "INSERT INTO test (server, tape, 'date', reference, tape_name) 
             VALUES (:server, :tape, :the_date, :reference, :tape_name)");

    $stmt->bindParam(':server', $server);
    $stmt->bindParam(':tape', $tap);
    $stmt->bindParam(':the_date', $the_date);
    $stmt->bindParam(':reference', $my_tape);
    $stmt->bindParam(':tape_name', $ref);

    $result = $stmt->execute();
}

已编辑

现在,如果您只选择了第 5 个复选框和第 10 个复选框,那么第 5 个选中的复选框将获得磁带 01,第 10 个选中的复选框将获得磁带 02。在这种情况下,以下代码将很有用

$i = 1;
foreach ($tape as $tap) {
    //some variables
    // I have tried this but it gives me 1 everytime. 
    $i = sprintf('%02d', $i);
    $array = array($the_date, $i);
    $ref = implode(".Tape.", $array); // for 2016_73_98.Tape.01

    $stmt = $pdo->prepare(
            "INSERT INTO test (server, tape, 'date', reference, tape_name) 
             VALUES (:server, :tape, :the_date, :reference, :tape_name)");

    $stmt->bindParam(':server', $server);
    $stmt->bindParam(':tape', $tap);
    $stmt->bindParam(':the_date', $the_date);
    $stmt->bindParam(':reference', $my_tape);
    $stmt->bindParam(':tape_name', $ref);

    $result = $stmt->execute();
    $i++;
}

让我知道它是否适合你。


了解进一步要求后编辑

在脚本中添加以下 jquery 代码

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function () {

    $("input[type='checkbox']").click(function(){

       var cnt = Number($("#store_count").val());

        var checked_index = $(this).val(); alert("checked_index:" + checked_index);

        if($(this).is(":checked")){

            $(this).val(cnt); $("#store_count").val(cnt + 1);
        }
        else{

            $(this).val("");

            $("input[type='checkbox']:checked").each(function(){

                if( $(this).val() > checked_index ){ $(this).val(Number($(this).val()) - 1); }
            });

            $("#store_count").val(cnt - 1);
        }
    });
});
</script>

然后在您的 HTML 代码中进行以下更改,我们需要一个隐藏字段来跟踪计数器。

<?php
for ($i = 1; $i < 10; $i++): //adjust this number to whatever number of checkboxes you want

    echo '<div class="each_checkboxes">';
    echo '<label for="checkbox' . $i . '">' . $i . '</label>';
    echo '<input type="checkbox" name="tape[]" id="checkbox' . $i . '" value=""/>';
    echo '</div>';

endfor;

echo '<input type="hidden" id="store_count" value="1" />';
?>

最后将数据插入数据库代码

您甚至不需要在这里使用 $i 递增,因为您已经将递增的数字存储在复选框中。

你只需要像这样获取选中的复选框值。

foreach ($tape as $tap) {
    //some variables
    // I have tried this but it gives me 1 everytime. 
    $i = sprintf('%02d', $tap);
    $array = array($the_date, $i);
    $ref = implode(".Tape.", $array); // for 2016_73_98.Tape.01

    $stmt = $pdo->prepare(
            "INSERT INTO test (server, tape, 'date', reference, tape_name) 
             VALUES (:server, :tape, :the_date, :reference, :tape_name)");

    $stmt->bindParam(':server', $server);
    $stmt->bindParam(':tape', $tap);
    $stmt->bindParam(':the_date', $the_date);
    $stmt->bindParam(':reference', $my_tape);
    $stmt->bindParam(':tape_name', $ref);

    $result = $stmt->execute();
}

【讨论】:

  • 确实代码可以工作,但它仍然没有将 01 放在第一个选中的复选框中:当我按此顺序选择 45、8、14 时,我得到了 8 的 tape01我希望它在 45 中,第一个检查
  • 在这种情况下,你必须使用 jquery,因为 PHP 不会先选择哪个复选框,然后选择第二个。
  • 哦,好的,非常感谢!你能告诉我应该在 jQuery 中使用哪个属性吗?再次感谢队友!
  • 在这种情况下,我有一个问题。如果用户选择了 45、8 和 14。那么根据您的要求,45 将得到 01,8 将得到 02,14 将得到 03。现在如果用户取消选中 45 并选中 35,会发生什么?
  • 那么 35(旧 45)是最后一个被选中的(8 会成为第一个)
猜你喜欢
  • 1970-01-01
  • 2019-05-22
  • 2013-03-09
  • 1970-01-01
  • 2011-06-10
  • 2017-09-30
  • 2012-04-23
  • 1970-01-01
  • 2017-07-17
相关资源
最近更新 更多