Submitting multiple values in php to a search query - php

I'm sorry I'm asking something very basic but I'm beyond stuck. I have a search form with 3 fields: a blank field for search term data, a dropdown box, and a checkbox list. My page is functioning as intended for the search term box.
However, I don't know what I need to do to be able to send the select value (one select choice) and checkbox values to the processing form.
This is my current query:
SELECT nameid, productypetid, typeid, termsid, endsid, sourceid FROM listings WHERE nameid LIKE %s ORDER BY nameid ASC
This is the html form:
<form action="results2.php" method="get" name="form" class="Ccenter">
<p>Product Name:
<input name="namesearch" type="text" id="namesearch" title="namesearch" />
</p>
<p>
<label for="category">Category:</label>
<select name="category" size="1" id="category">
<option value="Any" selected="selected">Any</option>
<option value="Baby and Kids">Baby and Kids</option>
<option value="Beauty and Hair">Beauty and Hair</option>
<option value="Beverages">Beverages</option>
<option value="Cleaning">Cleaning</option>
<option value="Condiments">Condiments</option>
<option value="Cooking and Baking">Cooking and Baking</option>
<option value="Dairy">Dairy</option>
<option value="Frozen">Frozen</option>
<option value="Meat">Meat</option>
<option value="Medicine">Medicine</option>
<option value="Other">Other</option>
<option value="Pantry Foods">Pantry Foods</option>
<option value="Paper Products">Paper Products</option>
<option value="Pets">Pets</option>
<option value="Restaurants">Restaurants</option>
<option value="Snacks and Candy">Snacks and Candy</option>
<option value="Toiletries">Toiletries</option>
</select>
</p>
<p>
<label>
Type:
<input name="Type" type="checkbox" id="manufacturer" value="manufacturer" checked="checked">
Manufacturer</label>
<label>
<input name="Type" type="checkbox" id="store" value="store" checked="checked">
Store</label>

If you're accessing the namesearch text field like so:
$name = $_GET['namesearch'];
Then your category select field will be available via:
$category = $_GET['category'];
As for your checkboxes (manufacturer and store), they may or may not exist in the $_GET array. It depends on whether the user has checked them or not.
$manufacturer = isset($_GET['manufacturer']) ? $manufacturer : null;
$store = isset($_GET['store']) ? $store: null;

Related

Issue With option Tag In HTML While Storing in Database By PHP

I want to save value as Hsn/product code and option text as product.
i defined value for hsn because i want to show it on my active webpage as Selected option Hsn code.
but problem is that when i submit form, then i recieve same value in productName & ProductHSN in our Database
And i want to store productName as saree, lehga, etc
And ProductHSN as W1, M1,etc .
plase help
<select class="form-control" id="colTwo2" name="colTwo[]" data-
type="productName" required="">
<option value=""> --SELECT-- </option>
<option value="W1">Saree</option>
<option value="W2">Lehga</option>
<option value="G1">Kurti</option>
<option value="G2">Salwar Suit</option>
<option value="G3">Lagi</option>
<option value="G4">Hairam</option>
<option value="G5">Long Suit</option>
<option value="W3">Goun</option>
<option value="W10">Chunri</option>
<option value="M1">Suiting</option>
<option value="M2">Shirting</option>
<option value="G6">Jeans</option>
<option value="M4">T-Shirt</option>
</select>
You are getting same value because the value in option value="" is only sent. Not the option text.
You could use PHP explode function.
HTML
<select class="form-control" id="product" name="product" required>
<option value=""> --SELECT-- </option>
<option value="W1|Saree">Saree</option>
<option value="W2|Lehga">Lehga</option>
<option value="G1|Kurti">Kurti</option>
<option value="G2|Salwar Suit">Salwar Suit</option>
</select>
PHP
<?php
$result = $_POST['product'];
$result_explode = explode('|', $result);
$producthsn = $result_explode[0];
$productname = $result_explode[1];
?>
In the select box we are giving 2 options with a '|' separator to split the values. In action page result is exploded into an array. You can then insert each one separately in the database.

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
}

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.

php mysql Calculate price from database value with form selection

Hello I am really struggling with this. I was asked to develop a script to calculate oil price but cannot get it to work. I have been able to setup a form to update fuel price.
I have a table called fuel_price. In this table will be cost per litre of fuel which is stored under fueltype.
For example there are Four columns in the table ‘ID’ value=1, ‘Oil’ value= £0.50, ‘Diesel’ value=£0.85 and RedDiesel value=’£0.75’.
I need to multiply selected fuel type by quantity of litres selected within form.
Can anyone please guide me on what I am supposed to do??
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<select name="fueltype">
<option>- Select fuel type -</option>
<option value="Diesel">Diesel</option>
<option value="Red Diesel">Red Diesel</option>
<option value="Home Heating Oil">Home Heating Oil</option>
</select>
<select name="qtylitres">
<option>- Qty in Litres -</option>
<option value="100">100</option>
<option value="200">200</option>
<option value="400">400</option>
<option value="500">500</option>
<option value="900">900</option>
<option value="1000">1000</option>
</select>
<input type="hidden" name="id" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
include 'mysql_connect.php';
$stmt = $pdo->prepare("SELECT `Oil` from `fuel_price` WHERE id = '1'");
if (!$stmt->execute()) { die($stmt->errorInfo[2]); }
$row = $stmt->fetch();
$price = $row['Oil'];
echo $_POST['qtylitres'] * $price;
?>
Please help really unsure what to do??
maybe by putting
global $pdo;
in both mysql_connect.php and your file, just after the include statement will solve your issue

Categories