I've got a login form which I'm trying to simplify. It all worked when you manually inputted your username and password. Now I am wanting a drop down for the username box from the MySql database.
This is the code that I have put into the form and it drops down and shows all the users but when you select it, put the password in and click login it doesn't pass the username.
<?php
mysql_connect('localhost', 'user', 'password.');
mysql_select_db('database');
$sql = "SELECT username FROM users";
$result = mysql_query($sql);
echo "<select username='sub1'>";
while ($row = mysql_fetch_array($result)) {
echo"<option value'" . $row['username'] ."'>" . $row['username'] ."</option>"; } echo "</select>";?>
Any Ideas Anyone
Thanks
Tom, always check your generated HTML to investigate errors.
Change your code to:
<?php
mysql_connect('localhost', 'user', 'password.');
mysql_select_db('database');
$sql = "SELECT username FROM users";
$result = mysql_query($sql);
echo "<select name='username'>"; // Note name attribute
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>"; // Note `=` sign after value
}
echo "</select>";
?>
replace your code with this:
<?php
mysql_connect('localhost', 'user', 'password.');
mysql_select_db('database');
$sql = "SELECT username FROM users";
$result = mysql_query($sql);
echo "<select name='sub1'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>";
}
echo "</select>";
?>
Related
I am using the below code to populate drop down list in php html,
<?php
$mid="mario";
$sql = "SELECT * FROM tbl_prdy WHERE col_master_id = '$mid'";
$result = mysqli_query($conn,$sql);
echo "<select name='list'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['col_of_fa'] . "'>" . $row['col_of_fa'] . "
</option>";
}
echo "</select>";
?>
But, I am getting internal server error. I have debugged the code and found that the issue is with the following 2 lines in the above code. There is not much information in server logs. Can you tell me what might be the issue with the following 2 lines of code?
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['col_of_fa'] . "'>" . $row['col_of_fa'] .
"/option>";
}
mixing mysqli with mysql
change
$row = mysql_fetch_array($result)
to
$row = mysqli_fetch_array($result)
You would use this
while($row=mysqli_fetch_assoc($result )){
}
or
while($row=mysqli_fetch_array($result )){
}
//Try this :
while ($row = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row['col_of_fa'] ?>" ><?php echo $row['col_of_fa'] ?>
</option>
<?php }
<?php
$con = new mysqli('localhost', 'root' ,'', 'world');
$query = 'SELECT * FROM city ORDER BY Name';
if ($result = mysqli_query($con, $query)) {
echo "<table>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>"
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['CountryCode'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
}
mysqli_close($con);
?>
This simple code will display every entries in this database. Now I would like to add a selective display option by choosing the CountryCode.
$query2 = 'SELECT DISTINCT CountryCode FROM city ORDER BY CountryCode';
How do I use the result I got from the query above and make it become radio buttons to choose what to display?
Similar to what you are doing already: Something like
while ($row = mysqli_fetch_assoc($result)) {
echo "<input type='radio' name='whatever' value='".$row['Name']."'>". $row['Name'];
}
Ofcourse the field names can be whatever you like them to be, as long as you select them in the query
I am populating a drop down menu from mysql database. It works well, But I want it not to repeat values. (i.e if some value is N times in database it comes only once in the drop down list)
Here is my code:
<?php
mysql_connect('host', 'user', 'pass');
mysql_select_db ("database");
$sql = "SELECT year FROM data";
$result = mysql_query($sql);
echo "<select name='year'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['year'] . "'>" . $row['year'] . "</option>";
}
echo "</select>";
?>
Use DISTINCT in your query.
"SELECT DISTINCT year FROM data";
just change Your query. is better
$sql = "SELECT distinct year FROM data";
Another technique:
select year from table group by year
in PHP side you have to do this
$all_data = array();
echo "<select name='year'>";
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
array_push($all_data,$row["column"]);
}
$all_data = array_unique($all_data);
foreach($all_data as $columns => $values){
print("<option value='$value'>$value</option>");
}
echo "</select>";
Here is a simple trick. Take a boolean array. Which value has not come in list print it in list and which value has come in list already once, set it as true through indexing the boolean array.
Set a condition, if boolean_array[ value ] is not true, then show value in list. Otherwise, don't.
mysql_connect('host', 'user', 'pass');
mysql_select_db ("database");
$sql = "SELECT year FROM data";
$result = mysql_query($sql);
echo "<select name='year'>";
while ($row = mysql_fetch_array($result)) {
if($bul[$row['year']] != true){
echo "<option value='" . $row['year'] . "'>" . $row['year'] . " </option>";
$bul[$row['year']] = true;
}
}
echo "</select>";
?>
I am trying to populate a Drop down box from results of a mySQL Query, in Php. I've looked up examples online and I've tried them on my webpage, but for some reason they just don't populate my drop down box at all. I've tried to debug the code, but on the websites I looked at it wasn't really explained, and I couldn't figure out what each line of code. Any help would be great :)
Here's my Query: Select PcID from PC;
You will need to make sure that if you're using a test environment like WAMP set your username as root.
Here is an example which connects to a MySQL database, issues your query, and outputs <option> tags for a <select> box from each row in the table.
<?php
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT PcID FROM PC";
$result = mysql_query($sql);
echo "<select name='PcID'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['PcID'] . "'>" . $row['PcID'] . "</option>";
}
echo "</select>";
?>
Below is the code for drop down using MySql and PHP:
<?
$sql="Select PcID from PC"
$q=mysql_query($sql)
echo "<select name=\"pcid\">";
echo "<option size =30 ></option>";
while($row = mysql_fetch_array($q))
{
echo "<option value='".$row['PcID']."'>".$row['PcID']."</option>";
}
echo "</select>";
?>
Since mysql_connect has been deprecated, connect and query instead with mysqli:
$mysqli = new mysqli("hostname","username","password","database_name");
$sqlSelect="SELECT your_fieldname FROM your_table";
$result = $mysqli -> query ($sqlSelect);
And then, if you have more than one option list with the same values on the same page, put the values in an array:
while ($row = mysqli_fetch_array($result)) {
$rows[] = $row;
}
And then you can loop the array multiple times on the same page:
foreach ($rows as $row) {
print "<option value='" . $row['your_fieldname'] . "'>" . $row['your_fieldname'] . "</option>";
}
No need to do this:
while ($row = mysqli_fetch_array($result)) {
$rows[] = $row;
}
You can directly do this:
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['value'] . "'>" . $row['value'] . "</option>";
}
At the top first set up database connection as follow:
<?php
$mysqli = new mysqli("localhost", "username", "password", "database") or die($this->mysqli->error);
$query= $mysqli->query("SELECT PcID from PC");
?>
Then include the following code in HTML inside form
<select name="selected_pcid" id='selected_pcid'>
<?php
while ($rows = $query->fetch_array(MYSQLI_ASSOC)) {
$value= $rows['id'];
?>
<option value="<?= $value?>"><?= $value?></option>
<?php } ?>
</select>
However, if you are using materialize css or any other out of the box css, make sure that select field is not hidden or disabled.
After a while of research and disappointments....I was able to make this up
<?php $conn = new mysqli('hostname', 'username', 'password','dbname') or die ('Cannot connect to db') $result = $conn->query("select * from table");?>
//insert the below code in the body
<table id="myTable"> <tr class="header"> <th style="width:20%;">Name</th>
<th style="width:20%;">Email</th>
<th style="width:10%;">City/ Region</th>
<th style="width:30%;">Details</th>
</tr>
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row['username']."</td>";
echo "<td>".$row['city']."</td>";
echo "<td>".$row['details']."</td>";
echo "</tr>";
}
?>
</table>
Trust me it works :)
What if you want to use both id and name in the dropdown? Here is the code for that:
$mysqli = new mysqli($servername, $username, $password, $dbname);
$sqlSelect = "SELECT BrandID, BrandName FROM BrandMaster";
$result = $mysqli -> query ($sqlSelect);
echo "<select id='brandId' name='brandName'>";
while ($row = mysqli_fetch_array($result)) {
unset($id, $name);
$id = $row['BrandID'];
$name = $row['BrandName'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
What i'm trying to do is display a drop down with all field names from mysql database, once the user picks one and submits the form i want to display a second dropdown filled with all the rows from the submitted field name, this is my code so far:
$result = mysql_query("select * from `parts`") or die(mysql_error());
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>";
echo "<select name='field_names'>";
$i = 0;
while ($i < mysql_num_fields($result)) {
$fieldname = mysql_field_name($result, $i);
echo '<option value="'.$fieldname.'">'.$fieldname.'</option>';
$i++;
}
echo "</select>";
echo "<input type='submit' value='submit'></input>";
echo "</form>";
if($_POST) {
$fields = $_POST['field_names'];
$result1 = mysql_query("select '".$fields."' from `parts`") or die(mysql_error());
echo '<select name="fields">';
while ($row = mysql_fetch_array($result1)) {
echo "<option value=".$row[$fields].">".$row[$fields]."</option>";
}
echo '</select>';
}
Can anyone spot where i'm going wrong, thanks
there is a mistake on the line number 29
$result1 = mysql_query("select '" . $fields . "' from `parts`") or die(mysql_error());
you are using ' instead of `. Do as follows
$result1 = mysql_query("select `" . $fields . "` from `parts`") or die(mysql_error());
Hope your problem is solved.
As it stands now, the second set of selects will be issued OUTSIDE of your </form> tag, so will never get submitted with the rest of the form. At best, you should move the form closing tag to below the POST handler.
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