Reputation: 13
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());
}
Upvotes: 1
Views: 7564
Reputation: 4173
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
mysql_query
which is outdated, see here: http://php.net/manual/en/function.mysql-query.php$fileurl
, $user_custermers_id
,$name
i cannot see these being set, so i am assuming they are set somewhere higher in the fileUpvotes: 1
Reputation: 4142
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...
Upvotes: 1
Reputation: 12927
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";
}
Upvotes: 0
Reputation: 15
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) {
...
};
Upvotes: 0
Reputation: 4111
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
Upvotes: 1
Reputation: 19882
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);
Upvotes: 0
Reputation: 67
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";
}
Upvotes: 0