php mysql multiply 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 Price. For example if oil price per litre is £0.50 I need to multiply this value by value selected within form dropdown.
Can anyone please guide me on what I am supposed to do??
Ok heres an update code preview.
<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';
$pdo = '';
$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;
?>
Anyone know where I am going wrong??
Thanks

<?php
//Connect to database here. In this example, I'll assume you connected using PDO
//Although the same logic applies on any engine.
$stmt = $pdo->prepare("SELECT `price` from `fuel_price` WHERE `type` = :type"); //Prepare a query
$stmt->bindValue(':type', $_POST['type']); //Assuming the first <select> is named type
if (!$stmt->execute()) { die($stmt->errorInfo[2]); } //Display an error and terminate script if query failed.
$row = $stmt->fetch(); //Assuming you have only one row, fetch should only be called once.
$price = $row['price'];
echo $_POST['qtylitres'] * $price; //Multiply quantity with price and print result.
?>
Note that I have not tested it, but it should work. Your markup is incomplete, it lacks the opening for the first <select>. Read the comments and you should be good to go.

assuming that you have a column 'price' in your table, and that the only result contains the correct price:
include 'mysql_connect.php';
if (isset($_POST['submit'])) {
// edit: added fueltype in the where clause
$fueltype = mysql_real_escape_string($_POST['fueltype']);
$q = "SELECT * FROM fuel_price WHERE id = '1' AND fueltype='$fueltype'";
$result = mysql_query($q);
$row= mysql_fetch_array($result);
$price = $row['price'] * $_POST['qtylitres'];
echo $price;

Related

why is my PHP query displaying blank results

my query is echoing the right amount of results in my selection box but theyre all blank? super confused any help appreciated
<?php
session_start();
$cupsession = $_SESSION['c_cname'];
$teamSet = $mysqli->query("SELECT cup_name FROM teams WHERE cup_name='$cupsession'");
?>
<select name="team-1" required>
<option value='Holder' disabled selected>Team 1</option> <!--Placeholder for Select-->
<?php
while($rows = $teamSet->fetch_assoc()){ //Fills select with cup entries in db
$teamName = $rows['team_name'];
echo " <option value='$teamName'>$teamName</option>";
}
?>
</select>
I presume you're trying to get the team_name from your database not cup_name so it would be:
$teamSet = $mysqli->query("SELECT team_name FROM teams WHERE cup_name='$cupsession'");

PHP search with few parameters

I have an query that I am trying to do search with multiple parameters to sort my data response from sql.
The query code :
$sql = "SELECT * FROM `apartments` WHERE `building_num`='{$i}' ORDER by `apartment_num` ASC";
The search form :
<form method="GET">
<input type="hidden" name="page" value="price">
<select class="styled-select2" name="type">
<option value="0">Apartment type</option>
<option value="דופלקס">Duplex</option>
<option value="דירה">Apartments</option>
<option value="דירת גן">דירת גן</option>
</select>
<select class="styled-select2" name="rooms">
<option value="0">rooms</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="5">5</option>
</select>
<select class="styled-select2" name="price">
<option value="0">Price</option>
<option value="700000-800000">700,000-800,000 ₪ </option>
<option value="800000-900000">800,000-900,000 ₪</option>
<option value="900000-1000000">900,000-1,000,000 ₪</option>
<option value="1000000-1100000">1,000,000-1,100,000 ₪</option>
<option value="1100000-1200000">1,100,000-1,200,000 ₪</option>
<option value="1200000-1300000">1,200,000-1,300,000 ₪</option>
<option value="1300000-1400000">1,300,000-1,400,000 ₪</option>
<option value="1400000">1,400,000 up</option>
</select>
<input type="hidden" name="action" value="search">
<button type="submit">Search</button>
</form>
There are three parameters but they are not required, could the customer only use 'apartment type' AND 'Price' OR only one parameter, I tried for few hours to write query with no success, Any one can help ?
// Vars on table apartments
// Rooms - > Rooms
// Price -> price
// Apartment type -> type
1st of all, there's no direct link between and query (or queries) and tge options you're giving to the customer...
If you want to then you can allow the customer to fill some of the fields and if you don't then no... it's a matter of decision, from which derives the development
There's missing example code, so I can obly answer regarding the query and add some remarks
Since you've given specific select options, then you should use it for specific searches
I would also use some validations on the $_POST information returned
Regarding the query, I need to assume that all of the options are saved in the apartments table, under that, then the query should be something like this :
$sql = "SELECT * FROM `apartments` WHERE
rooms = $rooms
OR `type` = $type
OR (price BETWEEN *$range_min* AND *$range_max*)
ORDER by `apartment_num` ASC";
If you want all parameters combined you should use AND instead of OR
and, again, you can decide you want both and use different queries for each scenario the user givea
In this case you have to build dynamic query as below :-
$sql = "SELECT * FROM `apartments` WHERE ";
$condition = "";
$condition .= ($rooms=='')?"":" or 'rooms'= $rooms ";
$condition .= ($type=='')?"":" or 'type'= $type ";
$condition .= ($price=='')?"":" or prince bewteen 'range_min' AND 'range_max'";
$sql .= $condition;
$sql .= " ORDER by `apartment_num` ASC";
In this way you can make all three filters free to select or not.

Php price asc function is not working

I'm trying to do price ascending or descending filter in php, but it is not working. Could some one help to fix it? Thanks
<select name='sort'>
<option value='ASC'> Price Low to High </option>
<option value='DESC'> Price High to Low </option>
</select>
<?php
$query = 'SELECT prd_price FROM products ORDER BY '.$_REQUEST['sort'];
$run_query = mysqli_query($con,$query);
$row = mysqli_fetch_array($run_query);
?>
You can't do it like this. Cause your select is not processed.
When you read the php docs about $_REQUEST you see that it only processes the values of $_POST, $_GET and $_COOKIE.
If you would like to make this working, you could do something like this:
<form method="post">
<select name="sort">
<option value="asc">Price Low to High</option>
<option value="desc">Prive High to Low</option>
</select>
<input type="submit" value="Sort">
</form>
<?php
// check if the server recieved a post:
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (isset($_POST['sort'])) {
if ($_POST['sort'] == "asc") {
$query = "SELECT prd_price FROM products ORDER BY prd_price ASC";
} else {
$query = "SELECT prd_price FROM products ORDER BY prd_price DESC";
}
}
// execute query here.
}
?>
And try to avoid using $_REQUEST

Select all users with specific MySql request PHP

I'm coding a feature in PHP, it's a search engine.
<form action="result" method="POST">
<select name="instrument" id="instrument">
<option value="all">All instrument</option>
<option value="1">Guitar</option>
<option value="2">Bass</option>
<option value="3">Battery</option>
<option value="4">Singer</option>
</select>
<select name="theme" id="theme">
<option value="all">All themes</option>
<option value="1">Metal</option>
<option value="2">Jazz</option>
<option value="3">Rock</option>
<option value="4">Blues</option>
</select>
<input type="submit" value="search" />
</form>
I already code this search engine like this and it works
<?php
if(isset($_POST)) {
$theme = $_POST['theme'];
$instrument = $_POST['instrument'];
$req_search = $bdd->query("SELECT * FROM user,theme,instrument WHERE theme.theme_id = user.user_theme AND instrument.instrument_id = user.user_instrument AND theme_id =".$theme." AND instrument_id =".$instrument);
if($req_search->rowCount()>0) {
while($search = $req_search->fetch()) {
?>
<p><?php echo $search['user_username']; ?></p>
<?php
}
$req_search->closeCursor();
} else {
echo "Users not found";
}
}
?>
You can find users who is matching with those options (without All instrument and All themes.
Everything is stored into a MySql database.
However when I click on All themes and All instrument. I want to update my select request. Like if I select All instrument and All themes I want to display every users. Or if I select All instrument and Metal, I want to display all users listening to metal and playing whatever instrument.
Thanks for your help !
You can construct your query to be conditional depending on whether $theme and $instrument have values:
$query = "SELECT * FROM user,theme,instrument
WHERE theme.theme_id = user.user_theme
AND instrument.instrument_id = user.user_instrument".
($theme!="" ? " AND theme_id='$theme'" : "").
($instrument!="" ? " AND instrument_id='$instrument'" : "");
$req_search = $bdd->query($query);
That said, you should look into PDO Prepared Statements and Placeholders, because all this would take is someone to fiddle around with the DOM to perform some MySQL Injection.

how to run an sql statement base on the value of the select tag in PHP

Hi I want to filter my searches in the sql based on the requirement the user wants so i used a select tag and depending on the value of the select tag a certain sql statement will be used but its not working as expected can you help me looked what went wrong here:
$strFilterByStatus = $_POST['byStatus'];
<select name="byStatus" id="byStatus" onChange="frmAdminWorklist.submit()">
<option value="all">All</option>
<option value="recovered">Recovered</option>
<option value="unrecovered">Unrecovered</option>
</select>
<?php
if($strFilterByStatus=='all'){
$strSQL = "SELECT * FROM tblcase";
$SQL=mysql_query($strSQL);
}
?>
appreciate the help :)
Suppose this is all in theTest.php
<?php
$strFilterByStatus = $_POST['byStatus'];
?>
<form id='testform" action="theTest.php" method="post">
<select name="byStatus" id="byStatus" onChange="frmAdminWorklist.submit()">
<option value="all">All</option>
<option value="recovered">Recovered</option>
<option value="unrecovered">Unrecovered</option>
</select>
<input type='submit' name='' value='Go test it'>
</form>
<?php
if ( $strFilterByStatus == 'all' )
{
$strSQL = "SELECT * FROM tblcase";
$SQL = mysql_query($strSQL);
}
?>

Categories