0

所以我试图从复选框中插入选中的值,但不起作用。我相信我的问题是它们一开始没有正确设置。我试图使用循环,但我相信我做错了:P

我的“isset”:

if(!empty($_POST))
{

    if(empty($_POST['name'])) {die("Please enter a Your Name.");}
    if(empty($_POST['ckboxes[]'])) {die("Please check at least one box.");}
    if(empty($_POST['ddown'])) {die("Please select a value.");}
    if(empty($_POST['txtDate'])) {die("Please type in a date.");}
    if(empty($_POST['agdis'])) {die("Please agree or disagree.");}
    if(empty($_POST['yn'])) {die("Please select Yes or No.");}

和插入过程:

//Insert for Checkboxes//
    /////////////////////////
    $query2 = "
        INSERT INTO coffee_ckbx (
            white,
            green,
            red,
            blue
        ) VALUES (
            :white,
            :green,
            :red,
            :blue   
        )
    ";

    foreach($_POST['ckboxes'] as $check){array($check);}

    $query_params2 = array(
        ':white' => $check['1'],
        ':green' => $check['2'],
        ':red' => $check['4'],
        ':blue' => $check['5']
    );

    try
    {
        // Execute the query to create the user
        $stmt = $db->prepare($query);
        $result = $stmt->execute($query_params);

        $stmt2 = $db->prepare($query2);
        $result2 = $stmt2->execute($query_params2);
    }
    //kept the catch out and other code due to lengthiness.

这是我的html:

            <li>
            <label for='ck-boxes'>Select from the check boxes: </label>
            <input type="hidden" name="ckboxes" value="0" />
            <input type='checkbox' id='' name='ckboxes' value='white'/>
            <input type='checkbox' id='' name='ckboxes' value='green'/>
            <input type='checkbox' id='' name='ckboxes' value='red'/>
            <input type='checkbox' id='' name='ckboxes' value='blue'/>
        </li>

有任何想法吗?先感谢您 :)

4

1 回答 1

0

尝试添加[]到名称的末尾,这样会告诉 PHP 这是一个数组。

<input type="hidden" name="ckboxes[]" value="0" />
<input type='checkbox' id='' name='ckboxes[]' value='white'/>
<input type='checkbox' id='' name='ckboxes[]' value='green'/>
<input type='checkbox' id='' name='ckboxes[]' value='red'/>
<input type='checkbox' id='' name='ckboxes[]' value='blue'/>

也不要引用数字,引号内的东西被视为string

所以$check['1']应该$check[1]作为例子。

于 2013-07-08T19:54:59.120 回答