1

I have two checkboxes

<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>

and i have two rows on my database table das_custermer_files.

row 1 = files row 2 = statistic

how can i check one of them an put the value into my rows.

i have try with this code

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";
            }

But if i only check one of them it stil put value 1 in both of my rows.

Hope people understand. If not. ask


i have done it.

the finel code:

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());
            }
3
  • what happens if they're both ticked? maybe use radio buttons instead Commented May 8, 2013 at 11:30
  • can you show me how i can do that Commented May 8, 2013 at 11:36
  • I've added my answer below Commented May 8, 2013 at 11:37

7 Answers 7

1

My approach would be like this:

I would use radio buttons instead, to ensure a type is selected.

Form Bits:

    <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 Bits:

$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);
}

Things worth noting

  • You're using mysql_query which is outdated, see here: http://php.net/manual/en/function.mysql-query.php
  • Even though you are using $fileurl, $user_custermers_id,$name i cannot see these being set, so i am assuming they are set somewhere higher in the file
Sign up to request clarification or add additional context in comments.

2 Comments

there are nothing in my database. But i like the radio buttons
i've added some echo's to see what its doing.. so run it & see what the output is, it'll probably help diagnose the issue
1

your check_box names should be different. yours the same.

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

Comments

1

Html code :

<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" />

checkbox will contain its value in array $_POST['file_type'] and you may inter its value to db as follows :-

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";
    }

this maybe your requirement...

Comments

0

Try this .Should work.

 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";
        }

1 Comment

You are using same name for the two check boxes.That is wrong.Please use unique name
0

You approach is totally wrong. You should follow this structure

<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>

Now with 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);

Comments

0

You probably forgot to <form action = "..." method = "POST"> </form>.

First step, you should do the test:

print_r($_POST);

What you get by this test? If the first step is correct (not empty array), we can finish:

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

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

Comments

0

Clicking on a checkbox doesen't change the value attribute but adds "checked" in the dome like this:

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

So the submitted value of the checked box will stay forever at "1"

EDIT

When you replace the values with strings you can check against it in 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";
            }

2 Comments

I did as you wished and tried to let your code almost untouched.
Could you please replace echo "no choices"; with echo "no choices - submitted value: ".$_POST['file_type']; and tell me what it says?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.