Create a HTML Form to Search SQL Database - php

I have a HTML Form with Drop Down options which pass the values to php sql query I have on the same page as the form. This works if I use the values with LIKE statements but when I try to use the posted values with BETWEEN or >= it doesnt work. If i try with the actual values it does work. Could someone advise on what I am doing wrong. I have posted my HTML Form with the PHP code for the SQL Query.
HTML:
<html><form action="#" method="GET"></select>
<select name="MinPrice">
<option value="">Min Price</option>
<option value="">No Min</option>
<option value="500">£500</option>
<option value="600">£600</option>
<option value="700">£700</option>
<option value="800">£800</option>
<option value="900">£900</option>
<option value="">Show All</option>
</select>
<select name="MaxPrice">
<option value="">Max Price</option>
<option value="">No Max</option>
<option value="500">£500</option>
<option value="600">£600</option>
<option value="700">£700</option>
<option value="800">£800</option>
<option value="900">£900</option></select></form></html>
PHP:
<?php
mysql_connect("****", "****", "****");
mysql_select_db("****");
$sql = mysql_query("SELECT * FROM Product WHERE Price >= '%$_GET[MinPrice]%'");
echo "<h3>...Something...";
Or
<?php
mysql_connect("****", "****", "****");
mysql_select_db("****");
$sql = mysql_query("SELECT * FROM Product WHERE Price BETWEEN '%$_GET[MinPrice]%' AND '%$_GET[MaxPrice]%'");
echo "<h3>...Something...";
i dont get any results back, but if i exchange the GET Values with actual numbers it works as I want. Anyone got any suggestions on this?
I have tried absolutely everything I know

why action is "#" ?
if your php code is another page put the name in actions :
ex:
<form action="selectproduct.php" method="GET">
or if php code is on html page set action :
<form action="<?php $PHP_SELF?>" method="GET">

sorry, I can't comment, but could you just change:
echo "<h3>...Something...";
to
echo $sql;
or
var_dump($sql)
and compare value of $sql when you use $_GET and use "actual numbers"
there must be difference :-) ;-)

Related

storing data from multiple select form to MYSQL using PHP

I am looking for help on storing data from multiple select form to MYSQL using PHP, here is my form
<form action="" method="post">
Choose your preferred browser
<select name="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</select>
Choose your preferred car
<select name="carlist">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<input type="submit">
</form>
$_POST['browser']; and $_POST['carlist']; each hold their own different value. So you can easily fetch each of their values.
$car = $_POST['carlist'];
$browser = $_POST['browser'];
Now it's just a matter of question where you wanna take it from there. F.ex. if you'd like to save it to a database;
// Just make it a standard to secure your database inputs
$car = mysqli_real_escape_string($conn, $_POST['carlist']);
$browser = mysqli_real_escape_string($conn, $_POST['browser']);
$sql = "INSERT INTO someTable (car, browser) VALUES ('".$car."', '".$browser."')";
mysqli_query($conn, $sql);
This is very simplified of course, but I hope it gives you an idea.

Filter search based on selected drop downs in PHP

I have a Search form to search products.
<form method="get" action="search.php">
<select name="minprice">
<option value="">Min price</option>
<option value="100">100</option>
<option value="200">200</option>
<option value="300">300</option>
<option value="400">400</option>
<option value="500">500</option>
</select>
<select name="maxprice">
<option value="">Max price</option>
<option value="100">100</option>
<option value="200">200</option>
<option value="300">300</option>
<option value="400">400</option>
<option value="500">500</option>
</select>
<select name="type">
<option value="">type</option>
<option value="electric">electric</option>
<option value="other">other</option>
</select>
<select name="brand">
<option value="">type</option>
<option value="asus">ASUS</option>
<option value="ms">MS</option>
<option value="other">other</option>
</select>
<input type="submit" name="search" value="search">
</form>
In search.php page, i need results based only on what selected.
If i selected single list, i need that result only.
Example : I selected min price=100 , then i need to select products with minimum price 100 only...
I have tried this method , but it is not easy in my case, i am sure there is another way to implement this.
Any experts??
Any help / Ideas greatly appreciated..
EDIT
if(isset($_GET['minprice'])
{
$minprice=$_GET['minprice'];
$query="SELECT * FROM products WHERE price>='$minprice'";
}
else if()....
{ }
I am currently doing this way, But on next conditions not taking...
My Questions:
Do only this way to get the results?
Any change needed on $_GET[''] method like !="" or empty().
First, change your form from get to post. While users can manipulate POST, it's far easier to manipulate GET.
<form method="post" action="search.php">
Second, evaluate all of the POST variables first, and then compose your database query.
$minprice = isset($_POST['minprice']) ? $_POST['minprice'] : null;
$maxprice = isset($_POST['maxprice']) ? $_POST['maxprice'] : null;
$type = isset($_POST['type']) ? $_POST['type'] : null;
$brand = isset($_POST['brand']) ? $_POST['brand'] : null;
NOTE: I'm not sure what database type you're using, but you should use prepared statements with these variables when building your query. It's also a good idea to limit the possible values in your variables -- so, for example, if you expect price to always be a decimal number, you should cast the string to float.
You can then use something like the following for your query. I'm going to assume you're using a mysql database.
"SELECT <columns> FROM products WHERE minprice >= ISNULL(<$minprice variable>,minprice) AND maxprice <= ISNULL(<$maxprice variable>,maxprice) AND type = ISNULL(<$type variable>,type) AND brand = ISNULL(<$brand variable>,brand)"
NOTE: the query above needs to be changed to handle parameter binding / prepared statements. Since I don't know your database flavor, I've given you the basic query structure. Also, you should SELECT the specific columns you need from your table, not SELECT * (which is a performance hit).

How to keep showing selected option from drop down list?

I have a drop down list where I select options
<form action="" method="POST" class="styled-select">
<select name="seasons" onchange='this.form.submit()'>
<option value="">Select a Season</option>
<option value="1">2002/2003</option>
<option value="2">2003/2004</option>
<option value="3">2004/2005</option>
<option value="4">2005/2006</option>
<option value="5">2006/2007</option>
<option value="6">2007/2008</option>
<option value="7">2008/2009</option>
<option value="8">2009/2010</option>
<option value="9">2010/2011</option>
<option value="10">2011/2012</option>
<option value="11">2012/2013</option>
<option value="12">2013/2014</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
You can see the list here footystat
I am using the following PHP
if(isset($_POST['seasons'])){ $seasonette = $_POST['seasons']; }
if(isset($_POST['year'])){ $yearette = $_POST['year']; }
if(isset($_POST['comp'])){ $competitionette = $_POST['comp']; }
if(isset($_POST['which'])){ $whichette = $_POST['which']; }
When I select something from the list, I want selected item in the list to continue showing. At the moment when I select (for example) 2013/2014, it will show the results but the drop down menu goes back to its original state instead of showing 2013/2014.
Get Option value selected when it gets posted value, like this,
<option value="1" <?php if(isset($_POST['seasons']) && $_POST['seasons'] == '1'){ ?> selected="selected" <?php } ?>>2002/2003</option>
Set value like this for each option
You can set the "selected" property to the option , just like you set a value !
<option value="8" selected>2009/2010</option>
Use a if statement in PHP to determine which one should be selected.
Thats because the page refreshes.
On page load check if there is post variable than match the value with each option's HTML and write selected attribute.
The shorter way is
<option value="1" <?php echo $_POST['seasons']==1?"selected":""; ?>2002/2003</option>

How do I Pass Values from One Form to Another?

I have this form:
<form name="summaryform">
<select name="category" id="category" style="width:500px;">
<option value="">Choose category...</option>
<option value="ActionScript">ActionScript</option>
<option value="AppleScript">AppleScript</option>
<option value="Asp">Asp</option>
<option value="BASIC">BASIC</option>
<option value="C">C</option>
<option value="C++">C++</option>
<option value="Clojure">Clojure</option>
<option value="COBOL">COBOL</option>
<option value="ColdFusion">ColdFusion</option>
<option value="Erlang">Erlang</option>
<option value="Fortran">Fortran</option>
<option value="Groovy">Groovy</option>
</select><br>
<select name="subcategory" id="subcategory" style="width:500px;">
<option value="">Choose sub category...</option>
<option value="Haskell">Haskell</option>
<option value="Java">Java</option>
<option value="JavaScript">JavaScript</option>
<option value="Lisp">Lisp</option>
<option value="Perl">Perl</option>
<option value="PHP">PHP</option>
<option value="Python">Python</option>
<option value="Ruby">Ruby</option>
<option value="Scala">Scala</option>
<option value="Scheme">Scheme</option>
</select><br>
<input type="text" name="comments" id="comments" value=" Enter request comments..." onfocus="clearText(this)" onblur="restoreText(this)" style="width:493px;color:#999;font-size:9pt;height:20px;">
On the form above, I have two dropdowns (name="categoryId" and "subcategoryid") and one textboxt (name="comments")
How do I pass the values from this form to another form?
Let's say below is the form I want to pass the values to:
<div>
<p>
<form name="summaryform" method='POST' action='process.php'>
<div style="font-size:12pt;">
value from one dropdown
value from the other dropdown
value from textbox
</form>
</p>
</div>
jQuery Method
If the forms are on the same page, you can use jQuery to get the values and write to the div:
var content = $('#category').val()+'<br>'+$('#subcategory').val()+'<br>'+$('#comments').val();
$('#div-to-write-to').html(content);
PHP Method
If the forms are on different pages, you will need to use PHP and the $_POST[] variable to pass the values you are looking for.
NOTE: for this method to work the page being POSTed to MUST be PHP. You will also need to add an action and method in the first form to POST to the second form.
This would be the code in the form on the page being POSTed to:
<?php echo $_POST['category'].'<br>'.$_POST['subcategory'].'<br>'.$_POST['comments']; ?>
A value can be passed between forms using the POST and GET methods. The first form (Which is sending the values) does not have this defined, so modify the first form to this -
<form name="summaryform" method='POST' action='process.php'>
Then, in process.php, put the following php code -
$dropdown_first = $_POST['category'];
$dropdown_second= $_POST['subcategory'];
$comment = $_POST['comments'];
After that, you can just use the variables as you see fit!
If you want just print the selected values from first form #zsaa14's
answer is good
If you want to print whole select list, autoselected your value, heres a code.
foreach($categories as $category) {
$selected = $category['name'] === $_POST['category'] ? 'selected="selected"' : '';
echo " <option value="{$category['name']}" {$selected}>{$category['name']}</option>" ;
}
NOTE: the variable names and keys are for example, use Yours. This is only fragment of code.
You can use post data fields to send the values to the next form

Creating select list from database

I am making select list from database table everything works fine only issue is in option value if the value consist of two words like "New York" it only returns "New". Here is the code
<? $country=intval($_GET['country']);
include('connect-db.php');
$query="SELECT CityName FROM cities WHERE CountryId='$country'";
$result=mysql_query($query);
?>
<select name="city">
<option value="">Select City</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['CityName']?>><?=$row['CityName']?></option>
<? } ?>
</select>
<option value="<?=$row['CityName']?>"><?=$row['CityName']?></option>
The " " .
You'll have to surrond the value in "", the HTML code thinks the York part is just another attribute of the option tag, so:
<option value="<?=$row['CityName']?>"><?=$row['CityName']?></option>
You haven't included the "" while giving the values. if you look at the html created for the select box by your code is something like
<option value="new" york>new york</option>
correct solution is given below, just included the quotation while giving value of option tag.
<? $country=intval($_GET['country']);
include('connect-db.php');
$query="SELECT CityName FROM cities WHERE CountryId='$country'";
$result=mysql_query($query);
?>
<select name="city">
<option value="">Select City</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value="<?=$row['CityName']?>"><?=$row['CityName']?></option>
<? } ?>
</select>
Just use "" for the value of value same as you write HTML tag.

Categories