php mysql Calculate price from database value with form selection - php

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

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.

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
}

Using multiple PHP drop downs to filter MySQL table data

I am currently building a page that uses five separate drop down menus (static content) that when selected the combination of what is selected will filter my mysql query and display the correct results using those parameters.
I am a newbie in php and i don't know how to go about it
Here is the html code for index.php
<form action="search.php" method="post">
<p>Body Type</p>
<select class="form-control" name="bodytype">
<option value="" selected="selected">Any body type</option>
<option value="saloon">Saloons</option>
<option value="hatchback">Hatchbacks</option>
<option value="4 wheel drive">4 wheel drives</option>
<option value="station wagon">Station wagon</option>
<option value="pickup">Pickups</option>
<option value="motor bike">Motor bikes</option>
<option value="convertible">Convertibles</option>
<option value="bus">Buses, Danfos & Vans</option>
<option value="truck">Trucks</option>
</select>
<p>Condition</p>
<select class="form-control" name="condition">
<option value="" selected="selected">Any condition</option>
<option value="brand new">Brand new</option>
<option value="foreign used">Foreign used (Tokunbo)</option>
<option value="nigerian used">Nigerian used (Registered)</option>
</select>
<p>Fuel type</p>
<select class="form-control" name="fueltype">
<option value="" selected="selected">Any fuel type</option>
<option value="petrol">Petrol</option>
<option value="diesel">Diesel</option>
<option value="hybrid">Hybrid</option>
</select>
<p>Transmission</p>
<select class="form-control" name="transmission">
<option value="" selected="selected">Any transmission</option>
<option value="automatic">Automatic</option>
<option value="manual">Manual</option>
</select>
<p>Drive type</p>
<select class="form-control" name="drivetype">
<option value="" selected="selected">Any drive type</option>
<option value="2 wheel drive">2 wheel drive</option>
<option value="4 wheel drive">4 wheel drive</option>
</select>
<input name="search" type="submit" value="Search">
</form>`
This is the search.php
<body>
<?php
$selected_btype = $_POST['bodytype']; // Storing Selected Value In Variable
$selected_condition = $_POST['condition'];
$selected_fueltype = $_POST['fueltype'];
$selected_makemodel = $_POST['makemodel'];
$selected_transmission = $_POST['transmission'];
$selected_location = $_POST['location'];
$selected_dtype = $_POST['drivetype'];
$selected_year= $_POST['year'];
$selected_dsetup= $_POST['drivesetup'];
$query="SELECT * FROM vehicle WHERE(body_type=$selected_btype) OR
(condition='$selected_condition') OR (fuel_type='$selected_fueltype') OR
(make_model='$selected_makemodel') OR (transmission='$selected_transmission') OR
(location='$selected_location') OR (drive_type='$selected_dtype') OR
(year='$selected_year') OR (drive_setup='$selected_dsetup')";
$result=mysql_query($query);
if($result)
{
while ($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['sn']."</td>";
echo "<td>".$row['body_type']."</td>";
echo "<td>".$row['make_model']."</td>";
echo "<td>".$row['location']."</td></tr>";
echo "<td>".$row['year']."</td>";
echo "<td>".$row['condition']."</td>";
echo "<td>".$row['transmission']."</td>";
echo "<td>".$row['description']."</td></tr>";
echo "<td>".$row['images']."</td></tr>";
}
}else{
die(mysql_error());
}
?>
The output of search.php:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'condition='nigerian used') OR (fuel_type='') OR (make_model='peugeot'' at line 1
There are a few issues with your code:
Firstly, condition is a MySQL reserved word and needs to be wrapped in backticks.
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
Either wrap it in backticks or rename it to another word.
$query="SELECT * FROM vehicle WHERE(body_type=$selected_btype) OR
(`condition`='$selected_condition')
Notice where the error is pointing to:
for the right syntax to use near 'condition='nigerian used')
^
Then there's WHERE(body_type=$selected_btype)
The $selected_btype variable needs to be wrapped in quotes, since string values are involved.
WHERE(body_type='$selected_btype')
Footnotes:
You should consider using mysqli with prepared statements, or PDO with prepared statements, they're much safer, because your present code is open to SQL injection.
In your query, the body type selection is not encapsulated in quotes like others, but it is a string so it needs to be like this
$query="SELECT * FROM vehicle WHERE(body_type=<b>'</b>$selected_btype<b>'</b>) OR......

Submitting multiple values in php to a search query

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;

How can I stop an empty SQL query from displaying all results?

I am creating a meat packaging search form, users can search for different packages using multiple forms and dropdown boxes. I have a had a lot of problems but most of it is sorted now, I only need to create an "Any" search for the dropdown boxes and the problem of empty textboxes displaying all results.
Currently when a user sends a search, they may have entered in some other text boxes, but when one of the forms is left empty that automatically displays all of the results. I want it so when a search is sent and a box is empty, the code ignores that form and just checks the ones that have info inside of them.
here is my test code (not my current final form code):
<body>
<?php
$con = mysql_connect("localhost", "root", "");
mysql_select_db("delyn_db", $con);
if (!$con)
{
die("Could not connect: " . mysql_error());
}
$descrip = mysql_real_escape_string($_POST['descrip']);
$sql = "SELECT * FROM delyn WHERE description LIKE '%" . $descrip . "%'";
$r_query = mysql_query($sql);
if ($descrip === "")
{
echo 'Null value';
}
while ($row = mysql_fetch_array($r_query))
{
echo '<br /> Description: ' . $row['description'];
}
?>
</body>
Anyone have any ideas on how to stop this?
EDIT: Sorry here is my HTML with the search boxes. The above php is just where the values are sent.
<body>
<form action="form5null.php" method="post">
<label for="description">Description:</label> <input type="text" name="descrip">
<br>
<label for="trayheight">Trayheight:</label> <input type="text" name="height">
<br>
<label for="traywidth">Traywidth:</label> <input type="text" name="width">
<br>
<label for="traydepth">Traydepth:</label> <input type="text" name="depth">
<br>
<label for="trayrange">Trayrange:</label> <select name="trayrange">
<option value="BBQ">
BBQ
</option>
<option value="Dessert">
Dessert
</option>
<option value="Display">
Display
</option>
<option value="Meat">
Meat
</option>
<option value="Microwave">
Microwave
</option>
<option value="Party">
Party
</option>
<option value="Salad/Wet Pasta">
Salad/Wet Pasta
</option>
<option value="Snacks">
Snacks
</option>
<option value="Standard">
Standard
</option>
</select> <label for="traytype">Traytype:</label> <select name="traytype">
<option value="Open">
Open
</option>
<option value="Cavitised">
Cavitised
</option>
<option value="Lid">
Lid
</option>
<option value="Tray">
Tray
</option>
<option value="Coallition">
Coallition
</option>
<option value="Bowl">
Bowl
</option>
<option value="Hinge pack">
Open
</option>
<option value="Pot">
Pot
</option>
<option value="Base & Lid">
Base and Lid
</option>
<option value="Rectangular">
Rectangular
</option>
<option value="Specalist">
Specialist
</option>
</select>
<br>
<label for="trayshape">Trayshape:</label> <select name="trayshape">
<option value="Rectangular">
Rectangular
</option>
<option value="Oval">
Oval
</option>
<option value="Square">
Square
</option>
<option value="Insert">
Insert
</option>
<option value="Round">
Round
</option>
<option value="Open">
Open
</option>
</select>
<br />
<input type="submit" value="Submit">
</form>
</body>
Add the following as the first option to all your dropdowns:
<option value="*">All</option>
Then use the SQL I provided in your duplicate question here:
Make SQL ignore a specific search value if clicked in a dropdown?
Dirk Nachbar gave a good answer as well.
Have you tried limiting your SQL queries by just checking if the values are blank/null (whichever is appropriate for PHP) and just not running the query if nothing is filled in?
e.g. (pseudocode)
if(box1.HasText || box2.HasText || box3.HasText)
{
RunTheSqlForForm1();
}

Categories