I am a newbie in php. I am trying to write code for displaying dropdown select from database(my_db).
I've attached the code here:
<?php
// Create connection
$con=mysqli_connect("localhost",$dbuname,$dbpwd,"my_db") or die("Couldn't connect!!!". mysqli_error());
mysqli_select_db($con,"my_db");
$result = mysqli_query($con,"Select country from Country");
$rowcount = mysqli_num_rows($result);
//echo $rowcount;
if($rowcount) {
$select = '<select name="select">';
//echo $select;
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)) {
//printf ("%s \n",$row["country"]);
//echo "<br>";
$select.='<option value="'.$row['country'].'">'.$rs['country'].'</option>';
//echo $select;
}
$select .= "</select>";
echo $select;
}
?>
I refer this link for writing this code.
But, I didn't get the output. The dropdown box would be blank.
What did I make as wrong?
Please give more idea to improve my code.
Thank you in Advance!!!
put php code inside html tag.
<select id="selectbox" name="selectbox">
<?php
//here your query
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)) {?>
<option value="<?php echo $row['country'];?>"><?php echo $rs['country'];?></option>
<?php }?>
</select>
echo '<select name="select">';
while($row=mysqli_fetch_array($result)) {
echo '<option value="'.$row['country'].'">'.$row['country'].'</option>';
}
echo '</select>';
it worked for me.
//db connection
mysql_connect("localhost","user","password");
mysql_select_db("database");
//query
$sql=mysql_query("SELECT id,name FROM table");
if(mysql_num_rows($sql)){
$select= '<select name="select">';
while($rs=mysql_fetch_array($sql)){
$select.='<option value="'.$rs['id'].'">'.$rs['name'].'</option>';
}
}
$select.='</select>';
echo $select;
Related
I have a form designed with bootstrap style and I want to retrieve data from a database into a drop-down list. I tried this code, but I get a list with no text.
<div class="form-group"><label class="col-sm-2 control-label">Location</label>
<div class="col-sm-8"><select class="form-control m-b" name=Location>
<?php
$mysqli = new mysqli( 'localhost', 'cshrnaf_user2', '=cXlIBsdMkdr', 'cshrnaf_mis_db' );
/* check connection */
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM adhoc";
$result = mysqli_query($mysqli,$query);
while($rows = mysqli_fetch_assoc($result))
{
echo "<option value=" . $rows['Aim'] . "></option>";
echo "<option value='{".$d['Aim']."}'>".$d['Aim']."</option>";
}
?>
$d is never mentioned before you try to echo it, so what is it's content?
echo "<option value=" . $rows['Aim'] . "></option>"; has two issues: firstly, the value is not encased in quotes, and secondly, there is no content in the tag, so no text will output.
Try changing your while to:
while($rows = mysqli_fetch_assoc($result)){
echo "<option value='{$rows['Aim']}'>{$rows['Aim']}</option>";
}
As long as $rows['Aim'] contains the content you wish to output, this should work.
// cat is array fetch from database
<select name="cat" id="cat_id" style="width:170px" value="<?php echo $_POST['cat']; ?>">
<option value="0"></option>
<?php
foreach ($cat as $row) {
?>
<option value="<?php echo $row->tag_cat_id ?>" ><?php echo $row->tag_cat_name ?></option>
<?php }
?>
</select>
</select>
You should select database in your file.Above you only connect to your localhost.And you should use variable name $rows for all fetch values
if (!mysqli_select_db("mydbname")) {
echo "Unable to select database: " . mysqli_error();
exit;
}
I am trying to edit my form. I want to get selected value selected in selection list.
I have created function to store values in database, and it works. Below is html code I use and function below to insert values in database.
// insert values in database
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" required>
<?php dobavljac() ?>
</select>
function dobavljac(){
$sql=mysqli_query($link, "SELECT * FROM `partneri` WHERE `Dobavljac`='1'
order by `PartnerId` asc ");
echo '<option value="">Izaberi dobavljača</option>';
while($record = mysqli_fetch_array($sql)) {
echo '<option value= "' .$record['PartnerId']. '">' . $record['PartnerNaziv'] . ' </option>';
}
}
// edit values
First I retrieve information from database
$id=$_GET['id'];
$sql = "SELECT * FROM materijali where Id=$id ";
$q = $conn->query($sql);
$r = $q ->fetch();
if ($r) {
$dobavljac=$r['Dobavljac'];
I want to get selected value in box
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" value="<?php echo $dobavljac; ?>">
<?php dobavljac() ?>
</select>
Probably I am not doing it the right way, any advice would be appreciated
Try this
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" value="<?php echo $dobavljac; selected?>">
<option value=<?php echo $dobavljac?> selected>
<?php dobavljac() ?>
</option>
</select>
Try this...
$id=$_GET['id'];
$sql = "SELECT * FROM materijali where Id=$id ";
$q = mysql_query($query);
echo "<select name="dobavljac" class="form-control">";
while (($row = mysql_fetch_row($q)) != null)
{
echo "<option value = '{$row['Dobavljac']}'>";
echo $row['Dobavljac'];
echo "</option>";
}
echo "</select>";
We managed to get the drop down list menu however, we are having difficulties getting the data from sql. So far, this is what we got.
<select>
<option id="">--Select jobscope--</option>
<?php
$con=mysqli_connect("host","user","password","database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$getIT = mysqli_query("SELECT job_title FROM `job_details`");
while($viewIT = mysqli_fetch_array($getIT)) {
}
?>
<option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option>
</select>
Shouldn't be like this ? with tag inside WHILE LOOP
while($viewIT = mysql_fetch_array($getIT)) {
<option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option>
}
$query = "SELECT * FROM test_groups_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$dd .= "<option value='{$row['group_id']}'>{$row['group_name']}</option>";
}
Try this,
<option value="">--select--</option>
<?php
while($rec = mysql_fetch_assoc($result)) {
?>
<option value="<?=$rec['job_title']?>"><?=$rec['job_title']?></option>
<?php }
}?>
</select>
I am not from php background. Try this.
<?php
$query = "SELECT job_title FROM job_details";
$result = $mysqli->query( $query );
echo '<select id="domain_account" name="domain_account" class="txtBox">';
echo '<option value="">-select-</option>';
while ($row = $result->fetch_assoc()){
?>
<option value="<?php echo $row['job_title']; ?>"><?php echo $row['job_title']; ?></option>
<?php
}
echo "</select>";
?>
Better use PDO or MYSQLi . MYSQL* is depriciated
U need to use that <option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option> line b/w while loop
$getIT = mysql_query("SELECT job_title FROM `job_details`");
while($viewIT = mysql_fetch_array($getIT)) {?>
<option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option>
<?hp }?>
$sql = "select custName from customer where active = 1 order by custName";
sult=mysql_query($sql);
echo"<select name='custNameColo'>";
while($row = mysql_fetch_array($result)) {
if($row['custName']==$_GET['defaultCust']) {
echo "<option value=".$row['custName']."selected = 'selected'>".$row['custName']."</option>";
}
else {
echo "<option value=".$row['custName'].">".$row['custName']."</option>";
}
}
echo "</select>";
I want set a default value a drop down menu, but it doesn't work, please help me.
<?php
$sql = "select custName from customer where active = 1 order by custName";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result)){
?>
<option value="<?php echo $row['custname'] ?>" <?php if($row['custname']==$_GET['defaultCust']) { echo "selected";}?>>
<?php echo $row['custname']; ?></option>
<?php
}
?>
<? echo "TRY this it will work";
$sql = "select custName from customer where active = 1 order by custName";
$result=mysql_query($sql);
?>
<select name="custNameColo">
<?
while($row = mysql_fetch_array($result)){?>
<option value="<?=$row['custname'];?>" <? if($row['custname']==$_GET['defaultCust']){?> selected="selected" <? }?>><?=$row['custname'];?></option>
<? } ?>
</select>
You didn't added single quotes in the value of option tag before and after. I done it for you. The default value was not selected because value attribute was not properly closed.
$sql = "select custName from customer where active = 1 order by custName";
$result=mysql_query($sql);
echo "<select name='custNameColo'>";
while($row = mysql_fetch_array($result)){
if($row['custName']==$_GET['defaultCust']){
echo "<option value='".$row['custName']."' selected = 'selected'>".$row['custName']."</option>";
}
else {
echo "<option value='".$row['custName']."'>".$row['custName']."</option>";
}
}
echo "</select>";
I wanted to make a dropdown area by getting the values from a mysql table. This code does not seem to work; all it prints out is the box for the dropdown without the content, how can I get this to work? or is there an alternative procedure in doing this?
<?
$connection = mysql_connect("localhost", "root", "");
mysql_select_db("test", $connection);
$query = "SELECT full_name FROM test";
$names = mysql_query($query);
function dropDown($content, $result)
{
while($row = mysql_fetch_row($result))
{
$name = $row[0];
$content .= "<option value='$name'>$name</option>";
}
}
$content=<<<EOT
<html>
<body>
<select name="name">
EOT;
dropDown($content, $names)
$content.=<<<EOT
</select>
</body>
</html>
EOT;
echo $content;
?>
return the string. PHP is not C where you use out parameters just because they are sometimes handy.
function dropDown($result, $fieldName)
{
$content = '';
while($row = mysql_fetch_row($result)) {
$name = $row[0];
$content .= "<option value='$name'>$name</option>";
}
return '<select name="'.$fieldName.'">'.$content.'</select>';
}
$content = '<html><body>';
$content .= dropDown($names, 'name');
here database details
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT username FROM userregistraton";
$result = mysql_query($sql);
echo "<select name='username'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>";}
echo "</select>";
here username is the column of my table(userregistration)
it works perfectly
or simply the code is
<?
$sql = "SELECT * FROM userregistraton ";
$result = mysql_query($sql); ?>
<select name="username" id="username">
<option value="">Select user</option>
<?php
while ($row = mysql_fetch_array($result))
{ ?>
<option value="<?php echo $row['uid']?>" <?php if($row['uid']==$_POST["username"]) echo "selected"; ?> >
<?php echo $row['username'];?></option>
<?php
} ?>
<?php echo $form->dropDownList($model,'courseprefer', array(
'(*value in database table*'=>'displaying value',
)); ?>
you can manually add them just make sure that the first value in array is in the database table.. :)
to avoid error!