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>
Related
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 have the following code:
<td>Province</td>
<td>
<select name="prov">
<?php
$sql1 = mysqli_query($connect, "SELECT * FROM Provincetbl order by Prov_Desc desc");
while ($row1 = $sql1->fetch_assoc()){
?>
<option value="prov1"><?php echo $row1['Prov_Desc']; ?> </option>
<?php
}
?>
</select><br><br></td></tr>
<td>Distrect</td>
<td>
<select name="dist">
<?php
$sql2 = mysqli_query($connect, "SELECT * FROM Distrecttbl order by Distrect_Desc desc");
while ($row2 = $sql2->fetch_assoc()){
?>
<option value="dist1"><?php echo $row2['Distrect_Desc']; ?> </option>
<?php
}
?></select></td></tr>
There are two listboxes prov and dist. I have been trying to figure out how to read the value of either listbox (i.e get it in a variable). I have found a post which states that the value of a listbox is read after the form is posted (in the variable $_Post). However, I am dealing with a listbox which is not sending anything to the server, so why is the value of the listbox read after the form is posted?
Second, I do not have a form element in my page to begin with..?
Other posts have stated that I can read the value of listbox in a variable $prov or $dist. However I cannot seem to echo the contents of these variables to the screen.
Thank you for your help.
I think all the options in the prov listbox will have the same value that you've wrote 'prov1' and the second listbox will have dist1 in all the options
What I recommande you to do is the following :
<option value="$row1['Prov_Desc']"><?php echo $row1['Prov_Desc']; ?> </option>
and for the second one use this :
<option value="$row2['Distrect_Desc']"><?php echo $row2['Distrect_Desc']; ?> </option>
I have edit form where I get info from database
<select name="table">
<?php
//fetch all tables from database
$user = $con->query("SELECT * FROM table") or die(mysql_error());
while($row = $table->fetch_object()) { ?>
<option value="<?php echo $row->tablename;?>">
<?php echo $row->tablename; ?>
</option> <?php }?> </select>
<label for="time">Time :</label>
<select name="time">
<option value="twotothree">2PM-3PM</option>
<option value="threetofour">3PM-4PM</option>
<option value="fourtofive">4PM-5PM</option>
<option value="fivetosix">5PM-6PM</option>
</select>
The names of the tables from drop-down menu are different. Everytime I select a table and a time slot and save the data, the selection goes back to the first row of the menu. Eg I select table 3 and 4PM-5PM after saving it goes back to table 1 and 2PM-3PM. I need to be fixed on the last selection as I might use 4PM-5PM for table 4 also. Any idea? Thanks
You can add selected attribute when rendering your select list, depending on $_POST variable, when it's available. For example for you table select element:
<select name="table">
<?php
$user = $con->query("SELECT * FROM table") or die(mysql_error());
while($row = $table->fetch_object()) { ?>
<option value="<?php echo $row->tablename;?>" <?php if (isset($_POST['table']) && $_POST['table'] == $row->tablename) echo 'selected'; ?> >
<?php echo $row->tablename; ?>
</option>
<?php }?>
</select>
In similar way you can do for time select element.
I have three select tags (1)Category, (2)Sub-Category, (3)Product Description and I want to fill it with data from database. But what I want is to when I select eg. Office Supplies from the Category on the Sub Category will only display eg. Ballpen, Ruler, Notebook and when I select eg. Ballpen on the Product Description will only display the description of the Ballpen. Btw I had created some code made from PHP but it only fills the select tags. I hope someone will help me. This is for out Thesis. :)
<html>
<head>
<title>Test Select</title>
</head>
<body>
<?php
$query = "SELECT DISTINCT(`product_cate`) FROM `tbl_product` ";
$result = mysql_query($query);
?>
Category:
<select id="select1">
<option></option>
<?php while($data = mysql_fetch_array($result)){
$displayData = $data['product_cate'];
?>
<option value="<?php echo $displayData;?>"><?php echo $displayData; ?></option>
<?php } ?>
</select>
<?php
$query2 = "SELECT DISTINCT(`product_sub`) FROM `tbl_product`";
$result2 = mysql_query($query2);
?>
Sub Category:
<select id="select2">
<option></option>
<?php while($data2 = mysql_fetch_array($result2)){
$displayData2 = $data2['product_sub'];
?>
<option value="<?php echo $displayData2;?>"><?php echo $displayData2;?></option>
<?php }?>
</select>
<?php
$query3 = "SELECT DISTINCT(`product_desc`) FROM `tbl_product`";
$result3 = mysql_query($query3);
?>
Product Description:
<select id="select3">
<option></option>
<?php while($data3 = mysql_fetch_array($result3)){
$displayData3 = $data3['product_desc'];
?>
<option value="<?php echo $displayData3;?>"><?php echo $displayData3;?></option>
<?php }?>
</select>
</body>
</html>
General guide on the path to take would be to use jquery. Basically what will happen is that you'll load the first select ie Category. When the user selects a category, the change should fire up an ajax call to query the db and get sub directories from the category id posted. the call returns the sub category as json which you'll put in the select box. same plan for the product description
This can be a starting point and follow up from there:
jQuery dropdown dependent
Lots of similar questions here
You can just link options with an other attribute for example :
<select class="cat"><option>Category<option><select>
<select class="sub_cat"><option data-cat="Category">Sub-Category<option><select>
after that use can use javascript to hide unwanted categories for example (with jquery) :
$('.cat').change(function() {
$('.sub_cat option:not([data-cat="Category"])').hide();
});
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;