Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 复选框增加数据库中的值_Php_Html_Forms_Checkbox_Isset - Fatal编程技术网

Php 复选框增加数据库中的值

Php 复选框增加数据库中的值,php,html,forms,checkbox,isset,Php,Html,Forms,Checkbox,Isset,是的,这是一个复选框形式的问题。 我尽量使我的问题简短易懂。 我有一个表格,大约有25个复选框,分为3组。每组为每个复选框给出不同的分数。组1=5分/检查|组2=8分/检查|组3=12分/检查。我想做的是计算提交时的检查数量,然后将所有内容添加到总分中。简单?可以随时选中新的复选框,这意味着总分可以增加,为了方便起见,不能取消选中复选框,因此总分不能减少。总分将保存在数据库表中。这是我现在得到的,不多,但我需要对我的问题有新的认识。这个方程式在我脑子里似乎很容易,但我无法在纸上找到它

是的,这是一个复选框形式的问题。 我尽量使我的问题简短易懂。 我有一个表格,大约有25个复选框,分为3组。每组为每个复选框给出不同的分数。组1=5分/检查|组2=8分/检查|组3=12分/检查。我想做的是计算提交时的检查数量,然后将所有内容添加到总分中。简单?可以随时选中新的复选框,这意味着总分可以增加,为了方便起见,不能取消选中复选框,因此总分不能减少。总分将保存在数据库表中。这是我现在得到的,不多,但我需要对我的问题有新的认识。这个方程式在我脑子里似乎很容易,但我无法在纸上找到它

            ....5points
                    <label for="first_name">first point</label>
                    <input type="checkbox" name="five[]" value="1" <?php if(isset($_POST['five'])) echo 'checked="checked"'; ?> />

                    <label for="first_name">sec point</label>
                    <input type="checkbox" name="five[]" value="1" <?php if(isset($_POST['five'])) echo 'checked="checked"'; ?> />

                    <label for="first_name">n point</label>
                    <input type="checkbox" name="five[]" value="1" <?php if(isset($_POST['five'])) echo 'checked="checked"'; ?> />
................8points
                    <label for="first_name">first point</label>
                    <input type="checkbox" name="eight[]" value="1" <?php if(isset($_POST['eight'])) echo 'checked="checked"'; ?> />

                    <label for="first_name">sec point</label>
                    <input type="checkbox" name="eight[]" value="1" <?php if(isset($_POST['eight'])) echo 'checked="checked"'; ?> />
....12points and so on

当然,它并没有起作用,它只是给了我一个回声结果,5555555和888888。另外,我不想让我的总成绩增加两倍,就像现在一样。有人能帮我把这个脑筋急转弯弄清楚吗

将$a+5和$a+8更改为:

$a += 5;
$a += 8;
或者你可以让整个计算过程更加智能,比如:

if(isset($_POST['submitted'])) {
  $fives = isset($_POST['five']) ? $_POST['five'] : array();
  $eights = isset($_POST['eight']) ? $_POST['eight'] : array();
  $twelves = isset($_POST['twelve']) ? $_POST['twelve'] : array();

  $total = 5 * count($fives) + 8 * count($eights) + 12 * count(twelves);
}
此外,如果提交,您的html会适当地检查所有复选框。如果不希望点被减分,请更改if语句、名称和add disabled属性:

            ....5points
                    <label for="first_name">first point</label>
                    <input type="checkbox" name="five[0]" value="1" <?php if(isset($_POST['five'][0])) echo 'checked="checked" disabled="true"'; ?> />

                    <label for="first_name">sec point</label>
                    <input type="checkbox" name="five[1]" value="1" <?php if(isset($_POST['five'][1])) echo 'checked="checked" disabled="true"'; ?> />

                    <label for="first_name">n point</label>
                    <input type="checkbox" name="five[2]" value="1" <?php if(isset($_POST['five'][2])) echo 'checked="checked" disabled="true"'; ?> />
................8points
                    <label for="first_name">first point</label>
                    <input type="checkbox" name="eight[0]" value="1" <?php if(isset($_POST['eight'][0])) echo 'checked="checked" disabled="true"'; ?> />

                    <label for="first_name">sec point</label>
                    <input type="checkbox" name="eight[1]" value="1" <?php if(isset($_POST['eight'][1])) echo 'checked="checked" disabled="true"'; ?> />
....12points and so on
…5分
第一点
/>
n点
/>
秒点

将$a+5和$a+8更改为:

$a += 5;
$a += 8;
或者你可以让整个计算过程更加智能,比如:

if(isset($_POST['submitted'])) {
  $fives = isset($_POST['five']) ? $_POST['five'] : array();
  $eights = isset($_POST['eight']) ? $_POST['eight'] : array();
  $twelves = isset($_POST['twelve']) ? $_POST['twelve'] : array();

  $total = 5 * count($fives) + 8 * count($eights) + 12 * count(twelves);
}
此外,如果提交,您的html会适当地检查所有复选框。如果不希望点被减分,请更改if语句、名称和add disabled属性:

            ....5points
                    <label for="first_name">first point</label>
                    <input type="checkbox" name="five[0]" value="1" <?php if(isset($_POST['five'][0])) echo 'checked="checked" disabled="true"'; ?> />

                    <label for="first_name">sec point</label>
                    <input type="checkbox" name="five[1]" value="1" <?php if(isset($_POST['five'][1])) echo 'checked="checked" disabled="true"'; ?> />

                    <label for="first_name">n point</label>
                    <input type="checkbox" name="five[2]" value="1" <?php if(isset($_POST['five'][2])) echo 'checked="checked" disabled="true"'; ?> />
................8points
                    <label for="first_name">first point</label>
                    <input type="checkbox" name="eight[0]" value="1" <?php if(isset($_POST['eight'][0])) echo 'checked="checked" disabled="true"'; ?> />

                    <label for="first_name">sec point</label>
                    <input type="checkbox" name="eight[1]" value="1" <?php if(isset($_POST['eight'][1])) echo 'checked="checked" disabled="true"'; ?> />
....12points and so on
…5分
第一点
/>
n点
/>
秒点

在foreach循环中,您正在执行$a+5和$b+8,您可能需要编写$a+=5、$b+=8

在foreach循环中,您正在执行$a+5和$b+8,您可能需要编写$a+=5、$b+=8

您需要更改PHP代码,下面是一个示例:

$total = 0;
if(isset($_POST['submitted'])) {
    if (isset($_POST['five'])) {
        foreach ($_POST['five'] as $five) {
            $total += 5;
        }
    }
    if (isset($_POST['eight'])) {
        foreach ($_POST['eight'] as $eight) {
            $total += 8;
        }
    }
}

+=
运算符获取一个变量,在本例中为
$total
,并将右侧的任何内容添加到该变量中。因此,
$total+=8
$total=$total+8相同

您需要更改PHP代码,下面是一个示例:

$total = 0;
if(isset($_POST['submitted'])) {
    if (isset($_POST['five'])) {
        foreach ($_POST['five'] as $five) {
            $total += 5;
        }
    }
    if (isset($_POST['eight'])) {
        foreach ($_POST['eight'] as $eight) {
            $total += 8;
        }
    }
}

+=
运算符获取一个变量,在本例中为
$total
,并将右侧的任何内容添加到该变量中。因此,
$total+=8
$total=$total+8相同

替换
$a+5和<代码>$b+8如下所示

$a += 5;
$b += 8;

替换
$a+5和<代码>$b+8如下所示

$a += 5;
$b += 8;

嗯,我根本没想过要用count。很好的解决方案。这太棒了!但如果我没有选择任何12点复选框,我得到的值是未定义的?还有一个问题,即使在重新加载页面后,复选框是否可以被选中和禁用@是的,现在重新加载后应该检查并禁用它们。例如,如果您没有选择“五”复选框中的任何一个,那么此数组将为空,并且5*count($\u POST['five'])=0对不起,但是当我在没有输入每个复选框的情况下点击页面时,我会收到一个错误,要么是total未定义,要么是其他一些未定义。当我到达索引页,然后返回时,复选框不想保持选中状态:/hmm对,如果不选中任何复选框,则$u POST['five']未设置并抛出错误。让我编辑一下,嗯,我根本没想过使用count。很好的解决方案。这太棒了!但如果我没有选择任何12点复选框,我得到的值是未定义的?还有一个问题,即使在重新加载页面后,复选框是否可以被选中和禁用@是的,现在重新加载后应该检查并禁用它们。例如,如果您没有选择“五”复选框中的任何一个,那么此数组将为空,并且5*count($\u POST['five'])=0对不起,但是当我在没有输入每个复选框的情况下点击页面时,我会收到一个错误,要么是total未定义,要么是其他一些未定义。当我到达索引页,然后返回时,复选框不想保持选中状态:/hmm对,如果不选中任何复选框,则$u POST['five']未设置并抛出错误。让我编辑一下