I have a html form where there are two Input fields
say Caste (options are SC, ST and OBC) and Direction (options are north, south, east and west). Users will choose the options using check box.
<input name="ct" type="checkbox" value="SC">SC
<input name="ct" type="checkbox" value="ST">ST
<input name="ct" type="checkbox" value="OBC">OBC
<input name="dr" type="checkbox" value="N">North
<input name="dr" type="checkbox" value="S">south
<input name="dr" type="checkbox" value="E">East
<input name="dr" type="checkbox" value="W">West
I have also a database (name : PEOPLE) with column name Caste , Direction etc.
Now I want to run a sql query based on user's selection. For example
mysql_query("select * from PEOPLE where Caste='option/options selected by user' and Direction= 'option/options selected by user' ")
If user choose one option from each field then it is not a problem for me,
mysql_query("select * from PEOPLE where Caste='$_POST[ct]' and Direction= '$_POST[dr]' ")
but if they use multiple options , then how should I proceed.
Give name attribute to the checkboxes like this
<input type="checkbox" name="caste[]" value="SC" />
<input type="checkbox" name="caste[]" value="ST" />
<input type="checkbox" name="caste[]" value="OBC" />
On you php side you can use function implode to form caste into a string as shown below (considering you are doing a POST)
$caste_id = implode(",",$_POST["caste"]);
Where you read from the database you can transform the value from db to an array like this
$caste_array = explode(",",$row->caste_id);
I hope this helps
You can use WHERE IN
$caste_string = implode('","', $_GET['ct']);
$caste_string = '"'.$caste.'"';
$dir_string = implode('","', $_GET['dr']);
$dir_string = '"'.$caste.'"';
mysql_query("select * from PEOPLE WHERE Caste IN ($caste_string) AND Direction IN ($dir_string)")
Related
I've got a problem.
I don't know how to put the value of my checkbox in my MySQL database with PHP.
So :
<input type="checkbox" name="checkiard" value="IARD">
How to put the value "IARD" in my database ONLY if the checkbox is check ?
Thank you guys.
You can access the value of checkbox as any other input types. Its simply
if(isset($_POST['checkiard'])) {
$optionArray = implode(",", $_POST["checkiard"]);
}
Try this.
If you have more than one checkbox then you markup should be something like this
<input type="checkbox" name="checkiard[]" value="IARD-1">
<input type="checkbox" name="checkiard[]" value="IARD-2">
<input type="checkbox" name="checkiard[]" value="IARD-3">
you must keep the name same for all the checkbox.
here I have a table which its data come from database (Mysql DBMS)
In this table I can delete a book one by one but what I need is to delete multiple Book in once
Something like this
But Actually I have no Idea How to do That Can you guys do me a favor how to do that plz I will be thankful
Create a list of checkboxes with the same name using [] notation, but different values, ids of records presumably:
<input type="checkbox" name="record_id[]" value="42" />
<input type="checkbox" name="record_id[]" value="43" />
<input type="checkbox" name="record_id[]" value="44" />
<input type="checkbox" name="record_id[]" value="45" />
After you select some of them, they will be passed to a server as $_POST['record_id'] array.
Do a foreach:
foreach ($_POST['record_id'] as $id) {
// delete record with $id here
}
Or implode'em, for example:
$sql = "DELETE FROM `mytable` WHERE id IN (" . implode(', ', $_POST['record_id']) . ")";
I'm creating some simple search page for my little project. Its about list of Multifunctional devices and options on devices.
I have table devices and fields with device options:
option_hdd
option_fax
option_df (document feeder)
option_duplex
and so on... totally 8 option fields.
If device contain options in field is 1 if not 0.
I created small form with check boxess for all this option with GET method.
<form action="search.php" method="get" name="search">
<input name="HDD" type="checkbox" value="1" /> HDD<br />
<input name="Fax" type="checkbox" value="1" /> Fax<br />
<input name="DF" type="checkbox" value="1" /> DF<br />
...
<input name="Search" type="submit" />
</form>
I try to create some query but i don't get exact results.
If user want to check for devices which contain hdd and fax option he will check that two checkboxes and hit search, query need to return results with devices that contain checked options (not essential for other options)
i try with fallowing query's i found googling :)
SELECT * FROM devices WHERE option_hdd = '$_GET[HDD]' OR option_fax = '$_GET[Fax]' OR option_finisher = '$_GET[Df]' ......'
and also
SELECT * FROM devices WHERE option_hdd = '$_GET[HDD]' AND (option_fax = '$_GET[Fax]' OR option_finisher = '$_GET[Df]' ......);
but i do'n get wanted results... but this last is the most close-up...
Example i have 5 devices in DB for testing
All 5 devices have Hdd and 2 devices have Fax option
In form i check hdd and fax and i get result of all 5 devices, but correct results must be 2... cause only two devices have fax option...
Can somebody help me and give me correct query :)
Thanks
I suggest using 'AND' but...
If the checkbox is checked, the value will be 1. If the checkbox is not checked the value will be empty.
So you code will be like this:
SELECT * FROM devices WHERE option_hdd = '1' AND option_fax = ' ' AND option_finisher = '1';
Which will give you a wrong resultset.
Try this instead:
$hdd = (!empty($_GET['HDD'])) ? $_GET['HDD'] : 0;
$fax = (!empty($_GET['Fax'])) ? $_GET['HDD'] : 0;
$df = (!empty($_GET['DF'])) ? $_GET['HDD'] : 0;
$sql = "SELECT * FROM devices WHERE option_hdd = '".$hdd."' AND option_fax = '".$fax."' AND option_finisher ='".$df."'";
Another option is to build your query. Change the name of the checkboxes like in the example below:
<form action="" method="get" name="search">
<input name="option[HDD]" type="checkbox" value="1" /> HDD<br />
<input name="option[Fax]" type="checkbox" value="1" /> Fax<br />
<input name="option[DF]" type="checkbox" value="1" /> DF<br />
...
<input name="Search" type="submit" />
</form>
And see the phpexample:
$sql = 'SELECT * FROM devices';
if(isset($_GET['option']) && count($_GET['option']) > 0){
$c = 0;
$sql .= ' WHERE';
foreach ($_GET['option'] AS $key => $option) {
$sql .= ($c == 0) ? ' option_'.$key.'='.$option : ' AND option_'.$key.'='.$option;
$c++;
}
}
Another solution is to use this trick:
<input name="HDD" type="hidden" value="0" />
<input name="HDD" type="checkbox" value="1" /> HDD<br />
This way if PHP will revert to default 0 if it doesn't get the value from checkbox.
And please be careful with your data, don't use $_GET directly in SQL.
I am new to php & need little help here.
I've following code to INSERT multiple checkbox values into a table for same ID:
on my form I have:
<input type="checkbox" name="subcat[]" value="Mech">Mech
<input type="checkbox" name="subcat[]" value="Ele">Ele
<input type="checkbox" name="subcat[]" value="Civ">Civ
<input type="checkbox" name="subcat[]" value="Air">Air
<input type="checkbox" name="subcat[]" value="BSL">BSL
I've checked "Mech", "Ele" and "BSL"
and PHP to insert:
$subcat = $_POST['subcat'];
for($i=0; $i<sizeof($subcat);$i++){
$sql1="INSERT INTO mst_scatadd (party_code,scid)
VALUES ('$pcode',$subcat[$i])";
mysql_query($sql1,$con);
}
db looks something like...
sctid | party_code | scid
---------------------------------
1 | 01S001 | Mech
2 | 01S001 | Ele
3 | 01S001 | BSL
4 | 01K207 | Main
Now, how can I retrieve these values from db to my form for the same ID and make them as checked if I want to edit the checkboxes?
Little help would be appreciated!!
Thanks in adv.
EDIT 1:
Hello again guys!
I've done with inserting and retrieving checkbox values using implode and explode and its working fine.
Thanks to #Antoniossss for good suggession to keep checkboxes values as numbers(make sure the column in db should be VARCHAR)
Thanks to #Barmar to explain use of explode & thanks to #MarkTWebsite to share idea of if-else
Code for inserting multiple checkbox values:
foreach ($_POST['subcat'] as $_subcat)
{
$checksub[] = $_subcat;
} $finalsub = implode(',', $checksub);
$sql="INSERT INTO vendor_mst(party_code, subcat) VALUES ('$pcode','$finalsub')";
My final code to retrieve checkbox values from db...
sql="SELECT * FROM vendor_mst WHERE party_code like '".$searchname."'";
$result=mysql_query($sql,$con);
while ($row=mysql_fetch_array($result, MYSQL_BOTH)){
$checksub = explode(',',$row['subcat']);
}
if(in_array("1",$checksub))echo '<input type="checkbox" name="subcat[]" value="1" checked >Mech'; else echo '<input type="checkbox" name="subcat[]" value="1">Mech';
if(in_array("2",$checksub))echo '<input type="checkbox" name="subcat[]" value="2" checked >Ele'; else echo '<input type="checkbox" name="subcat[]" value="2">Ele';
if(in_array("3",$checksub))echo '<input type="checkbox" name="subcat[]" value="3" checked >Civ'; else echo '<input type="checkbox" name="subcat[]" value="3">Civ';
if(in_array("4",$checksub))echo '<input type="checkbox" name="subcat[]" value="4" checked >Air'; else echo '<input type="checkbox" name="subcat[]" value="4">Air';
if(in_array("5",$checksub))echo '<input type="checkbox" name="subcat[]" value="5" checked >BSL'; else echo '<input type="checkbox" name="subcat[]" value="5">BSL';
Thanks to stackoverflow!
I would rather go in direction of threating checkboxes as binary flags and store their binary OR operation into DB as integer. It is really easy to implement, and will simplify record relations in your database. To do that you need to change values of checkboxes into 2^n values. For example
<input type="checkbox" name="subcat[]" value="1">Mech
<input type="checkbox" name="subcat[]" value="2">Ele
<input type="checkbox" name="subcat[]" value="4">Civ
<input type="checkbox" name="subcat[]" value="8">Air
<input type="checkbox" name="subcat[]" value="16">BSL
selecting Mech and Civ should be storen in DB as 5 (binary 00101), Air+BSL would be 24 (11000b) and so on. To revert this process you would just simply test every bit of stored value and check/uncheck corresponding boxes in loop. I hope that you will get my point.
EDIT: example code as requested.
Be aware that I AM NOT a PHP programmer so there could (and definetly will be) syntax errors but I will do my best
To get user input you have to simply add values of checked boxes
$selected=0;
foreach($POST['subcat'] as $checkedBox){
$selected=$selected+$checkedBox
}
So now variable $selected will have encoded status of all checkboxes
To check which boxes (I never have troubles with that word) should be check or not you have to test every bit of stored value in DB. I dont know how are you generating your view, so i will provide only a template, you have to figure it out how to apply it to your project
$valueFromDb= /// some value fetched from DB
$maxValue= //// the highest value of your checkboxes, in my eg. 16
for($testValue=1;$testValue<=$limit;$testValue=$testValue << 1){
$result=($valueFromDb & $testValue) > 0;
// do something with checked result for current checkbox
}
variable result above will have checked status (boolean value) of coresponding checkboxes. Each loop iteration coresponds to different checkbox in order from Mech to BSL one by one.
Alternatively you can print checkboxes directly and set them as checked with if inscruction
$valueFromDb= // fetched value
<input type="checkbox" name="subcat[]" value="1" <?php if($valueFromDb & 1 >0) echo('checked') ?php>Mech
<input type="checkbox" name="subcat[]" value="2" <?php if($valueFromDb & 2 >0) echo('checked') ?php>>Ele
<input type="checkbox" name="subcat[]" value="4" <?php if($valueFromDb & 4 >0) echo('checked') ?php>>Civ
.... and so on and on and on
I have just noticed that combining those 2 methods would result in generating checboxes dynamicly in loop so the code will be much more readable. All you would need is to provide map of checbox value labels:)
I hope that eg. above will definetlly clearify how to apply such solution.
<?php
$checksub = array('1','2','3');
?>
<form action="<?php echo site_url('index/do_upload') ?>" method="post" enctype="multipart/form-data">
<label>Select file : <input type="file" name="userfile"></label>
<input type="submit" name="action">
<input type="checkbox" name="subcat[]" value="1" <?php if (in_array(1,$checksub)){ echo "checked"; }?>/>
</form>
You will need to use the PHP if statement;
I don't really understand your db structure, else I would provide code however, if you use PHP if, for each checkbox, check in the database to see if it's selected then;
if it is selected
echo '<input type="checkbox" name="subcat[]" value="Mech" selected>Mech';
else;
echo '<input type="checkbox" name="subcat[]" value="Mech">Mech';
Do you understand what I mean?
Source; HTML/CSS/PHP Developer for 3 years.
You can retrieve the subcats with:
SELECT party_code, GROUP_CONCAT(scid) scids
FROM mst_scadd
GROUP BY party_code
Then when you're fetching the rows from the database, you can do:
$scids = array_flip(explode(',', $row['scids']));
That will create an associative array whose keys are the subcategories. Then your code for displaying the checkboxes can be something like:
foreach ($all_subcats as $sc) {
echo '<input type="checkbox" name="subcat[]" value="' . $sc . '" ' .
(isset($scids[$sc]) ? 'checked' : '') . '>' . $sc;
}
I've this simple form, when the price is set to 120 $:
<input type="checkbox" name="addon[book]" value="120" /> book
<input type="checkbox" name="addon[plane]" value="420" /> Plane
Could I send two values (USD & €) with thr form?
EDIT:
I added name="addon[book]" because I'll display the name too
EDIT 2:
PHP:
foreach($addon as $name => $value) {
echo 'your addons: '.$name;
}
$total = array_sum(array_map("intval", $_POST["addon"]));
echo 'the total '.$total.' $';
<input type="hidden" name="addon[book_USD]" value="250" />
<input type="checkbox" name="addon[book_EUR]" value="120" /> book
However NEVER trust the input from the form. Check the price in the backend code or users can change the price!
EDIT
What about json encoding?
<input type="checkbox" name="addon[book]" value="{"USD":250,"EUR":120}" /> book
EDIT2
To explode the values you'll have to use json_decode()
EDIT3
As others (and myself) already stated what you are trying to do (at least from the looks of it) is that you use the price from the input field as the price the user is going to pay.
That is not the best thing you can do, cause users can easily change the price of the input field.
I don't know your code but somehting like the following would be much better:
<input type="checkbox" name="addon[book]" value="book1">
And in your PHP code:
$prices = array('book1'=>array('USD'=>250, 'EUR'=>120),
'book2'=>array('USD'=>120, 'EUR'=>60),
);