storing data from multiple select form to MYSQL using PHP - 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.

Related

Using $_POST to get select option value from more than two dropdown lists HTML

How can I fill a MySql query using php's $_POST from more than two dropdown lists?
Lets say I have the following three dropdowns in a HTML form:
<form method="post" action="myphpscript.php">
<select name="country">
<option value="1">Belgium</option>
<option value="2">Canada</option>
<option value="3">Spain</option>
</select>
<select name="year">
<option value="2001">First</option>
<option value="2002">Second</option>
<option value="2003">Third</option>
</select>
<select name="virus">
<option value="hiv">HIV</option>
<option value="hcv">HCV</option>
<option value="hbv">HBV</option>
</select>
<input type="submit" value="Submit the form"/>
</form>
Then I have my query:
<?php
$query=sprintf("select country, year, virus from f_report where country='%s' and year ='%s' and virus ='%s'",
mysqli_escape_string($con,$_POST["country"]),
mysqli_escape_string($con,$_POST["year"]),
mysqli_escape_string($con,$_POST["virus"]));?>
How can I get this query to work if, for example, the user selected only "country"?
There are two ways you can go about this.
you can assign default values by verifying $_POST['key'] is empty for example
$_POST['year'] = isset($_POST['year']) ? $_POST['year'] : 'default value';
or
Set the optional fields to null in your database.
I hope it helps.

How do I use PHP to query my SQL database using SELECT tag in html

Basically I'd like to use the choices which the user has selected from a different select tags, and in essence store these as variables which I can then use to query my database in SQL.
My HTML code is here:
<div id ="search_elements">
<img src="UniSelect.jpeg">
<select>
<option selected disabled>Select a university</option>
<option value="ucl">UCL</option>
<option value="kings">Kings College</option>
<option value="imperial">Imperial College</option>
<option value="lse">London School of Economics</option>
</select>
<img src="PriceSelect.jpeg">
<select>
<option selected disabled>Select a weekly rent price</option>
<option value="50">0-£50</option>
<option value="100"> £100-£150</option>
<option value="150">£150-200</option>
<option value="200"> £200+</option>
</select>
</div>
And the type of php i would be looking to use would be:
//$con=mysqli_connect("localhost","adam","YjM3ZTYwOTQ5OWRmYWZh","adam_...");
//if (mysqli_connect_errno())
// {
// echo "Failed to connect to MySQL: " . mysqli_connect_error();
// }
// Perform queries
//$sql=mysqli_query($con,"SELECT CONTENT FROM Blog WHERE ID = 01");
//$result=mysqli_fetch_assoc($sql);
//echo $result['CONTENT'];
//mysqli_close($con);
To make it clear once more, I want to use the different values which the user selects, upon clicking a search button, have these query results shown in a table.
This solution a little differs from yours because you have no provided your form, submit button, etc. but in general, after a few changes, it should work too:
<form method="post" action="">
<img src="UniSelect.jpeg">
<select name="university">
<option selected disabled>Select a university</option>
<option value="ucl">UCL</option>
<option value="kings">Kings College</option>
<option value="imperial">Imperial College</option>
<option value="lse">London School of Economics</option>
</select>
<img src="PriceSelect.jpeg">
<select name="rent_price">
<option selected disabled>Select a weekly rent price</option>
<option value="50">0-£50</option>
<option value="100"> £100-£150</option>
<option value="150">£150-200</option>
<option value="200"> £200+</option>
</select>
<input type="submit" value="Submit form">
</form>
And now, to get values of these (something like this and I recommend to place it above your form):
if (!empty($_POST)) {
// Checking connection in here
$university_id = mysqli_real_escape_string($con, $_POST['university']);
$rent_price = mysqli_real_escape_string($con, $_POST['rent_price']);
$sql = mysqli_query($con, "SELECT * FROM university WHERE name = '".$university_id."'");
$result = mysqli_fetch_assoc($sql);
// Same thing goes for rent price
}

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).

passing values to sql through check boxes and dropdowns

How can i pass Drop down values to sql database and also the check box for example if a user selects English and maths than the value inserted in to the database would be 1 or else the value would be 0
<form>
<p id="p1">Select Your Year</p>
<select id="year_sel">
<option value="blank"></option>
<option id="primary" value="primary">Primary</option>
<option value="1">Year one</option>
<option value="2">Year two</option>
<option value="3">Year Three</option>
</select>
<input type="checkbox" name="Math" value="Math">Math<br>
<input type="checkbox" name="English" value="English">English<br>
<input type="checkbox" name="HealthScience" value="HealthScience">Health Science<br>
<input class="sub_reg" type="submit" value="Register Subjects" />
</form>
this is how my database looks like
First, your <select id="year_sel"> needs a name attribute to post ->
<select id="year_sel" name="year_sel" >
Since you are using <form> the default is get, so you would get the selected value in $_GET
$year_sel = $_GET['year_sel'];
If you changed it to
<form method="post">
then you would get it in $_POST
$year_sel = $_POST['year_sel']
Second, checkboxes are only posted if checked, so you can use isset() to set a value using a ternary -
$math = isset($_GET['Math']) ? 1 : 0;
$english = isset($_GET['English']) ? 1 : 0;
...[rest of your checkboxes]...
swap $_GET/$_POST like the select
$math = isset($_POST['Math']) ? 1 : 0;
$english = isset($_POST['English']) ? 1 : 0;
<select id="year_sel" name="year_sel">
<option value="primary">Primary</option>
<option value="1">Year one</option>
<option value="2">Year two</option>
<option value="3">Year Three</option>
</select>
on your form action something.php file use this to get the select value
$language = $_POST["year_sel"]; // chose whetever your form method
establish the database connection please do refer some tutorials (if you don't know )
write the mysqli insert command like
$SQL = "INSERT INTO yourtable (language) VALUES ('$language')";
mysqli_real_escape_string($sql);
$result = mysqli_query($sql) or die (mysqli_error());
now you can insert this value in your mysql or any database table
this is not tested
<form>
<p id="p1">Select Your Year</p>
<select id="year_sel" name="year_sel">
<option value="blank"></option>
<option id="primary" value="primary">Primary</option>
<option value="1">Year one</option>
<option value="2">Year two</option>
<option value="3">Year Three</option>
</select>
<?php
$variable = $_POST["year_sel"];
?>
Should mention the HTML ELEMENT name to get the value.

Create a HTML Form to Search SQL Database

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 :-) ;-)

Categories