i have been try for PHP dropdown list with data from a MySQL database.
I have attached my code below, am not getting the dropdownlist .Can anyone help
<?php
$conn = mysql_connect('localhost','root','');
mysql_select_db('qsearch',$conn);
$query = "select Category from information";
$result = mysqli_query($query);
echo '<select id="info" name="info">';
echo '<option value="">-select-</option>';
while ($row = mysqli_fetch_assoc($result)){
?>
<option value="<?php echo $row['Category']; ?>"><?php echo $row['Category']; ?> </option>
<?php
}
echo "</select>";
?>
Because half of your functions are from mysql and other from mysqli
$conn = mysql_connect('localhost','root','');
mysql_select_db('qsearch',$conn);
$query = "select Category from information";
$result = mysqli_query($query);
while ($row = mysqli_fetch_assoc($result)){
You need to closely read the documentation first
http://www.php.net/mysqli & http://www.php.net/mysqli_connect
Related
I need to output questions from my database so users can answer them as part of the registration process. Each question has 2 choices related to it, and each question is now being outputted to the site twice each with one choice below it. I need each question to appear once with their related choices appearing below it. Please see my code below.
<?php
$sql = "SELECT Question_ID, Question FROM Questions;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck > 0):
while($row = mysqli_fetch_assoc($result)):
$questionid = (int)$row['Question_ID'];
?>
<label><?php echo $row['Question'];?></label><input type="hidden" value="<?php echo $row['Question_ID'];?>"/>
<?php
$query = "SELECT Choice_ID, Choice FROM Choices WHERE Question_ID = '$questionid';";
$results = mysqli_query($conn, $query);
$resultsCheck = mysqli_num_rows($results);
if($resultsCheck > 0):
while($rows = mysqli_fetch_assoc($results)):
?>
<br><select id="choice">
<option value="<?php echo $rows['Choice_ID']; ?>"><?php echo $rows['Choice']; ?></option>
</select><br/>
<?php
endwhile;
endif;
endwhile;
endif;
?>
You do not have any <form> in your question and I suppose you know what you are doing and you know how to handle the posted data since your <input> and <select> elements does not have any name properties.
As Strawberry suggested, it's recommended to use prepared statements.
I have changed your code just a little to meet your needs, In your code, you have to take the <select> tag out of the while loop and just have the <option> tags in the loop.
So the code will be like this:
<?php
$sql = "SELECT Question_ID, Question FROM Questions;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck > 0):
while($row = mysqli_fetch_assoc($result)):
$questionid = (int)$row['Question_ID'];
echo '<label>'.$row['Question'].'</label><input type="hidden" value="'.$row['Question_ID'].'">';
$query = "SELECT Choice_ID, Choice FROM Choices WHERE Question_ID = '$questionid';";
$results = mysqli_query($conn, $query);
$resultsCheck = mysqli_num_rows($results);
if($resultsCheck > 0):
$string = '<br><select id="choice">';
while($rows = mysqli_fetch_assoc($results)):
$string .= '<option value="'.$rows['Choice_ID'].'">'.$rows['Choice'].'</option>';
endwhile;
$string .= '</select><br>';
echo $string;
endif;
endwhile;
endif;
?>
Organize your code like this (simplified for readability)
while($question = mysqli_fetch_assoc($result))
{
echo "<label for='q'>...; // print question
echo "<select name='answer'>"
while ($answer = mysqli_fetch_assoc($results))
{
echo '<option>'.$answer.</option>';
}
echo '</select>';
}
i am using Php/Mysql , i have the client table and trying to display data in a drop down list. Unfortunately, only one client is displayed in drop down which i have the total of 3 clients. Why only one ? For example : Michael King, Michael Jordan , Michael John when i select all the data from table and make an output to display in dropdown, Michael John is only in the dropdown.
Here my Mysql code :
//All data is selected from client_tb
<?php
$sql = "SELECT * FROM client_tb";
$result = $conn->query($sql);
while($row=mysqli_fetch_array($result))
{
$id = $row['id'];
$lname = $row['lname'];
$fname = $row['fname'];
}
?>
//my dropdown which will show the clients from client_tb but only one will appear.
<option value ="<?=$lname?><?=$fname?>"><?=$lname?> , <?=$fname?> </option> </select><br><br>
You can also achieve dropdown outside the while loop. Try this:
$sql = "SELECT * FROM client_tb";
$result = $conn->query($sql);
$options =array();
while($row=mysqli_fetch_array($result))
{
$options[] =$row;
}
Your dropdown:
<select name="">
<?php
foreach($options as $option):
echo '<option value ="'.$option['lname'].''.$option['fname'].'">'.
$option['lname'].','.$option['fname'].'</option>';
endforeach;
?>
</select>
You could also add your db query into a function , then call it.
function myFunction() {
$sql = "SELECT * FROM client_tb";
$result = $conn->query($sql);
while($row=mysqli_fetch_array($result))
{
$myvalues[] =$row;
}
return $myvalues;
}
Now the dropdown,
Note the options are inside the loop
<select name="">
<?php foreach($myvalues as $myvalue) {
echo '<option value="'.$myvalue['lname'].''.$myvalue['fname'].'">'.
$myvalue['lname'].','.$myvalue['fname'].'</option>';
}
?>
</select>
I am new in programing and i have stuck in one piece.
Well I have 1 database, in that database I have some teams, my goal is to put those teams in a option tag where I can modify the team. Thanks all!
$sql_grupi = "select grup_name from grupi";
$res_grupi = mysqli_query($dbCon, $sql_grupi);
$res_array = array();
if (!$res_grupi) {
die("Error");
}
while ($row = mysqli_fetch_array($res_grupi, MYSQLI_ASSOC)) {
$res_array[] = $row['grup_name'];
}
down here is my form where i call the table data
<select name="grupi">
<option value="<?php $res_array; ?>"><?php $res_array; ?></option>
</select>
you can do it through loop array:
echo '<select name="grupi">';
while ($row = mysqli_fetch_array($res_grupi, MYSQLI_ASSOC)) {
echo "<option value='".$row['grup_id']."'>".$row['grup_name']."</option>"
}
echo '</select>';
Okay so I have a table called Countries and it looks like this:
---------------------------
|Country | Code |
---------------------------
|Afganastan | AF |
|Ă…LAND ISLANDS| AX |
| etc. | etc. |
---------------------------
The thing that I want to do is create a dynamic menu in which the user chooses a country and that itself gets stored as a value that I can call after the user hits submit.
I did try something here but I'm not sure what its doing because I am still new to PHP and HTML to the point where I just type things in to see what would happen.
Anyways I am really stuck and I tried using google and the search feature in this site and nothing I found worked for me...
The code I tried is this:
<select>
<?php
$result = mysql_query('SELECT Country FROM Countries');
echo '<select name="country">';
while ($row = mysql_fetch_array($result))
{
echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
echo '</select>';
?>
</select>
The result is supposed to look like a dropdown menu with the list of countries from the database in it. But this doesn't work and just shows this in the drop down:
.$row['name']
Which is nothing close to what I want because that's not even a country. when I remove that part of the code, then there is no option for the user to choose, the menu is empty.
EDIT
My code so far that still doesn't work:
<select name = 'country'>
<?php
include ("account.php");
include ("connect.php");
$result = mysql_query('SELECT Code , Country FROM Countries');
while ($row = mysql_fetch_array($result))
{?>
<option value="<?php echo $row['Code']?>"><?php echo $row['Country']?></option>
<?php}
?>
</select>
The include ("account.php"); and include ("connect.php"); lines allow me to connect to my database.
you code should be something like this
$host = "localhost";
$user = "root";
$pass = "yourpassword";
$db = "databasename";
// This part sets up the connection to the
// database (so you don't need to reopen the connection
// again on the same page).
$ms = #mysql_connect($host, $user, $pass);
if ( !$ms )
{
echo "Error connecting to database.\n";
}
// Then you need to make sure the database you want
// is selected.
#mysql_select_db($db);
<form method = "POST" action = "abc.php">
<select name = 'country'>
<?php
$result = mysql_query('SELECT id , name FROM Countries');
while ($row = mysql_fetch_array($result))
{?>
<option value="<?php echo $row['id']?>"><?php echo $row['name']?></option>
<?php}
?>
<input type = "submit" value = "Submit">
</form>
Now in php use this
echo '<pre>';
print_r($_POST);
And you will see what user selected. Check your settings there might be some problem.
Your single and double quotes are messing you up:
echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
should be:
echo "<option value=\"" . $row['id'] . "\">" . $row['name'] . "</option>";
You can use a single quote around your script but when you jump out of it to do the $row['id'] and $row['name'] you are running into issues because it thinks you are jumping back into your quoted code... Either use my example above, starting/ending with double-quotes and escaping all double-quotes inside that need to display, or escape your single quotes in the $row[\'id\'] and $row[\'name\']
Thant should help you out.
Try this code
<?php
$result = mysql_query('SELECT * FROM Countries');
?>
<select name="country">
<?php
while ($row = mysql_fetch_array($result))
{
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?></option>
<?php
}
?>
</select>
Firstly your table doesn't have a column id. Try changing your query like
SELECT Country, Code FROM Countries
Then the code and the html should be like this
<?php
$host = "localhost";
$user = "user"; //username
$pass = "pass"; //password
$db = "db"; //database
$con = #mysql_connect($host, $user, $pass);
if ( !$con )
{
echo "Error connecting to database.\n";
}
#mysql_select_db($db);
?>
<select name="country">
<option value="0" selected="selected">Choose..</option>
<?php
//echo '<select name = \'country\'>';
$result = mysql_query('SELECT Country, Code FROM Countries');
while ($row = mysql_fetch_array($result))
{
echo '<option value="'.$row['Code'].'">'.$row['Country'].'</option>';
}
?>
</select>
<select>
<?php
$result = mysql_query('SELECT Country FROM Countries');
echo '<select name="country">';
$row = mysql_fetch_array($result)
for ($i=0; $i<count($row ); $i++)
{
echo '<option value="'.$row[$i]['id'].'">'.$row[$i]['name'].'</option>';
}
echo '</select>';
?>
next page use print_r($_POST);
or var_dump($_REQUEST);
If you are using mysql_fetch_array you can use either the field names or their selected index to read them from the fetched row. You can also use either the sprintf or printf functions to merge content into a string to help keep the HTML fragment clean of the quotes needed to merge in values otherwise.
$result = mysql_query('SELECT Country, Code FROM Countries');
while ($row = mysql_fetch_array($result)) {
printf('<option value="%1$s">%2$s</option>',
$row['Code'], $row['Country']);
}
Your SQL statement selected only 'Country' from the 'Countries' table; as a result, it's impossible for you to use $row['id'] and $row['name'].
Use this instead:
echo '<select name="country">';
while ($row = mysql_fetch_array($result))
{
echo '<option value="'.$row['Code'].'">'.$row['Country'].'</option>';
}
echo '</select>';
?>
That should solve your problem.
I figured out the problem I was having. The first being that there was an extra select tag in the page and the second that the file was saved as a html page instead of a php file. Thank you to everyone that helped me figure this out!
Try my code, I'm using this and it really works... just change the values...
<?php
include ('connect.php');
$sql = "SELECT * FROM casestatusfile";
$result = mysql_query($sql);
echo "<select name = 'txtCaseStatus'/>";
echo "<option value = ''>--- Select ---</option>";
$casestatus = $_POST['txtCaseStatus'];
$selected = 'selected = "selected" ';
while ($row = mysql_fetch_array($result)) {
echo "<option " .($row['CASESTATUSCODE'] == $casestatus? $selected:''). "value='". $row['CASESTATUSCODE'] ."'>" . $row['CASESTATUS'] ."</option>";
}
echo "</select>";
?>
I'm sure that is working because that is the one that i'm using....
so as the title states, using the following code I have got it populating the dropbox with a single result from the query, that result being the latest added in the table.
here is my code:
<?php
$query = "SELECT * FROM units_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_assoc($result)){
$aa = "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
}
?>
<select name="t_unit"><? echo $aa; ?></select>
The odd thing is, I use this same code for another field, and it works, populating the dropdown with all the results, however in this case it only fills in the last unit code in the table and not all of which are attached to the particular user id.
I would appreciate anyones thoughts :D
thanks
$aa .= "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
add . before = and initiate $aa = ''; before while loop
<?php
$query = "SELECT * FROM units_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die (mysql_error());
$options = "";
while($row = mysql_fetch_assoc($result)){
$options .= "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
}
?>
<select name="t_unit"><? echo $options; ?></select>
should work. You forgot a . in your while loop