dongmaxi6763 2015-01-21 03:48
浏览 115

将复选框和数字输入的值插入数据库

I tried combining two answers from here: counting how many checkbox are checked php/html And here: validating input types "checkbox" and "number" php/html

My goal is to count how many checkboxes were selected, and insert their values along with the number value in the number input.

I found various other posts about it, but couldn't figure out an answer.

Here is the code:

echo '<form action="" method="post">';

Then I have a for loop, where I create my table rows, with the checkboxes. Where $id = $row[0] is updated on each loop, which is the id equivalent of my entry. (The output of

echo '<td style="vertical-align: top;">' . $row[0] . '</td>';

Shows the id correctly)

echo '<tr>';
echo "<td style='vertical-align: top;'><input type='checkbox' name='choice[$id][id]' value='$id'></td>";
echo "<td style='vertical-align: top;'><input type='number' name='choice[$id][order]' size='20'></td>";
echo '</tr>';
echo '<b> <input type="submit" value="Insert"></b>';

Then, on the next page, I have this:

$var_checkbox=$_POST['choice'];
$sql_var_id = "SELECT id FROM custom_form WHERE id=(SELECT max(id) FROM custom_form)";
$var_id_result = mysqli_query($link,$sql_var_id);
$var_id = mysqli_fetch_array($var_id_result);

        $count=count($var_checkbox[$var_id[0]]);

        for($i=0; $i<$count; $i++){
            if($var_checkbox[$i]!= NULL){

                $sql1 = sprintf("INSERT INTO custom_form_has_property (custom_form_id,property_id,field_order) VALUES ('%d','%d','%d');",
                    $var_id[0],
                    $var_checkbox[$var_id[0]][id],
                    $var_checkbox[$var_id[0]][order]
                );
                $result1 = mysqli_query($link,$sql1);
            }
        }

The problem is, that no values of checkboxes or numbers are inserted.

(As a side note, to try and be more specific) If instead of matrixes, I use

   echo "<td style='vertical-align: top;'><input type='checkbox' name='choice[]' value='$row[0]'></td>";
   echo '<td style="vertical-align: top;"><input type="number" name="order[]" size="2"></td>';

And

    $var_checkbox=$_POST['choice'];
    $order = $_POST['order'];

Then

echo "Orders: $order[0],$order[1],$order[2],$order[3],$order[4],$order[5],$ordemr[6]";
echo "Choices: $var_checkbox[0],$var_checkbox[1],$var_checkbox[2],$var_checkbox[3],$var_checkbox[4],$var_checkbox[5],$var_checkbox[6]";

Will output

Orders: 1,,2,,3,,Choices: 1,3,13,,,,

I need the choiced from the checkboxes to be somehow linked to the orders from the number field. And the only way to achieve that, is if they have the same name, but different indexes, which is why I have the matrix, in the first index, it's saved the id number, which is generated on each loop, and in the second, the actual name, which makes the distinction between them (id and order).

I tried various other things, but it all comes back to the same problem... The value of order (number) is stored into the array, even when null, while the value of choices (checkbox) doesnt. I could do an array_filter(), but there is no guarantee that the user will not input an order, while not ticking a choice checkbox. If I would compare the length of choice with the length of order, after filtered, and if they output the same size, there is still no guarantee, that the user didnt check checkbox of line x, and added a value in the order field, on line y.

Maybe there is a way to pass unchecked values to the choice[] variable as null, the same way it happens in order[]?

  • 写回答

2条回答 默认 最新

  • dtm37893 2015-01-21 04:19
    关注

    What is this? choice[.$id.] ? what are the dots for?

    I believe this is the bug!
    You probably were trying to concatenate $id into the string before and just forgot to remove them?

    Corrected line:

    echo "<td style='vertical-align: top;'><input type='checkbox' name='choice[$id][id]' value='$id'></td>";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器