At this time the dropdown menu unable to call data from database Postgres.
<div class="form-group">
<?php
<label>Payee</label>
<form action="pg-t-payment-update.php" method="post">
<select class="form-control select2" style="width: 100%;">
<?php
$db = pg_connect("host=10.0.32.x port=5432 dbname=postgres user=postgres password=123");
$sql ="select distinct tenant_name FROM payment_ref_tenancy order by tenant_name asc";
$result = pg_query($db, $sql);
$rows = pg_num_rows($result);
while ($row = pg_fetch_assoc($result)) {
echo '<option value="'.htmlspecialchars($row['tenant_name']).'"></option>';
}
pg_close($db);
?>
</select>
</div>
I think the issue is here:
echo '<option value="'.htmlspecialchars($row['tenant_name']).'"></option>';
change it to:
echo '<option value="'.htmlspecialchars($row['tenant_name']).'">'.htmlspecialchars($row['tenant_name']).'</option>';
The correct syntax of option is:
<option value="1">One</option>
here value is the text we get when use it in code, where One is the text which is shown to user.
Related
I have my search form, where the cities are fetched from the database, but how can I make the search work when I select some option? I need to get "printed" everything about all houses that are in that particular selected city
HTML Form:
<form action="./server/Search.php" method="post">
<div class="col-auto mt-5">
<select name="city" class="form-select" aria-label="Default select example ">
<option selected disabled>City</option>
<?php
$query = $conn->query("SELECT * FROM `houses`") or die(mysqli_error());
while($fetch = $query->fetch_array()){
?>
<option value=""><?php echo $fetch['city']?></option>
<?php
}
?>
</select>
</div>
<div class="col-auto mt-5">
<button type="search" name="search" class="btn btn-primary"><i class='bx bx-search-alt-2'></i></button>
</div>
</form>
Search.php
<?php
$con= new mysqli("localhost","root","","KBHestate");
$name = $_post['search'];
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM houses
WHERE city LIKE '%{$city}%'");
while ($row = mysqli_fetch_array($result))
{
echo $row['city'];
echo "<br>";
}
mysqli_close($con);
?>
I don't know how to connect selected option with the query. Is there any way how to handle this the best way possible?
First change in the html form, to avoid selecting columns and rows you don't need:
<?php
$query = $link->query("SELECT distinct city FROM `houses`");
while($fetch = $query->fetch_array(MYSQLI_ASSOC)){
echo "<option value='{$fetch['city']}'>{$fetch['city']}</option>\n";
}
?>
Then search.php with bind variables.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = new mysqli("localhost","root","","KBHestate");
$name = $_POST['city'];
$query = $link->prepare("SELECT id FROM houses where city = ?");
$query->bind_param("s", $name);
$query->execute();
$query->bind_result($id);
while($query->fetch()){
echo "{$id}</br>\n";
}
?>
It seems that a few things are missing here:
The value is missing in the HTML Form so nothing gets posted
You should not use the cities name (because there are a lot of cities with problematic characters) but a numerical id for the value.
This also means that you need a seperate lookup table with cities and the city id and join this with the houses table.
">
Then your second script search.php does not use the posted value
$city_id = $_POST['city_id'];
[...]
now you can use $city_id in the query:
$result = mysqli_query($con, "SELECT * FROM houses
WHERE city_id ='{$city_id}'");
BUT in fact this is not the proper way to do it - use prepared statments instead.
I am creating one IMS System. On order page I put one drop down list when I select any option from drop down it working perfectly but when it redirect to update page drop down filed data is coming blank. If anyone know solution than please help. Below is my code of create.php
<select class="form-control select_group party" data-row-id="row_1"
id="party_1" name="name" style="width:100%;" required>
<option value=""></option>
<?php
$dbc = mysqli_connect('localhost', 'root', '', 'stock')
or die('Error connecting to MySQL server.');
$query = "SELECT * FROM partys";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row['name'] ?>">
<?php echo $row['name'] ?
</option>
<?php } ?>
</select>
And for update.php
<select class="form-control select_group party" data-row-id="row_1" id="party_1"
name="name" style="width:100%;" required>
<option value=""></option>
<?php
$dbc = mysqli_connect('localhost', 'root', '', 'stock')
or die('Error connecting to MySQL server.');
$query = "SELECT * FROM partys";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
?>
<option <?php if (!empty($name) && $name == $row['name']) echo 'selected = "selected"'; ?> value="<?php echo $row['name'];?>"><?php echo $row['party_id'];?>
</option>
<?php } ?>
</select>
I search for answers here but haven't found a solution.
I have also added picture of the error.
I want the data to go to the first drop-down list ( above the error)
I think the method I try to perform is also create drop-down list, am I correct?
<form name="message" action="" method="post" onsubmit="" accept-charset="utf-8">
<div class="form-group">
<label id="senderName">שם השולח:</label>
</div>
<div class="form-group">
<label for="to_user">מען:</label>
<select name="to_user" class="form-control">
<option value="pick">בחר מהרשימה</option>
<?php
$sql = \mysqli_query("SELECT name From users");
$row = mysqli_num_rows($sql);
echo "<select name='to_user'>";
while ($row = mysqli_fetch_array($sql)){
echo "<option value='". $row['name'] ."'>" .$row['name'] ."</option>" ;
}
echo "</select>" ;
?>
</select>
</div>
picture of the error
In MySQLi, the first parameter of a query needs to be the database connection. Also, there's no need to add a \ before the statement.
$sql = \mysqli_query("SELECT name From users"); should be $sql = mysqli_query($con, "SELECT name From users");
Note: replace $con with your database connection variable!
As you mentioned that you wanted the result from the database to go inside the select form, simply adjust your code to look like this:
<select name="to_user" class="form-control">
<option value="pick">בחר מהרשימה</option>
<?php
$sql = mysqli_query($con, "SELECT name From users");
$row = mysqli_num_rows($sql);
while ($row = mysqli_fetch_array($sql)){
echo "<option value='". $row['name'] ."'>" .$row['name'] ."</option>" ;
}
?>
</select>
<div class="row form-group">
<div class="col col-md-3">
<label for="email-input" class=" form-control-label"> Vehicle</label>
</div>
<div class="col-12 col-md-9">
<select name="car_id" id="car_id" class="form-control-label" >
<?php
$list = mysqli_query($conn,"SELECT * FROM `vehicle_registration` where `status`='0' ");
while ($row_ah = mysqli_fetch_assoc($list)) {
?>
<option value="<?php echo $row_ah['id']; ?>"><?php echo $row_ah['car_no']; ?></option>
<?php } ?>
</select>
</div>
</div>
<label><b>Select Steam: </b></label>
<select id="study">
<option value="" selected="selected" disabled="">---Selected---</option>
<?php
$query = "SELECT study FROM details";
$query_run = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($query_run)) {
echo "<option value='".$row['study']."'>".$row['study']."</option>";
}
?>
</select>
Is there anyway of retrieving whole tables based on a drop down list? Like when i select one and press submit it will bring that particular table? 'gameResults' is the database which features tables. My code is as follows:
if(isset($_POST["submit"])) {
$tablesnames = $_POSt["tablenames"]; // name of selection list
if($_POST["tablenames"] == '1') { // if option 1 is selected
// display table
} }
<?php
$conn = mysql_connect("localhost", "xxxxxx", "xxxxxx");
mysql_select_db("gameResults", $conn)
or die ('Database not found ' . mysql_error() );
"SELECT TABLE FROM information_schema.tables WHERE table_schema = 'gameResults'";
$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());
?>
<html>
<body>
<form name ="tables" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p>Choose form to display :
<select name="tablesnames" id="tablesnames">
<option value="nothing"> </option>
<option value="fixtures"> Fixtures </option>
<option value="results"> Results </option>
<option value="teams"> Teams </option>
<option value="seasons"> Seasons </option>
<option value="administrators"> Administrators </option>
<option value="users"> Users </option>
</select></p>
<input type="submit" name="submit" > <input type="reset"
<html>
</body>
First off, there is no column TABLE. What you are looking for is TABLE_NAME. Consider this example: (And please stop using mysql_ functions, use PDO or mysqli_ instead)
<?php
$tables = array();
// host, db user, db password, datababse
$con = mysqli_connect("localhost","test","test","test");
$query = mysqli_query($con, "SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `TABLE_SCHEMA` = 'test'");
$tables = mysqli_fetch_all($query, MYSQLI_ASSOC);
?>
<!-- loop thru the values -->
<select name="whatever">
<?php foreach($tables as $value): ?>
<option value="<?php echo $value['TABLE_NAME']; ?>"><?php echo $value['TABLE_NAME']; ?></option>
<?php endforeach; ?>
</select>
**<html>
<body>
<form name="call">
<select name="category">
<option>Select a category</option>
<?php
mysql_connect("localhost","root","");
mysql_select_db("shenvel");
$category = "SELECT productname FROM newproduct";
//retrieving product name alone from newproduct
$query_result = mysql_query($category);
while($result = mysql_fetch_assoc($query_result))
{
?>
<option value = "<?php echo $result['productname']?>"><?php echo $result['productname']?></option>
//the above code displays the combo box with one empty space as output.
**
change fetch records using mysql_fetch_array()
while($result = mysql_fetch_array($query_result))
{
*********
}
And try it..
Try this
<select name="category">
<option>Select a category</option>
<?php
mysql_connect("localhost","root","");
mysql_select_db("shenvel");
$category = "SELECT productname FROM newproduct";
$query_result = mysql_query($category);
while($result = mysql_fetch_array($query_result))
{
?>
<option value = "<?php echo $result['productname']; ?>"><?php echo $result['productname']; ?></option>
<?php
}
?>
</select>
try this:
<html>
<body>
<form name="call">
<select name="category">
<option>Select a category</option>
<?php
mysql_connect("localhost","root","");
mysql_select_db("shenvel");
$category = "SELECT productname FROM newproduct";
//retrieving product name alone from newproduct
$query_result = mysql_query($category);
while($result = mysql_fetch_array($query_result))
{
?>
<option value = "<?php echo $result['productname']?>"><?php echo $result['productname']?></option>
Your while loop is being fully executed in the first <?php ... ?> snippet. The syntax you used for your while loop will not execute across multiple php code snippets.
Try echoing out your option HTML code and thus having only one php code snippet.
<html>
<body>
<form name="call">
<select name="category">
<option>Select a category</option>
<?php
mysql_connect("localhost","root","");
mysql_select_db("shenvel");
$category = "SELECT productname FROM newproduct";
//retrieving product name alone from newproduct
$query_result = mysql_query($category);
while ($result = mysql_fetch_assoc($query_result)) {
echo '<option value = "', $result['productname'], '">', $result['productname'], '</option>';
}
?>