使用PHP和MYSQL复选框

时间:2013-05-08 11:16:59

标签: php mysql checkbox

我有两个复选框

<form action="" method="post" enctype="multipart/form-data">
<input type="checkbox" name="file_type" value="1"> Filer<br />
<input type="checkbox" name="file_type" value="1"> Statistik
</form>

我的数据库表das_custermer_files上有两行。

第1行=文件 第2行=统计

我如何检查其中一个将值放入我的行。

我试过这段代码

if($_POST['file_type'] == 1){
            mysql_query("INSERT INTO das_custermer_files (url, das_file_categories_id, das_custermers_id, name) 
                         VALUE('$fileurl', 1, $user_custermers_id, '$name')"
                    ) or die(mysql_error());
            }

            elseif($_POST['statistik_type'] == 1){
                mysql_query("INSERT INTO das_custermer_files (url, das_custermer_upload_id, das_custermers_id, name) 
                         VALUE('$fileurl', 1, $user_custermers_id, '$name')"
                    ) or die(mysql_error());
            }
            else{
                echo "no choices";
            }

但是,如果我只检查其中一个,那么它在我的两行中都保留了值1。

希望人们明白。如果不。问


我做到了。

精细代码:

HTML

<form action="" method="post" enctype="multipart/form-data">
    <input type="radio" name="file_type" value="1"> Filer<br />
    <input type="radio" name="file_type" value="2"> Statistik
</form>

PHP

$field = false;

            switch($_POST['file_type'])
            {
                case 1:
                    $field = 'das_file_categories_id'; 
                break;
                case 2:
                    $field = 'das_custermer_upload_id'; 
                break;
                default:
                $field = false;
            }
            if($field)
            {
               mysql_query("INSERT INTO das_custermer_files (url, $field, das_custermers_id, name) 
                         VALUE('$fileurl', 1, $user_custermers_id, '$name')"
                    ) or die(mysql_error());
            }

7 个答案:

答案 0 :(得分:1)

我的方法是这样的:

我会改用radio按钮来确保选择一个类型。

表格位:

    <input type="radio" name="file_type" value="0" checked="checked"> None<br />
    <input type="radio" name="file_type" value="1"> Filer<br />
    <input type="radio" name="file_type" value="2"> Statistik

PHP位:

$field = false;

 // set the field name based on the file type selected in the form
switch($_POST['file_type'])
{
    case 1:
        $field = '`das_file_categories_id`';
    break;
    case 2:
        $field = '`das_custermer_upload_id`'; 
    break;
    default:
    $field = false;
}

// if a field has been set ( i.e file_type != 0 ) then build and run the query
if($field)
{

    $query  =   "INSERT INTO `das_custermer_files` (`url`, " . $field . ", `das_custermers_id`, `name`) ";
    $query .=   "VALUES ('" . $fileurl . "', 1, " . $user_custermers_id . ", '" . $name . "')";
    mysql_query($query);
    echo "Inserting to db: " . $query;
} else {
    echo "No field set: ";
    print_r($_POST);
}

值得注意的事情

答案 1 :(得分:1)

您的check_box名称应该不同。你的一样。

<input type="checkbox" name="file_type" value="1"> Filer<br />
<input type="checkbox" name="file_type" value="1"> Statistik

答案 2 :(得分:1)

Html代码:

<form action="test.php" method = 'post'>
<input type="checkbox" name="file_type[]" value="1"> Filer<br />
<input type="checkbox" name="file_type[]" value="2"> Statistik
<input type="submit" value="submit" />

复选框将在数组$ _POST ['file_type']中包含其值 你可以按如下方式将其值与db相互匹配: -

if(isset($_POST['file_type'])){
 foreach($_POST['file_type'] as $keys)
   {     
     mysql_query("INSERT INTO das_custermer_files (url, das_file_categories_id,     das_custermers_id, name) 
                 VALUE('$fileurl', $keys, $user_custermers_id, '$name')"
            ) or die(mysql_error());
    }
 }
    else if(isset($_POST['statistik_type'])){
        mysql_query("INSERT INTO das_custermer_files (url, das_custermer_upload_id, das_custermers_id, name) 
                 VALUE('$fileurl', 1, $user_custermers_id, '$name')"
            ) or die(mysql_error());
    }
    else{
        echo "no choices";
    }

这可能是你的要求......

答案 3 :(得分:0)

试试这个。应该工作。

 if(isset($_POST['file_type'])){
        mysql_query("INSERT INTO das_custermer_files (url, das_file_categories_id, das_custermers_id, name) 
                     VALUE('$fileurl', 1, $user_custermers_id, '$name')"
                ) or die(mysql_error());
        }

        else if(isset($_POST['statistik_type'])){
            mysql_query("INSERT INTO das_custermer_files (url, das_custermer_upload_id, das_custermers_id, name) 
                     VALUE('$fileurl', 1, $user_custermers_id, '$name')"
                ) or die(mysql_error());
        }
        else{
            echo "no choices";
        }

答案 4 :(得分:0)

你接近是完全错误的。你应该遵循这个结构

<form action="test.php" method = 'post'>
    <input type="checkbox" name="file_type" value="1"> Filer<br />
    <input type="checkbox" name="file_type" value="2"> Statistik
    <input type="submit" value="submit" />
</form>

现在用php

$query  =   '';
if($_POST['file_type'] == 1){
    $query  =   "INSERT INTO das_custermer_files 
                (url, das_file_categories_id, das_custermers_id, name) 
                VALUE
                ('$fileurl', 1, $user_custermers_id, '$name')";
}else if($_POST['file_type'] == 2){
    $query  =   "INSERT INTO das_custermer_files 
                (url, das_custermer_upload_id, das_custermers_id, name) 
                VALUE
                ('$fileurl', 1, $user_custermers_id, '$name')";
}

mysqli_query($query);

答案 5 :(得分:0)

您可能忘了<form action = "..." method = "POST"> </form>

第一个步骤,您应该进行测试:

print_r($_POST);

您通过此测试得到了什么? 如果第一步是正确(非空数组),我们可以完成:

if (isset($_POST['file_type'])) { 
   ... 
};

// OR use alternative
if ($_POST['file_type'] == 1) { 
   ... 
};

答案 6 :(得分:0)

点击一个复选框,不要更改属性,但在圆顶中添加“已选中”,如下所示:

<input type="checkbox" name="file_type" value="1" checked> Filer<br />

因此,复选框的提交值将永远保持为“1”

修改

使用字符串替换值时,可以在php中检查它。

HTML

<form action="" method="post" enctype="multipart/form-data">
<input type="checkbox" name="file_type" value="Filer"> Filer<br />
<input type="checkbox" name="file_type" value="Statistik"> Statistik
</form>

PHP

if($_POST['file_type'] == "Filer"){
            mysql_query("INSERT INTO das_custermer_files (url, das_file_categories_id, das_custermers_id, name) 
                         VALUE('$fileurl', 1, $user_custermers_id, '$name')"
                    ) or die(mysql_error());
            }

            elseif($_POST['file_type'] == "Statistik"){
                mysql_query("INSERT INTO das_custermer_files (url, das_custermer_upload_id, das_custermers_id, name) 
                         VALUE('$fileurl', 1, $user_custermers_id, '$name')"
                    ) or die(mysql_error());
            }
            else{
                echo "no choices";
            }