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.
Related
I have been searching this for the last couple of hours and there are a few very similar questions and answers but none are quite working for my situation. What I'm trying to do is insert a list of songs set to the same value for two columns, one per row, and with a couple of options set via radio buttons. The latter is the reason why I can't just write all of the songs in a textarea and split them.
This is what the form looks like (obviously not fully designed)
There are only three there now while I get it working, in reality an unlimited amount may be added via javascript.
And the code:
<form action="" method="post">
<label>Session ID</label>
<input type="text" name="session-id"><br>
<label>Songs</label><br>
<input type="text" name="song-key[]"><input type="checkbox" name="is-partial[]"><input type="checkbox" name="is-jam[]"><br>
<input type="text" name="song-key[]"><input type="checkbox" name="is-partial[]"><input type="checkbox" name="is-jam[]"><br>
<input type="text" name="song-key[]"><input type="checkbox" name="is-partial[]"><input type="checkbox" name="is-jam[]"><br>
<input type="text" name="update-date" value="<?php echo date(" Y-m-d H:i:s ");?>" hidden readonly><br>
<input type="submit" name="submit" value="Submit">
</form>
The desired result, assuming the ID has been set and a unique song entered for each of the them, and whatever variety on the radio buttons, would be three table rows. session-id and update-date would all be the same value, the rest would be unique based on entry.
This is what I currently have, but it only inserts the last of the three songs.
for ($i=0; $i < count($_POST['song-key']); $i++ ) {
$sessionkey = mysqli_real_escape_string($connection, $_POST["session-key"]);
$songkey = mysqli_real_escape_string($connection, $_POST["song-key"][$i]);
$partial = mysqli_real_escape_string($connection, isset($_POST['is-partial'][$i])) ? 1 : 0;
$jam = mysqli_real_escape_string($connection, isset($_POST['is-jam'][$i])) ? 1 : 0;
$updated = mysqli_real_escape_string($connection, $_POST["update-date"]);
$sql = "INSERT INTO session_songs (session_key, song_key, is_partial, is_jam, last_updated)
VALUES ('$sessionkey', '$songkey', '$partial', '$jam', '$updated')";
}
What do I need to change to ensure all three (or more) are entered?
If you alter your input names, you can get a more useful $_POST array.
<input type="text" name="songs[0][key]">
<input type="checkbox" name="songs[0][is-partial]">
<input type="checkbox" name="songs[0][is-jam]"><br>
<input type="text" name="songs[1][key]">
<input type="checkbox" name="songs[1][is-partial]">
<input type="checkbox" name="songs[1][is-jam]"><br>
<input type="text" name="songs[2][key]">
<input type="checkbox" name="songs[2][is-partial]">
<input type="checkbox" name="songs[2][is-jam]"><br>
With specified keys, the checkbox type inputs will match up properly with the text inputs, where they way you have it now, they will not*, because only checked checkboxes are submitted to PHP. (Try only checking "is-partial" in the second and "is-jam" in the third row, and then var_dump($_POST), and you'll see that they both have index 0.)
If your form is structured like that, you can insert your records using a foreach loop instead of a for loop.
foreach ($_POST['songs'] as $song) {
$key = $song['key'];
$partial = isset($song['is-partial']) ? 1 : 0;
$jam = isset($song['is-jam']) ? 1 : 0;
// do the insert inside the loop rather than just building the SQL
}
The reason you're currently only getting the last one is that you're defining the SQL string inside the loop, but without executing the statement in the loop, you'll only get the values from the last iteration of the loop. Move your query execution inside the loop and you should get all three rows.
*unless they are all checked
running the following query ($box is yes or no from previous page):
$raw_results = mysqli_query($con,"
SELECT *
FROM `tbl_case`
WHERE `self_paid` ='$box'
AND `case_date_closed` BETWEEN '$startdate' AND '$enddate'
");
It displays all the fields correctly, and finishes with another checkbox
with the radio button below, if selected yes, it should set yes to paid_state on all previously returned $id'
<h1> Would you like to set these as paid to self ?
<form>
<form action="selfpaid.php" method="POST">
<input type="radio" name="state" value="yes" checked> Yes<br>
<input type="radio" name="state" value="no"> no<br>
<input type="submit" style="margin-left:80px; width:150px; height:30px;" value="Set state" />
</form>
<?php
if(isset($_POST['state']) && $_POST['state'] == "yes")
{
mysqli_query($con,"UPDATE tbl_case SET self_paid = 'yes' WHERE id = '$id'");
echo "Setting paid statesucessfull.";
}
else{
echo "paid_state not set";
}
?>
i am guessing it needs to be pulled from the db, and put all those $id in another array var, but i can;t figure out how, and then loop over that array .. can someone point me in the right direction ?
I have created a contact form on my website using php and some of the values, more specifically the ones that have a checkbox-type input, are not send to my DB. I think there must be something wrong with the way I use the checkboxes or something. Upon submitting a form I get several notices like such:
Notice: Undefined index: ironing in
/Applications/XAMPP/xamppfiles/htdocs/wordpress/demo.php on line 79
Notice: Undefined variable: POST in
/Applications/XAMPP/xamppfiles/htdocs/wordpress/demo.php on line 94
The rest of the code seems to work fine and the data is loaded into my DB.
Here is some of my code in the demo-form.php file that I think is wrong:
<h4>i) Cleaningservice:</h4>
<p>Experience (in years): <input type="number" name="cleanExpInYears"/> </p>
<p>Skills: <br>
<input type="checkbox" name="basic" value="yes"> Basic cleaning<br>
<input type="checkbox" name="kitchen" value="yes"> Kitchen tools<br>
<input type="checkbox" name="laundry" value="yes"> Laundry<br>
<input type="checkbox" name="ironing" value="yes"> Ironing<br>
<input type="checkbox" name="window" value="yes"> Windows<br>
<p> Please tell us about other skills: <input type="text" name="other_clean"/> </p>
</p>
And here is the corresponding demo.php file that connects to the DB and stores the data there:
/* Cleaningservice data is gathered... */
$clean_value1 = $_POST['cleanExpInYears'];
$clean_value2 = $_POST['basic'];
$clean_value3 = $_POST['kitchen'];
$clean_value4 = $_POST['laundry'];
$clean_value5 = $_POST['ironing'];
$clean_value6 = $_POST['window'];
$clean_value7 = $_POST['other_clean'];
/* ...and inserted into the table cleaningservice */
$sql = "INSERT INTO cleaningservice (applicant_id, cleanExpInYears, basic, kitchen, laundry, ironing, window, other)
VALUES (LAST_INSERT_ID(),'$clean_value1', '$clean_value2', '$clean_value3', '$clean_value4', '$clean_value5', '$clean_value6', '$clean_value7')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
What am I missing here ??
Thanks in advance
The browser only passes the checkbox to the PHP script if the checkbox was actually checked.
So in PHP you have to check that the $_POST variable actually exists before using its contents, and provide the UNCHECKED i.e. default option if it does not exist.
/* Cleaningservice data is gathered... */
$clean_value1 = $_POST['cleanExpInYears']);
$clean_value2 = isset($_POST['basic']) ? $_POST['basic'] : 'No';
$clean_value3 = isset($_POST['kitchen']) ? $_POST['kitchen'] : 'No';
$clean_value4 = isset($_POST['laundry']) ? $_POST['laundry'] : 'No';
$clean_value5 = isset($_POST['ironing']) ? $_POST['ironing'] : 'No';
$clean_value6 = isset($_POST['window']) ? $_POST['window'] : 'No';
$clean_value7 = $_POST['other_clean'];
To avoid error messages like `array index does not exist you should check all POST/GET values exist like this anyway.
/* Cleaningservice data is gathered... */
$clean_value1 = isset($_POST['cleanExpInYears']) ? $_POST['cleanExpInYears'] : '';
$clean_value2 = isset($_POST['basic']) ? $_POST['basic'] : 'No';
$clean_value3 = isset($_POST['kitchen']) ? $_POST['kitchen'] : 'No';
$clean_value4 = isset($_POST['laundry']) ? $_POST['laundry'] : 'No';
$clean_value5 = isset($_POST['ironing']) ? $_POST['ironing'] : 'No';
$clean_value6 = isset($_POST['window']) ? $_POST['window'] : 'No';
$clean_value7 = isset($_POST['other_clean']) ? $_POST['other_clean'] : '';
You must switch between name and values
<p>Skills: <br>
<input type="checkbox" value="basic" name="yes"> Basic cleaning<br>
<input type="checkbox" value="kitchen" name="yes"> Kitchen tools<br>
<input type="checkbox" value="laundry" name="yes"> Laundry<br>
<input type="checkbox" value="ironing" name="yes"> Ironing<br>
<input type="checkbox" value="window" name="yes"> Windows<br>
<p> Please tell us about other skills: <input type="text" name="other_clean"/> </p>
</p>
and in PHP side, you will find your selected values in var_dump($_POST['yes'])
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 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)")