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'";
}
Related
I want to write an IF statement for a dynamically generated dropdown menu, but I keep getting errors or it doesn't just work, what I am trying to do is compare two variables to see if they match from a data in the database
$loma = "Asokoro";
$row['locales'] is from locality table in the database
$row['locales'] = "Asokoro";
$row['locales'] = "Dutse";
$row['locales'] = "Mari";
$row['locales'] = "Cook";
That means if $loma which is Asokoro matches $row['locales'] = "Asokoro"; select it as the option menu
<select name="checkout_area_name" id="checkout_area_name" required>
$query = "SELECT * FROM `locality` WHERE state_name = '$hi_state'";
$sql = mysqli_query($con, $query) or die(mysqli_error($con, $query));
$r = ' <option value="">Please Choose Locality</option>';
?>
<?php
while ( $row = mysqli_fetch_assoc($sql))
{
?>
<?php $r = $r . '<option value="'.$loma.'" if ("'.$loma.' == '.$row["locales"].'") selected="selected" >'.$row['locales'].'</option>'; ?>
<?php
}
echo $r;
?>
</select>
I am trying to select the options menu that has $loma and $row['locales'] matching but I keep getting errors or when I console.log, it does not produce the result i want.
You are outputting php script as html markup, try changing your code to:
<select name="checkout_area_name" id="checkout_area_name" required>
$query = "SELECT * FROM `locality` WHERE state_name = '$hi_state'"; $sql = mysqli_query($con, $query) or die(mysqli_error($con, $query)); $r = '
<option value="">Please Choose Locality</option>'; ?>
<?php
while ( $row = mysqli_fetch_assoc($sql))
{
$r = $r . '<option value="'.$loma.'" '.(($loma==$row["locales"])?'selected':'').'>'.$row['locales'].'</option>';
}
echo $r;
?>
</select>
Can someone correct the code for filter a record by its name. I know the query but perhaps I'm not implementing it properly.
Here is my code. I want to either search by city or simply put a name in textbox to search an hospital. search-by-name is for an input field where I am supposed to write the name I want to search from database. I want to make both options available. How should I implement it correctly, as this one won't work for me.
if (isset($_POST['search'])) {
if (isset($_POST['search-by-city'])) {
$city_id = $_POST['search-by-city'];
$query = "SELECT * FROM `hospitals` WHERE `City_ID` LIKE '$city_id'";
$result = mysqli_query($con,$query);
if (isset($_POST['search-by-name'])) {
$hospital_name = $_POST['search-by-name'];
$query = "SELECT * FROM `hospitals` WHERE `Name` LIKE '$hospital_name'";
$result = filterTable($query); {
if (mysqli_num_rows($result) == 0) {
echo '<div class="col-md-12"> <h2>No recod Found</h2> </div> ';
}
}
}
while($row = mysqli_fetch_array($result)){
$city_id = $row[3];
$query = "SELECT `Name` FROM `cites` WHERE `ID` LIKE '$city_id'";
$result2 = mysqli_query($con,$query);
$row2 = mysqli_fetch_row($result2);
$city_name = $row2[0];
echo '<div class="col-md-4"><h3>'.$row[1].'</h3><h4>'.$city_name.'</h4><h4>'.$row[2].'</h4><h5>'.$row[3].'</h5><h5>'.$row[4].'</h5>
';
}
}
I'm guessing your query should be:
"SELECT * FROM `hospitals` WHERE `Name` LIKE '%$hospital_name%'
Checkout the mySQL manual on string comparison.
Also please don't use $_POST variables directly in SQL queries, that is a major security issue. (Search for sql-injection.)
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 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' " ;
}
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);