Drop down menu with data from MySQL datebase as options - php

i try to get data from mysql db into html dropdown , i execute the query in PHPmyadmin and its work fine , the result is one record,and all the website is connected with theses details of MYSQL my code is :
<?php
mysql_connect("localhost", "root", "1212") or die("Connection Failed");
mysql_select_db("test")or die("Connection Failed");
$query = "SELECT department_name FROM department";
$result = mysql_query($query);
?>
<label for="department" > Department Name </label>
<select name="departments" >
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="<?php echo $line['field'];?>"> <?php echo $line['field'];?> </option>
<?php
}
?>
</select>
the output is drop-down with one empty record , any one can help me in that ?

First required statement: mysql_ is deprecated. mysqli_ should be used instead.
Second ...
Did you try $line['department_name'] instead of $line['field'] ?

Well first of all you should print the results of the query to ensure the array structure is what you think it is.
This would have shown you that there is no column in the result set named field as you seem to believe due to this line of code:
<option value="<?php echo $line['field'];?>"> <?php echo $line['field'];?> </option>
As to why you only get one option, my first bit of advice will probably shed some light on said issue as well.

Related

UPDATING information into MySQL Database but department remains the same for each employee

I am trying to update my DB for the employees table. When I run and test, every user is coming up as working in Accounting even if they work in another department. Is there something I am missing? I am not receiving any errors either. Any help is greatly appreciated.
PHP/HTML
<?php
//ERROR CHECKING CODE
mysqli_report(MYSQLI_REPORT_ALL ^ MYSQLI_REPORT_STRICT);
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once("dbconnect.php");
$id = (isset($_REQUEST['id']) ? $_REQUEST['id'] : '');
$sql = "SELECT * FROM employees WHERE empid= '" . $id . "';";
$result = mysqli_query($connect, $sql) or die(mysql_error);
$row = mysqli_fetch_array($result, MYSQL_ASSOC) or die (mysql_error());
?>
PHP/HTML
<p>Department</br>
<select name="department">
<option <?php if($row['department']==1) {print('selected');}?>value="1">Accounting</option>
<option <?php if($row['department']==2) {print('selected');} ?>value='2'>Legal</option>
<option <?php if($row['department']==3) {print('selected');} ?>value='3'>Information Technology</option>
<option <?php if($row['department']==4) {print('selected');} ?>value='4'>Human Resources</option>
</select>
Without seeing the code in action it's hard to say, but at first glance there's this problem: In each option tag you're not leaving any space between the closing PHP tag (?>) and the value property, so when if $row['department'] matches the correspondent value, the PHP statement will print "selected" but with no space, the HTML will look like this (say $row['department'] equals 2):
<option selectedvalue='2'>Legal</option>
which obviously won't select that option. Try adding a space after each closing PHP tag, or print "seleted ", with a space in the end.

How create a drop down list box using data from MySQL table

I need some help in populating a drop down down list from a mysql table. I'm new to php and I am having a hard time. Here's my code and I know it's wrong, I just don't know where.
My current output is just a drop down box with nothing inside it.
My expected output is that it would show the driver's name from the mysql table.
<?php
$Hehe = $mydb->getALL('SELECT drivername FROM driver;'); //select from all users
?>
<select name=mydriver value=''>Driver Name</option> // list box select command
<?php
foreach($Hehe as $hehe){//Array or records stored in $row
?>
<option name = "mydriver"><?phpecho $Hehe['drivername']?></option>
</select>
<?php
}
?>
the getALL function:
function getAll($query) {
$result = $this->conn->prepare($query);
$ret = $result->execute();
if (!$ret) {
echo 'PDO::errorInfo():';
echo '<br />';
echo 'error SQL: '.$query;
die();
}
$result->setFetchMode(PDO::FETCH_ASSOC);
$reponse = $result->fetchAll();
return $reponse;
}
You're using wrong array to get the drivername. It should be $hehe['drivername'], not $Hehe['drivername']. Like I said, use some meaningful variable names in your code, it would be easy for you track down the error. Also </select> should be outside of foreach loop.
<?php
$Hehe = $mydb->getALL('SELECT drivername FROM driver;'); //select from all users
?>
<select name="mydriver">Driver Name</option> // list box select command
<?php
foreach($Hehe as $hehe){//Array or records stored in $row
?>
<option value="<?php echo $hehe['drivername']; ?>"><?php echo $hehe['drivername']; ?></option>
<?php
}
?>
</select>
Sidenote: Always turn on error reporting, add these two statements ini_set('display_errors', 1); error_reporting(E_ALL);at the very top of your PHP scripts to debug any syntax related issues.

Drop down list with data from database

First of all I would like to let you know that I tried every other solution that I found here regarding to my topic.. But nothing worked for me!
As the title says I want to create a drop down list with data from a database!
The connection with the database is right so I don't show you the code! I have tried many ways, one is below.
The only thing that I am getting is a blank list.Thanks!
<select name="Anaktisi">
<?php
$query2="select name from books WHERE sub_ID=1";
$result2 = mysqli_query($con,$query2);
while ($row2 = mysqli_fetch_array($result2)) { ?>
<option value="<?php echo $row2['name'];?>"> </option>
<?php } ?>
</select>

html options displaying duplicate json output

So, I'm facing a strange situation here:
I have a form, that form has several categories wich are loaded via Json.
The code for the part of the form I'm refering is the following:
<div>
<select>
<?php include_once("php_loader/getAllCategoriesOptions.php"); ?>
</select>
</div>
So far so good, then I have that php file:
<?php
$con = $con = mysqli_connect("localhost","XXX","XXX","XXX");
mysqli_query($con,"SET NAMES UTF8"); /
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
$result = mysqli_query($con,"SELECT * FROM categories WHERE IdCategory > 1") or die;
while($row = mysqli_fetch_assoc($result))
{
$output[]=$row;
}
print json_encode($output);
foreach($output as $json){
echo'<option value="'.$json['IdCategory'].'">'.$json['Category'];
}
mysqli_close($con);
?>
Wich returns me the following output:
[{"IdCategory":"2","Category":"Hobbies"},{"IdCategory":"3","Category":"Tattoos"},{"IdCategory":"4","Category":"Trips"},{"IdCategory":"5","Category":"Esoteric"},{"IdCategory":"6","Category":"Books"}]
Hobbies
Tattos
Trips
Esoteric
Books.
So far so good. But when I include this on my html page it gives-me the following output:
<div>
<select>
[{"IdCategory":"0","Category":"All Categories"},{"IdCategory":"1","Category":"Technology"},{"IdCategory":"2","Category":"Hobbies"},{"IdCategory":"3","Category":"Tattoos"},{"IdCategory":"4","Category":"Trips"},{"IdCategory":"5","Category":"Esoteric"},{"IdCategory":"6","Category":"Books"},{"IdCategory":"2","Category":"Hobbies"},{"IdCategory":"3","Category":"Tattoos"},{"IdCategory":"4","Category":"Trips"},{"IdCategory":"5","Category":"Esoteric"},{"IdCategory":"6","Category":"Books"}]
<option value="0">All Categories</option>
<option value="1">Technology</option>
<option value="2">Hobbies</option>
<option value="3">Tattoos</option>
<option value="4">Trips</option>
<option value="5">Esoteric</option>
<option value="6">Books</option>
<option value="2">Hobbies</option>
<option value="3">Tattoos</option>
<option value="4">Trips</option>
<option value="5">Esoteric</option>
<option value="6">Books </option>
</select>
</div>
( I took this value from mozzila firebug - in the website itself appears only the values twice )
What can possibly be happening here?? I'm not being able to understand where it's wrong.
It might be important to note that I Have a simmilar php file running before and that php files loads every entry of that table - I suspect it might have something to do with this but I'm not beeing able to get the solution ( I'm querying the same table twice in the same html page to have different outputs, one gives me several , one for each category, other gives me several as mentoned above).
Thanks in advance.
PS: As asked: here is the other php file I used before in the same code:
<?php
$con = $con = mysqli_connect("localhost","ZZZ","ZZZ","sametable");
mysqli_query($con,"SET NAMES UTF8"); //é preciso meter isto em utf-8 senão não manda nada
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
$result = mysqli_query($con,"SELECT * FROM categories") or die;
while($row = mysqli_fetch_assoc($result))
{
$output[]=$row;
}
foreach($output as $json){
echo
'<li>
<a class="item">'.$json['Category'].'</a>
</li>';
}
mysqli_close($con);
?>
So, It seems that I found the error ( and it's a really stupid one ).
I thaught that there was no problem running 2 php files at the same code ( and it hasn't ) but I didn't know that I couldn't name the two files variables the sameway, so, $output from the first php was appearing in the second php file giving me $output$output as the result.
Changing the name of the second file to $output2 solved this issue.

having trouble getting selected value from php dynamic selection option

I want to show options from my database for users to check, but having trouble getting user's choice.
So, I write two php files,
the first one doing things like: getting data from database, displaying in select option, then submit value by post to and the second php file.
And the second php file just display the recieved value.
Here's the first php file:
<html>
<body>
<form method="post" action="second.php">
<Select name=”select_value”>
<?
//connect to server
$con = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die("Error " . mysqli_error($con));
$query = "SELECT * FROM MYTABLE" or die("Error in the consult.." . mysqli_error($con));
$result = $con->query($query);
//display result in select option
while ($row = mysqli_fetch_array($result)) {
echo "<Option value=".$row['ENTRY_ID']."> ".$row['ENTRY_NAME']."</Option><br>";
}
mysqli_close($con);
?>
</Select>
</form>
</body>
</html>
And the second php file:
<?
$option = isset($_POST['select_value']) ? $_POST['select_value'] : false;
if($option) {
echo $_POST['select_value'];
} else {
echo "not getting value of select option";
exit;
}
?>
If this works fine, I should see the selected value by the second php file, but I keep recieving my echo "not getting value of select option".
There must be something wrong between select option and my recieving file.
Can someone help?
try this double quotes
<Select name="select_value">
instead of <Select name=”select_value”>

Categories