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
Related
I have to implement a dropdown list to select a customer from db, and then get that customer's orders from db.
My codes below is able to show the list of customers in the drop down box.
$query_customer = "SELECT customer FROM Customer;";
$result_customer = mysqli_query($connect, $query_customer);
<select>
<option value="" disabled selected>--- Select Customer ---</option>
<?php while($row_customer = mysqli_fetch_array($result_customer)) { ?>
<option> <?php echo $row_customer["customer"]; ?> </option>
<?php } ?>
</select>
I intend to get that customer's orders from db with codes below.
$customer = $_GET['customer'];
$query_order = "SELECT order FROM Customer where customer = '$customer';";
$result_order = mysqli_query($connect, $query_order);
My problem is: how can I get hold of the customer selected, and pass to this code $_GET['customer']?
I've spent an hour on this problem. Please could someone help. Thank you.
==========================================================
Based on the comments I revised codes below.
$query_customer = "SELECT customer FROM Customer;";
$result_customer = mysqli_query($connect, $query_customer);
<select name="customer">
<option value="" disabled selected>--- Select Customer ---</option>
<?php while($row_customer = mysqli_fetch_array($result_customer)) { ?>
<option> <?php echo $row_customer["customer"]; ?> </option>
<?php } ?>
</select>
$customer = $_GET['customer']; //Warning: Undefined array key "customer"
$query_order = "SELECT order FROM Customer where customer = '$customer';";
$result_order = mysqli_query($connect, $query_order);
<p><?php echo $result_order['order']; ?></p> //Uncaught Error: Cannot use object of type mysqli_result as array
fyi the Customer table has 10 customers and each customer has exactly one order. The result should be a single value of an order of the selected customer.
I got a warning and an error as stated above. Please advise a proper way to implement the codes.
Thank you so much.
In your <select> opening tag you need name="customer"
Like this: <select name="customer">
Look through the documentation for html forms
You can solve as below
$query_customer = "SELECT customer FROM Customer;";
$result_customer = mysqli_query($connect, $query_customer);
<select name="customer">
<option value="" disabled selected>--- Select Customer ---</option>
<?php while($row_customer = mysqli_fetch_array($result_customer)) { ?>
<option> <?php echo $row_customer["customer"]; ?> </option>
<?php } ?>
</select>
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'");
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.
I'm a bit stuck on a bit of code im doing.
Here's the code i already have
HTML code:
<form method="post" action="index.php" >
<select name="sortby" class="sortby" >
<option value=" ORDER BY id DESC">Date Added (Newest First)</option>
<option value=" ORDER BY id ASC">Date Added (Oldest First)</option>
<option value="ORDER BY clicks DESC">Website Clicks </option>
<input type="submit" value="Submit" />
</select>
</form>
<form method="post" action="index.php">
<select name="country" class="sortby">
<option value="">Country...</option>
<option value="AF">Afghanistan</option>
<option value="AL">Albania</option>
<option value="DZ">Algeria</option>
<option value="AS">American Samoa</option>
<option value="AD">Andorra</option>
.... and so on
</select>
<input type="submit" value="Submit" />
PHP code:
$sortby = $_POST['sortby'];
$sortby = $mysqli->real_escape_string($sortby);
$country = $_POST ['country'];
$country = $mysqli->real_escape_string($country);
$results = $mysqli->query("SELECT id, link, image, title, description, country, clicks FROM isc_links WHERE approved=1 $sortby");
while($row = $results->fetch_assoc()) {
print '<div id="linkmainwrapper"><div id="linkwrapper"><div class="linkimageborder"><div class="linkimage">'.$row["image"].'</div></div>';
print '<div class="linktitle">'.$row["title"].'</div>';
print '<div class="link">('.$row["link"].')</div>';
print '<div class="countryflag"><img src="http://www.hatblocksdirect.co.uk/lib/flags/'.$row["country"].'.gif"></div>';
print '<div class="linkdescription">'.$row["description"].'</div>';
print '<div class="clickcount">Website Clicks ('.$row["clicks"].')</div></div></div>';
what i would like to do is sort colums. the first sort box works, i can sort by website clicks, id etc using $sortby.
Ive passed the $country using a select drop down but i dont know how to implement this into my select query. I need it to display all entries if a country hasn't been selected. once a country is selected only display that country.
Any help very much appreciated
Put the additional condition of the WHERE clause in a variable depending on whether the parameter is supplied.
if (!empty($_POST['country'])) {
$country = "AND country = '" . $mysqli->real_escape_string($country) . "'";
} else {
$country = "";
}
$results = $mysqli->query("SELECT id, link, image, title, description, country, clicks FROM isc_links WHERE approved=1 $country $sortby");
Use an if condition like
if(isset($_POST ['country']))
{ // if country is set
$country = $mysqli->real_escape_string($_POST ['country']);
$sql="SELECT id, link, image, title, description, country, clicks FROM isc_links WHERE approved=1 and country='$country' $sortby";
}
else
{ // if country is not se
$sql="SELECT id, link, image, title, description, country, clicks FROM isc_links WHERE approved=1 $sortby";
}
$results = $mysqli->query($sql); // execute the query
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;