如何在数据库的不同列中插入多个复选框

时间:2019-12-03 01:18:36

标签: php html

我在这里遇到了一些问题,我无法从多个复选框中获得单个值。我想将其存储在每个刻度值= 1的不同列中,该框在数据库中不会勾选为0.对于普通输入,我得到了该值,但不适用于复选框。你们能帮忙吗?

HTML代码:这是我的html代码

1:software:Word Processor:55.00
2:software:Bad Wolf Video Game:19.99
3:software:Return to Gallifrey Video Game:59.99
4:vehicle:TARDIS:999999.99
5:hardware:sonic screwdriver:9999.99
6:merchandise:company t-shirt:20.00

1:Davy Jones:Security
2:Ricky Davis:Developer
3:Samantha Smith:Salesperson
4:Matt Smith:Doctor
5:David Tennant:Doctor
6:Buckminster Fuller:Engineer
7:Clara Oswald:Nurse
8:Amelia Pond:Nurse

1:1:1:01/02/2015:2
2:2:1:02/04/2017:2
3:3:1:03/06/2018:5
4:4:1:11/05/2018:5
5:1:1:01/12/2018:2
6:2:2:02/11/2018:2
7:3:1:05/13/2018:6
8:6:3:06/24/2018:1
9:5:1:02/02/2016:5
10:1:1:05/01/2017:5
11:2:1:11/05/2018:5
12:3:1:12/06/2018:5
13:2:1:02/12/2018:5
14:1:1:10/16/2018:5
15:6:4:05/18/2018:3
16:5:1:06/28/2018:6
17:1:1:07/30/2018:5
18:2:1:08/04/2018:7
19:3:1:09/07/2018:5
20:6:1:10/17/2018:4
21:6:1:10/17/2018:8
22:2:1:08/04/2018:7
23:3:1:09/07/2018:5

JavaScript:我应该在哪里获取值并将其存储在数据库中。

<div class="col-sm-4">

                    <div class="form-group">
                        <label for="colour">Machine</label><br>
                        <input type="text" id="input_1">
                    </div><br>
                <input type="checkbox" name="Inspection[]" value="true"> Function<br>
                <input type="checkbox" name="Inspection[]" value="true"> Visual<br>
                <input type="checkbox" name="Inspection[]" value="true"> Sleeve<br>
                <input type="checkbox" name="Inspection[]" value="true"> Mail<br>

    </div>
<p><input type="button" id="add_btn" value="Save"></p>

昨天我在这里的stackoverflow中找到了这个,但是每次尝试都行不通。

$(document).on("click", "input#add_btn", function() { Addsave() });

function  Addsave()
{

    var Inspec1 = $("#input_1").val();
    var Inspec2 = $("input[name='Inspection']:checked").val();
    var Inspec3 = $("input[name='Inspection']:checked").val();
    var Inspec4 = $("input[name='Inspection']:checked").val();
    var Inspec5 = $("input[name='Inspection']:checked").val();

    var tables = ["example"];

    var AddObject = new Object();


        AddObject.column1    = Inspec1;
        AddObject.column2    = Inspec2;
        AddObject.column3    = Inspec3;
        AddObject.column4    = Inspec4;
        AddObject.column5    = Inspec5;


    var list = [AddObject];
    var objParams = JSON.stringify(list);

    var render  = function(){ renderAddExec(data);};
    insertExecute(list,tables,render);
}

1 个答案:

答案 0 :(得分:0)

  1. 您的复选框从未执行过,因为您选中了 df.groupby('one').head(2) Out[246]: col one two a t 0 u 1 b t 4 u 5 c u 9 v 10 d w 11 t 12 ,但是您的“提交”按钮没有 任何名称,这只是ID。
  2. 您使用if(isset($_POST['add_btn']))创建对象 需要提交。您可以将其扩展为3个功能 如果需要JQuery,只需致电ajax

我确实在本地主机上使用纯php测试了您的代码,它的工作原理与预期相同。

async

post.php

<form action="post.php" method="post">
                <div class="col-sm-4">

                    <div class="form-group">
                        <label for="colour">Machine</label><br>
                        <input type="text" id="input_1">
                    </div><br>
                    <input type="checkbox" name="Inspection[]" value="true"> Function<br>
                    <input type="checkbox" name="Inspection[]" value="true"> Visual<br>
                    <input type="checkbox" name="Inspection[]" value="true"> Sleeve<br>
                    <input type="checkbox" name="Inspection[]" value="true"> Mail<br>

                </div>
            <!-- I added a name to button to catch the event on backend -->
            <p><input name="submit" type="submit" id="add_btn" value="submit"></p>
            </form>

结果:

if(isset($_POST['submit']))
{
    if(isset($_POST['Inspection']))
    {
        var_dump($_POST['Inspection']);
        foreach($_POST['Inspection'] as $val)
        {
            echo $val;
        }
    }
}

我使用PDO解决代码的方法:

array (size=4)
  0 => string 'true' (length=4)
  1 => string 'true' (length=4)
  2 => string 'true' (length=4)
  3 => string 'true' (length=4)
truetruetruetrue

如何将mysql转换为PDO,请检查以下链接: PHP: Convert INSERT MySQLi to PDO