value of options loaded from db in dropdown list - php

I used the below code to get the datas or options in my dropdown list from mysql db. also it works fine, but my problem is when i select a particular option from this dropdown list, on submission no value or empty value is saving for that for field. simply i can see the option name, but value is seems like this
value=" ";
actually, value is what i see as option name.
<strong> Select Data </strong>
<select name="data1">
<option value=""> NONE </option>
<?php
//Mysql db connection
$con = mysqli_connect("localhost", "my_user", "my_password", "my_db");
//Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Perform queries
$rs = mysqli_query($con, "SELECT DISTINCT relation FROM relation_names");
if ($rs && mysqli_num_rows($rs)) {
while ($rd = mysqli_fetch_object($rs)) {
echo("<option value='$rd->id'>$rd->relation</option>");
}
}
mysqli_close($con);
?>
</select>

add id in query too
$rs = mysqli_query($con,"SELECT DISTINCT relation,id FROM relation_names");
Then only
$rd->id
will populate correct value
If you want relation as value then do like below:
echo("<option value='$rd->relation'>$rd->relation</option>");

Related

How to show drop down selected values

I am trying to create a dropdown menu which has values from the database and when selected it should show all the information that are connected to the selected value.
In MySQL I have three tables, person, address and resume. I created two drop-down menu's(see code below) which are showing the values from address > address_state and address > address_city. What needs to happen is when I selected a state it will show only the persons living in the selected state. And when selecting city it has to show all the persons living in the same city.
My drop-down menu select state only:(got the same code city)
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = "persons";
// CREATE A CONNECTION WITH THE DATABASE
// CONNECTIE MAKEN MET DATABASE
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//$sql="SELECT name,id FROM student";
$sql="SELECT DISTINCT address_state FROM address ORDER BY address_state asc";
/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
echo "<select name=address value=''>Student Name</option>"; // list box select command
foreach ($conn->query($sql) as $row){//Array or records stored in $row
echo "<option value=>$row[address_state]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>
I know you cannot provide any code because I don't have any code to show which actually "selects" the persons. But I have searched on Google for tutorials and I did not find one which did the thing I needed. So I hope you guys can help me or can provide any tutorials.
Replace this line:
echo "<option value=>$row[address_state]</option>";
With
echo "<option value='".$row['address_state']."'>".$row['address_state']."</option>";
First replace:
echo "<select name=address value=''>Student Name</option>";
To:
echo '<select name="address">';
Next replace this:
echo "<option value=>$row[address_state]</option>";
With:
echo '<option value="'.$row[address_state].'">'.$row[address_state].'</option>';

PHP Dropdown Box from SQL Join

I want to do is have a dropdown box that will display the MealOption and when that is selected somehow find that products meal id?
This is my database layout
<?php
public function get_data()
{
$mysqli= new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die("Couldn't connect".mysqli_connect_error());
$sql="select id,name from table";
$result=$mysqli->query($sql) or die($mysqli->error);
while ($row=$result->fetch_array(MYSQLI_ASSOC))
{
echo "<option value=\"".$row["id"]."\" selected>".$row["name"]."</option>";
}
}
?>
<select name="abc" id="xyz">
<?php get_data(); ?>
</select>
In the function connect the db and then run the query. in the while loop echo your rows like above so you can get your name and id from table. In the end in your html call this function.

Multiple select form element from MySQL recordset in PHP update form

I have table called empicons that has two columns: EmpNo, IconId
I have the EmpNo stored as a session called $EmpNo
I have another table called icons that has 3 columns: Id, Name, URL
Currently I have a recordset that selects the icons table Id and Name and presents it in a Multiple Select box that then updates the empicons table.
Recordset query: SELECT id, name FROM gbl_icons ORDER BY id ASC
Multiple Select Form Element:
<select name="icons[]" size="10" multiple="multiple">
<?php
do {
?>
<option value="<?php echo $row_icons['id']?>"><?php echo $row_icons['name']?></option>
<?php
} while ($row_icons = mysql_fetch_assoc($icons));
$rows = mysql_num_rows($icons);
if($rows > 0) {
mysql_data_seek($icons, 0);
$row_icons = mysql_fetch_assoc($icons);
}
?>
</select>
I'm trying to do a join with empicons and icons table so that the Multiple Select highlights the data elements found in empicons based on the EmpNo variable. I cannot get the join to work correctly and then update the form element to highlight these.
Try this. It uses mysqli_ functions. If you're having a problem with JOINing the two tables, then we need to work on your query.
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT id, name FROM gbl_icons ORDER BY id ASC";
echo '<select name="icons[]" size="10" multiple="multiple">';
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
echo '<option value="' . $row[id] . '">' . $row[name] . '</option>';
}
/* free result set */
mysqli_free_result($result);
}
echo '</select>';
/* close connection */
mysqli_close($link);
?>
Your query should be like this
SELECT i.id, i.name, e.EmpNo FROM gbl_icons i
LEFT JOIN empicons e ON e.IconId = i.id AND e.EmpNo = '$EmpNo'
ORDER BY i.id ASC
So you will get all icons from your 'gbl_icons' table and for each one of them a corresponding record in empicons for the current EmpNo. If there is no corresponding record you will get NULL in the 'EmpNo' column.
And your code should be like that
<select name="icons[]" size="10" multiple="multiple">
<?php
while($row_icons = mysqli_fetch_assoc($icons)) {
?>
<option value="<?=$row_icons['id']?>" <?=!empty($row_icons['EmpNo']) ? ' selected="selected"' : ''?><?=$row_icons['name']?></option>
<?php
}
?>
</select>

Dynamic display of table based on values in combobox using MySQL and PHP

I have two tables in MySQL db namely vendor and menu.
vendor table
vendor_id(Primary) vendor_name
menu table
menu_id(Primary) menu_details cost vendor_id(Foreign)
I need to create a combo box which fetches vendor_name instances from the MySQL db dynamically. On selection of vendor_name from the combo box, menu table(HTML and table should be in editable mode) should be displayed from the MySQL database. I am new to MySQL and PHP. So please help me to deal with this criteria.
Below is the code that I'm working on:
<html><body>
<select name="vendor" id="vendor">
<option value="Choose">Choose</option>
<?php $conn = mysql_connect('localhost','root','');
if (!$con) { die('Could not connect: ' . Mysql_error()); }
mysql_select_db('project',$conn);
$query = "select vendor_id,vendor_name from vendor";
$result = mysql_query($query,$conn);
while($fetch= mysql_fetch_array($query)){
echo "<option>".$row[1]."</option>";
}?>
</select>
</body></html>
If I understood You correctly then this should do the thing that you're after. It displays the combobox from vendor table and if you change the selection, it reloads the page and shows the menu that corresponds to the vendor_id in the menu table.
<html>
<body>
<?php
// connecting to the server
$conn = mysql_connect('localhost','root','') or die('Could not connect: ' . Mysql_error());
// selecting the database
mysql_select_db('project',$conn) or die("db error: ".mysql_error());
// is menu_id set?
if (isset($_GET['menu']) && intval($_GET['menu'])>0) {
// if yes, then query the menu items
$query = "select menu_id, menu_details, cost, vendor_id from menu where vendor_id=".intval($_GET['menu']);
$result = mysql_query($query, $conn) or die("SQL error: ".mysql_error());
while ($row = mysql_fetch_assoc($result)) {
// print the menu items
print_r($row);
}
}
// select the vendor combobox
$query = "select vendor_id,vendor_name from vendor";
$result = mysql_query($query,$conn);
// If the vendor selection has changed then reload the page
?>
<select name="vendor" id="vendor" onchange="document.location='index.php?menu='+this.options[this.selectedIndex].value;">
<option value="Choose">Choose</option>
<?php
// loop through the results
while($fetch= mysql_fetch_assoc($query)){
echo "<option value=\"".$row['vendor_id']."\">".$row['vendor_name']."</option>";
}
?>
</select>
</body>
</html>
the assumption is that the file is named index.php. (look at the select onchange="" filename. You'd need to change it if You have a different filename.

Select box is generated by JavaScript but value is not passed on form submit

I've got a second select box which is created using Javascript when the user selects a value from the first select box. The JS runs a PHP file which queries a MYSQL database for the relevent items depending on the first selection.
The problem I'm having is that the value for the item the user selects in the second box is not passed in the header when the form is submitted.
Does anyone have any ideas how to get the value and pass it in the header?
Thanks.
This is the PHP for the second select box:
<?php
$cxn = mysqli_connect("localhost", "root", "root", "db") or die("Couldn't connect");
$choice = $_GET['choice'];
$query = "SELECT ID, Model FROM models WHERE MakeID ='$choice'";
$result = mysqli_query($cxn, $query) or die("Couldn't execute query");
echo "
<select id='model' name='model'>
<option value='0'>Model (any)</option>";
while ($row = $result->fetch_object()) {
echo "
<option value='$row->ID'>$row->Model</option> \n
";
}
echo "
</select> ";
?>
After creating select field you must append it to your form.
<script>document.getElementById("MyForm").appendChild("select_field_id");</script>

Categories