why is my PHP query displaying blank results - php

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'");

Related

PHP - How to get hold of selected choice and pass to db

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>

How to fetch value in multi select dropdown in php?

code:
<select data-placeholder="Click and Select Menu For Assign..." multiple class="chosen-select form-control" name="assign[]">
<?php
$main_menu = explode(",",$menu);
foreach($main_menu as $fetch)
{
?>
<option value="<?php echo $fetch; ?>"<?=($fetch==$fetch)? 'selected="selected"':''?>></option>
<?php
}
?>
<option value="null" style="color:red;">Select Menu For Assign</option>
<?php
$sql = "select menu from assign";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
echo "<option value='".$row['menu']."'>".$row['menu']."</option>";
}
?>
</select>
In this code I have create multi select dropdown. Now I want to fetch value inside the multi select dropdown like aaa bbb ccc but now its look like as we see in simple dropdown. So, How can I fix this issue ?Please help me.
Thank You

How do I fix the selected item in a drop down box

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.

Populating Select list with PHP and SQL

I am calling from my SQL server a list for the options and outputting into an array.. see query:
SQL
$location = "SELECT location_id, location_name, location_postcode, location_buildingname
FROM dbo.system_locations";
$stmt = sqlsrv_query( $conn, $location );
PHP
<select class="form-control" name="location">
<?php while ($row = sqlsrv_fetch_array($stmt)){ ?>
<option>
<?php echo $row['location_name']?>
</option>
<?php } ?>
</select>
This works great, however I want the option which is selected within the database to be the selected option..
There is one factor, the select list can be added to by visiting settings section and adding new location otherwise I would use the ternary operator method.
try this
<select class="form-control" name="location">
<?php while ($row = sqlsrv_fetch_array($stmt)){ ?>
<option <?php ($row['selected'] == 1){echo 'selected';}?>>
<?php echo $row['location_name']?>
</option>
<?php }?>
</select>

php mysql multiply 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 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;

Categories