How can I concat and insert two checkbox input arrays into db? - php

Ok I have a set of checkboxes with corresponding text inputs next to them. POST apparently sets a variable as an associative array, I want to be able to concat the values for the checkbox with the corresponding year by using a for loop and putting like indexes together. I'm currently getting an "unexpected [" error in the if statement. I looked into doing exactly this and found a few examples that went about this in the same way with success, what am I doing wrong?
Here is the form section
<form>
<label><input type="checkbox" name="check_list[]" Value = "Castle Connolly Top U.S. Doctor" id ="ccn"/>Castle Connolly Top U.S. Doctor</label>
<input type="text" name="check_year" value="<?php echo decode_text($check_year);?>"/>
<label><input type="checkbox" name="check_list[]" Value = "Vitals Patients Choice"/>Vitals Patient's Choice</label>
<input type="text" name="check_year" value="<?php echo decode_text($check_year);?>"/>
<label><input type="checkbox" name="check_list[]" Value = "Vitals Compassionate Doctor"/>Vital's Compassionate Doctor Recognition</label>
<input type="text" name="check_year" value="<?php echo decode_text($check_year);?>"/>
<label><input type="checkbox" name="check_list[]" Value = "Super Doctor"/>Super Doctor</label>
<input type="text" name="check_year" value="<?php echo decode_text($check_year);?>"/>
</form>
Then in my handler I have this code to get the values into separate arrays and concat them to another variable that I insert
$check_list = isset($_POST['check_list']) ? prepare_input($_POST['check_list']) : '';
$check_year = isset($_POST['check_year']) ? prepare_input($_POST['check_year']) : '';
$check_year_array = array_values($check_year);
$check_list_array = array_values($check_list);
$notable_awards .= ', ';
for($n=0; $n<5;$n++){
if(!empty($check_list_array[$n])){
$notable_awards .= $check_list_array[$n].','.$check_year_array[$n];
}
}
$sql = mysql_query("UPDATE" .TABLE_DOCTORS. " SET
notable_awards = '".$notable_awards."' ,
notable_publications = '".$notable_publications."'
last_logged_ip = '".$user_ip."'
WHERE id =".$doctor_id);

Related

Can't insert checkboxes into mysql database

I have these checkboxes that I am trying to (ultimately concat two together and) insert into a database. I've tried several ways and have searched through many pages, but my page keeps breaking when I try to insert. I'm not actually getting an error in my apache log, but a message is triggered that makes me believe it's some kind of sql error (the else statement at the end, which is a check if the query affected any rows). The error isn't thrown until I add the foreach and additional awards to the query .
Here is the form section
<label><input type="checkbox" name="check_list[]" Value = "Castle Connolly Top U.S. Doctor" />Castle Connolly Top U.S. Doctor</label>
<!-- <input type="text" name="check_year" value="<php echo decode_text($check_year);?>"/> -->
<label><input type="checkbox" name="check_list[]" Value = "Vitals Patients Choice"/>Vitals Patient's Choice</label>
<!--<input type="text" name="check_year" value="<php echo decode_text($check_year);?>"/> -->
<label><input type="checkbox" name="check_list[]" Value = "Vitals Compassionate Doctor"/>Vital's Compassionate Doctor Recognition</label>
<!--<input type="text" name="check_year" value="<php echo decode_text($check_year);?>"/> -->
<label><input type="checkbox" name="check_list[]" Value = "Super Doctor"/>Super Doctor</label>
<!--<input type="text" name="check_year" value <php echo decode_text($check_year);?>"/> -->
inserting
$additional_awards = '';
$check_list = $_POST['check_list'];
foreach($check_list as $check)
{
$additional_awards .= $check.", ";
}
// insert new user
$sql = 'INSERT INTO docflight_doctors (
email,
user_name,
user_password,
additional_awards,
preferred_language,
date_created,
registered_from_ip,
last_logged_ip,
is_active,
is_removed,
comments,
registration_code )
VALUES(
\''.encode_text($email).'\',
\''.encode_text($user_name).'\',
\''.$user_password.'\',
\''.$additional_awards.'\',
\''.Application::Get('lang').'\',
\''.date('Y-m-d H:i:s').'\',
\''.$user_ip.'\',
\'\',
'.$is_active.',
0,
\'\',
\''.$registration_code.'\')';
if(database_void_query($sql) > 0){
/// do other stuff
else{
///echo mysql_error();
$msg = draw_important_message(_CREATING_ACCOUNT_ERROR, false);
FIXED, \''.$user_password.'\', should have been '.$user_password.', for some reason ....

Use database to check a checkbox

I have a page to view assets with an Edit link. When I click the link it goes to edit_case.php which has a form to edit what elements of the row are in the database as checkboxes. However the boxes do not show them as checked. I have the following code...
// get already checked box values
$repop = "SELECT * FROM case_audit WHERE case_id = $case_id";
$popresults = mysqli_query($dbc, $repop);
$row = mysqli_fetch_array($popresults, MYSQLI_ASSOC);
print_r ($row);
The print_r does show the whole record row from DB. which is either a 1 or 0, checked || not checked.
The form...
<div id="facepics">
<label><input type="checkbox" name="Facial1" value="<?php echo $row['frontrest']; ?>" >Front at Rest </label><br>
<label><input type="checkbox" name="Facial2" value="<?php echo $row['frontbigsmile']; ?>" >Front Big Smille</label><br>
<label><input type="checkbox" name="Facial3" value="<?php echo $row['profile']; ?>" >Profile</label><br>
<label><input type="checkbox" name="Facial4" value="<?php echo $row['subvertex']; ?>" >SubMento Vertex</label><br>
</div>
I know I need to turn the 1's to "checked" just not sure how best to do that.
so basically checked="true" attribute in input creates a checked checkbox.
HTML Code looks like
<input type="checkbox" checked="true">
In your case you can do it like:
<input type="checkbox" name="Facial1" value="frontrest" <?= (intval($row['frontrest']) == 1) ? 'checked' : '';>
Also note that I changed value attribute, with frontrest so that you can identify the checkbox uniquely
EDIT: I have modified the code
<input type="checkbox" name="Facial1" <?=$row['frontrest']==1?'checked':''?>>
I often have the same issue where the browser ignores checked="false" and checks all
so I use
<input type="checkbox" checked>

PHP - Handling HTML checkbox array [duplicate]

I have 1 form in with multiple checkboxes in it (each with the code):
<input type="checkbox" name="check_list" value="<? echo $row['Report ID'] ?>">
Where $row['Report ID'] is a primary key in a database -so each value is different.
How would I be able to tell which checkboxes have been checked? (Maybe multiple)
This is for an inbox system and I have a button below that I want (when clicked) to delete all messages (ids of: $row['Report ID']) which have the checkbox's checked.
Set the name in the form to check_list[] and you will be able to access all the checkboxes as an array($_POST['check_list'][]).
Here's a little sample as requested:
<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="value 1">
<input type="checkbox" name="check_list[]" value="value 2">
<input type="checkbox" name="check_list[]" value="value 3">
<input type="checkbox" name="check_list[]" value="value 4">
<input type="checkbox" name="check_list[]" value="value 5">
<input type="submit" />
</form>
<?php
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check; //echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
}
}
?>
Edit To reflect what #Marc said in the comment below.
You can do a loop through all the posted values.
HTML:
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
PHP:
foreach($_POST['check_list'] as $item){
// query to delete where item = $item
}
you have to name your checkboxes accordingly:
<input type="checkbox" name="check_list[]" value="…" />
you can then access all checked checkboxes with
// loop over checked checkboxes
foreach($_POST['check_list'] as $checkbox) {
// do something
}
ps. make sure to properly escape your output (htmlspecialchars())
<input type="checkbox" name="check_list[<? echo $row['Report ID'] ?>]" value="<? echo $row['Report ID'] ?>">
And after the post, you can loop through them:
if(!empty($_POST['check_list'])){
foreach($_POST['check_list'] as $report_id){
echo "$report_id was checked! ";
}
}
Or get a certain value posted from previous page:
if(isset($_POST['check_list'][$report_id])){
echo $report_id . " was checked!<br/>";
}
It's pretty simple. Pay attention and you'll get it right away! :)
You will create a html array, which will be then sent to php array.
Your html code will look like this:
<input type="checkbox" name="check_list[1]" alt="Checkbox" value="checked">
<input type="checkbox" name="check_list[2]" alt="Checkbox" value="checked">
<input type="checkbox" name="check_list[3]" alt="Checkbox" value="checked">
Where [1] [2] [3] are the IDs of your messages, meaning that you will echo your $row['Report ID'] in their place.
Then, when you submit the form, your PHP array will look like this:
print_r($check_list)
[1] => checked
[3] => checked
Depending on which were checked and which were not.
I'm sure you can continue from this point forward.

how do i select multiple mysql records based on checked checkboxes?

on page 1 i have a form, then on page 2 which is the processor file, i want to select records based on the checked checkboxes that were checked on page 1.
<form action="output.php" method="post">
<input type="checkbox" id="" class="" name="check_list[]" value="something" />
<input type="checkbox" id="" class="" name="check_list[]" value="something else" />
<input type="checkbox" id="" class="" name="check_list[]" value="yet another thing" />
<input type="checkbox" id="" class="" name="check_list[]" value="one more thing" />
<input type="checkbox" id="" class="" name="check_list[]" value="some name" />
<input type="checkbox" id="" class="" name="check_list[]" value="some other name" />
<input type="submit" value="Submit" name="submit">
</form>
the following foreach can display all the values of everything that was checked, but i don't know how to take it further into my sql select statement to select all the records that have a column field by that name.
foreach($_POST['check_list'] as $check) {
echo $check . '<br>';
}
lets say in a table called stuff there are these fields
id, first_title, second_title
so i want to do the following, but obviously this isn't the way to write it. this is the part i need help with.
SELECT * FROM stuff WHERE first_title = $check or second_title = $check
lets us further say that these records exist in the table...
id first_title second_title
-----------------------------------------
1 something something else
2 yet another thing one more thing
3 some name some other name
then lets say these checkboxes were checked:
<input type="checkbox" id="" class="" name="check_list[]" value="something" />
<input type="checkbox" id="" class="" name="check_list[]" value="one more thing" />
so what i want to happen is for my select statement to select record 1 and record 2 and not record 3, because "something" is in the first_title column of the first record, and "one more thing" is in the second_title of the second record, and nothing was checked that is in third record.
i hope i gave as much detail as is needed. let me know if you need further explanation.
Use the SQL IN operator to test if a column is in a list of values. Here's how to write it with MySQLI:
$in_str = implode(', ', array_map(function($title) use ($con) {
return "'" . $con->real_escape_string($title) . "'";
}, $_POST['check_list']));
$sql = "SELECT * FROM stuff WHERE first_title IN ($in_str) OR second_title IN ($in_str)";
$result = $con->query($sql);
try this dynamic where condition in your code
<?php
$arr_where = array();
foreach($_POST['check_list'] as $check) {
$arr_where[] = " first_name='$check' OR last_name='$check' ";
}
$where_text = implode("OR", $arr_where);
$sql = "SELECT * FROM stuff WHERE ".$where_text;
?>

read only property for checkbox

Is there a read only property for a checkbox?
Because I can still tick on the checkbox even if I have this code, is the read only property only for text box? What's the code if you want the check box to be read-only?
<td><input name="stats1" type="checkbox" id="SSS" readonly="readonly" value="<?php echo $row["STAT"]; ?>" <?php echo $row["STAT"] ? 'checked="checked"' : ''; ?> >SSS</td>
The readonly attribute on HTML input elements actually means that the value is readonly.
You actually want to make a checkbox uncheckable, in that case grab Javascript:
<input type="checkbox" onclick="return false;">
Try disabled="disabled" instead ;)
here i have inserted 7 checkboxes and set their name as c1,c2....c7 in the html page
<input type="checkbox" name="c1" value="english" />
<input type="checkbox" name="c2" value="hindi" />
<input type="checkbox" name="c3" value="gujarati" />
.
.
.
<input type="checkbox" name="c7" value="Marathi" />
here we will use for loop with upto 7 times iteration
for($i=1;$i<=7;$i++)
{
echo $_GET['c'.$i] ."</br>";
}
here 'echo' statement will print values from gettig name of checkboxes c1 ...c7
Here $_GET['c'.$i] will print only its value if that checkboox is checked

Categories