I have a form with a select field:
<select class="indexSearchLocationList" name="locationList">
<option value="allLocations">Anywhere in London</option>
<option value="barking_and_dagenham">Barking and Dagenham </option>
<option value="barnet">Barnet</option>
What im trying to do is if the user chooses the option allocations (Anywhere in London) then I need to use that to select all when querying the database below is the php I currently have but how do I specifiy if that particular option is choosen:
$choosenLocation = $_POST['locationList'];
Just try this, if it is not meet your requirement, then add your requirement briefly as comment.
$choosenLocation = $_POST['locationList'];
if($choosenLocation == 'allLocations')
{
$query = "select * from TABLE ";
}
else
{
$query = "select * from TABLE WHERE condition="any_condition";
}
I think that may be
if(isset($_POST['locationList']) & $_POST['locationList']=='allLocations')
{
$query = "select * from TABLE ";
//based on the mysqli_ or PDO you execute
//fetch the row and display accordingly
}
Do you expect something similar,
$choosenLocation = $_POST['locationList'];
if(isset($choosenLocation) && $choosenLocation == 'allLocations'){
$query = "select * from TABLE ";
//based on the mysqli_ or PDO you execute
//fetch the row and display accordingly
}else{
$query = "select * from TABLE WHERE condition='your condition here' " ;
}
Related
I'm working on a project in order to be able to make a bespoke customizer for PC hardware in order to ensure everything is compatible. I am able to make the dropdowns and have validated that they can both be submitted successfully, although I need to pull 2 values and store them in 2 variables so I can use them to A (Store the ID of the CPU) and B (Store the appropriate socket).
<select id="pcat" name="pcat" onchange="autoSubmit();">
<option value="">-- Select Parent Category --</option>
<?php
//select parent categories. parent categories are with parent_id=0
$sql = "SELECT * FROM `cpus` ORDER BY `cpus`.`CPUName` ASC";
$result = dbQuery($sql);
while ($row = dbFetchAssoc($result)) {
echo ("<option value=\"{$row['CPUID']}\" " . ($pmenu == $row['CPUID'] ? " selected" : "") . ">{$row['CPUName']} ({$row['Cores']} Cores | {$row['Threads']} Threads | {$row['Frequency']}GHz)</option>");
}
?>
</select>
I would like to also add in the ability to store a variable called $sockets which would relate to the row called 'CPUSoc' in my database implementation.
Figured this out myself in a kind of... Strangely easy but stupid workaround. All I needed to do in order to pull out an additional variable was to have another query running with the CPUID like so:
$sql = "SELECT * FROM cpus WHERE CPUID = $cpumenu";
$result = dbQuery($sql);
while ($row = dbFetchAssoc ($result)){
($socket = $row['CPUSoc']);
}
And then using the stored $socket variable in the following query to use the stored result in the next query:
$sql = "SELECT * FROM `motherboards` WHERE Socket = '$socket' ORDER BY `motherboards`.`MoboName` ASC";
I'm trying to filter a mysql table of results using an HTML select dropdown box. I have the filter working but then I want to have the option to display 'all' again but all I get is a blank set of results. Then when I refresh, it keeps the $POST value, I'd like that to be reset so 'all' of my results would show as default?? Cheers!
$sql = "SELECT * FROM books";
if(isset($_POST['value'])) {
$catvalue = $_POST['value'];
$sql = "SELECT * FROM books WHERE category = '$catvalue'";
} else if($catvalue == 'all'){
$sql = "SELECT * FROM books";
}
$rs_result = mysql_query ($sql);
Bellow my select filter form.
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='form_filter' >
<select name="value">
<option value="All" selected="selected">All</option>
<option value="Design">Design</option>
<option value="Photography">Photograpy</option>
</select>
<br />
<input type='submit' value = 'Filter'>
</form>
<option value="All" selected="selected">All</option>
You have used "All" with an uppercase "A". PHP is case sensitive so your php code should be like this:
if(isset($_POST['value']) and $_POST['value']!='All') {
$catvalue = addslashes($_POST['value']);
$sql = "SELECT * FROM books WHERE category = '$catvalue'";
} else {
$sql = "SELECT * FROM books";
}
$rs_result = mysql_query ($sql);
$sql = "SELECT * FROM books";
if(isset($_POST['value']) and $_POST['value']!='All') {
$catvalue = addslashes($_POST['value']);
$sql = "SELECT * FROM books WHERE category = '$catvalue'";
}
$rs_result = mysql_query ($sql);
Refreshing the page will resend the same request.
A way to avoid that is to use the "Post/Redirect/Get" pattern.
It's well explained there : http://en.wikipedia.org/wiki/Post/Redirect/Get
$_POST['value'] always have a value on your code.
you may use the condition
if ( $_POST['value'] === 'All' ) {// select all}
$sql = "SELECT * FROM books";
if(isset($_POST['value'])) {
$catvalue = $_POST['value'];
if($catvalue != "All")
$sql = "SELECT * FROM books WHERE category = '$catvalue'";
}
$rs_result = mysql_query ($sql);
First of all, you are writing inline queries which is not a safe way to work. This can lead to SQL injection. Bind the parameters and pass them safely in to the query.
As far as you logic is concerned, you will have to do this
$sql = "SELECT * FROM books ";// should always fetch all the books
if(isset($_POST['value'])) {
$catvalue = $_POST['value'];
if($catvalue != 'All') // if value is not "all", filter it
$sql = "SELECT * FROM books WHERE category = '$catvalue'";
}
I am trying to search in MySql db based on user input something like below.
User may/not select from the below fields
<select name="field_one" id="f1">
<option value="AA">AA</option>
<option value="BB">BB</option>
<option value="CC">CC</option>
</select>
<select name="field_two" id="f2">
<option value="11">11</option>
<option value="22">22</option>
<option value="33">33</option>
</select>
if User selects only 'field_one', then mysql_query should filter only based on 'field_one'. In this case there can be four combination
Filed_one is slected and filed_two is not selected
Field_One is not selected and field_two is selected
Filed_one is not selected and Filed_two is not selected
Field_one is selected and Field_Two is selected
What is the best and efficient methode to make this search?
I tried with 'case .... break;' and 'if', but when the first condition is met, code stops and does not check the next conditions
Can any one give me a clue? Thanks in advance for the help....
try this:-
$query = "select * from table_name where 1 ";
if(!empty($_POST['field1']) ) {
$query .= " AND field1 like '".trim($_POST['field1'])."'";
}
if(!empty($_POST['field2'])) {
$query .= " AND field2 like '".trim($_POST['field2'])."'";
}
// and so on
$result = mysql_query($query);
please use escape string also http://php.net/mysql_real_escape_string
<?php
$sql = "SELECT * FROM table_name WHERE 1";
if(isset($_POST)){
if(isset($_POST['field_one'])){
$sql.= 'AND field_one'= $_POST['field_one'];
}
if(isset($_POST['field_two'])){
$sql.= 'AND field_two'= $_POST['field_two'];
}
}
mysql_query($sql);
?>
Example, not tested and needs lots of variable processing against SQL injection
$where = "";
$bits = array();
$bitset=false;
if(isset($_POST['field_one') && strlen($_POST['field_one')) > 0)
{
$bitset = true;
$bits[] = " field1 = $_POST['field_one')"
}
if(isset($_POST['field_two') && strlen($_POST['field_two')) > 0)
{
$bitset = true;
$bits[] = " field2 = $_POST['field_two')"
}
if($bitset)
{
$where = implode(", " $bits);
}
$sql = "select * from table " . $where;
You can also use PDO & param binding to avoid SQL Injection : http://www.php.net/manual/fr/pdostatement.bindparam.php
I have a drop downmenu on a page, after users add a content to the db,
i do not want the specific value that was added
from the dorpdown menu to show in the list again.
I do not want to delete that specific value from the dropdown table.
Your help will do.
Here is my code below:
<?php
$query = "SELECT * FROM vreg_no order by vreg desc";
$rs = mysql_query($query);
while($row = mysql_fetch_assoc($rs))
{{
$_SESSION['svregx'] = $row['vreg'];
}}
?>
<select name="svreg" class="bodytxt" id="svreg">
<option>Select Vehicle #</option>
<?php
$query = "SELECT * FROM vreg_no order by vreg desc";
$rs = mysql_query($query);
while($row = mysql_fetch_assoc($rs))
{{
$vreg = $row['vreg'];
if($_SESSION['svregx'] == $vreg){
//do nothing
}
elseif($_SESSION['svregx'] != $vreg){
echo"<option value='$vreg'>$vreg</option>";
}else{}
}}
?>
</select>
You are executing the same query twice.
The first one should be something like:
$query = "SELECT * FROM vreg_no WHERE user_id = YOUR_USER_ID";
or probably a join depending on your database structure.
Than you can add all values to an array and use something like in_array to check if this value exists for a certain user.
And you should dump the deprecated mysql_* functions and switch to prepared statements with bound variables in PDO or mysqli.
If the issue is not producing duplicate user generated content, wouldn't it just be an issue of issuing a DISTINCT query?
$query = "SELECT DISTINCT vreg FROM vreg_no order by vreg desc";
Am facing troubles in this code, i just want to get all data from table row if the user selected "show all" from the select drop menu.
here is the select menu !
so, this menu grab data from this table, but if he selects All, what is the suitable code to echoing in between option value :)
<b>speciality:</b> <select id="main_mav" name="speciality">
<option value="none">Select speciality:</option>
<option value=""> All specialities </option>
<?php
$result = mysql_query('SELECT speciality FROM visits') or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row['speciality'].'">'.$row['speciality'].'</option>';
}
?>
</select><br />
That's the Submit form !
if ($region=="All regions" ){
$region=$_POST['""'];
}
else ( $region=$_POST['region']);
$date1 =$_POST['from_date'];
$date2 = $_POST['to_date'];
$product=$_POST['product'];
$speciality=$_POST['speciality'];
$type=$_POST['visit_type'];
sql="SELECT id, customer_name, seller_1_name, seller_2_name FROM visits Where (speciality ='$speciality') AND (visit_type ='$type') AND (product ='$product') AND (region ='$region') AND (visit_date BETWEEN '$date1' AND '$date2')";
$result=mysql_query($sql); ## This line is new.
$num=mysql_numrows($result);
$row = mysql_fetch_array($result);
What's the correct code to enter if user selected " show all in drop menu " ?!
You really need to sanitize your inputs, at least with mysql_real_escape_string!
On to your actual question: just check if $speciality is empty, and generate a different query without the (speciality ='$speciality') condition.
Since your HTML referenced 'specialties' and your PHP referenced 'regions' I'm gonna just stick with 'regions', but here's the idea.
if ($region=="All regions" ){
$sql = 'SELECT id, customer_name, seller_1_name, seller_2_name, FROM visits';
} else {
$region = mysql_real_escape_string($_POST['region']);
$date1 = mysql_real_escape_string($_POST['from_date']);
$date2 = mysql_real_escape_string($_POST['to_date']);
$product = mysql_real_escape_string($_POST['product']);
$speciality = mysql_real_escape_string($_POST['speciality']);
$type = mysql_real_escape_string($_POST['visit_type']);
$sql = "SELECT id, customer_name, seller_1_name, seller_2_name FROM visits Where (speciality ='$speciality') AND (visit_type ='$type') AND (product ='$product') AND (region ='$region') AND (visit_date BETWEEN '$date1' AND '$date2')";
}
$result = mysql_query($sql); ## This line is new.
$num = mysql_numrows($result);
$row = mysql_fetch_array($result);