dynamic dropdown filled from mysql data - php

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

Related

HTML form drop down select issue

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>";
?>

How to use MySQL query results as options in HTML forms

<?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

PHP Script, Table data populating from form

<html><head></head><body>
<?php
$db_hostname = "mysql";
$db_database = "u1da";
$db_username = "u1da";
$db_password = "1234";
$con = mysql_connect($db_hostname ,$db_username ,$db_password);
if (!$con) die ("Unable to connect to MySQL: ".mysql_error ());
mysql_select_db ( $db_database ) ||
die (" Unable to select database : ". mysql_error());
$query = "select * from students";
$result = mysql_query($query);
$result || die ("Database access failed: ".mysql_error());
$rows = mysql_num_rows($result);
echo "<table border=1>";
echo "<tr><td><p><b>Name</b></p></td></tr>";
for ($j = 0; $j < $rows ; $j ++) {
echo "<tr><td>", mysql_result($result,$j,'name') ,"</td></tr>";
}
echo "</table><br />";
$query2 = "select * from groups";
$result2 = mysql_query($query2);
$result2 || die ("Database access failed: ".mysql_error());
$rows2 = mysql_num_rows($result2);
echo "<table border=1>";
echo "<tr><td><p><b>Tutorial Group</b></p></td><td><p><b>Capacity</b></p></td></tr>";
for ($i = 0; $i < $rows2 ; $i ++) {
echo "<tr><td>", mysql_result($result2,$i,'Tutorial_Group') ,"</td><td>", mysql_result($result2,$i,'Capacity') ,"</td></tr>";
}
echo "</table>";
echo "<form method='post' action='' enctype="multipart/form-data">";
$query3 = "select * from students";
$result3 = mysql_query($query3);
echo "<br /><br /><select name='name'>";
while($name = mysql_fetch_array($result3)) {
echo "<option value='$name[Name]' > $name[Name] </option>"."<BR>";
}
echo "</select><br />";
$query4 = "select Tutorial_Group from groups";
$result4 = mysql_query($query4);
echo "<select name = 'group'>";
while($grp = mysql_fetch_array($result4)){
echo "<option value='$grp[Tutorial_Group]'>$grp[Tutorial_Group]</option>";
}
echo "</select><br />";
echo "Student ID: ";
echo '<input type="text" name="SID"><br />';
echo "Email: ";
echo '<input type="text" name="email"><br />';
echo '<input type="submit" name="submit" value="Submit">';
echo "</form>";
?>
Here is my current script code.
I have to make a query which will get the value from the 2 drop-down menus and 2 text fields and insert them into a table.
Table is called assg and have columns: Name, Student_ID, email, s_group. I have tried some different ways, but it didn't worked out. Please help.
Your first problem is that you are using mysql_fetch_array to get an array of elements into $grp variable but next you try to use it with a associative array to get values like $grp[Tutorial_Group]
Using mysql_fetch_array you can get $grp[0] ... $grp[1] but not $grp[Tutorial_Group]
You need to use mysql_fetch_assoc to get associative arrays like $grp[Tutorial_Group]
Your second problem is using complex php vars incorrectly example
Incorrect:
echo "<option value='$grp[Tutorial_Group]'>$grp[Tutorial_Group]</option>";
Correct:
echo '<option value="'.$grp['Tutorial_Group'].'">'.$grp['Tutorial_Group'].'</option>';
Or
echo "<option value='{$grp['Tutorial_Group']}'>{$grp['Tutorial_Group']}</option>";
Also the code only has the part to view the tables and the form. The insert part of the data is missing in the example. Probably this part has also some errors.
No indentation, html in echos, i don't even understand what you want.
By dropdown you mean select ?
What about $_POST['nameOfTheField'] ?
And why multipart ? There's no files to send here.

populate drop down list from mysql database and don't repeat values

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>";
?>

Edited: Retrieve Table entries by adding ID attribute to file name?

I have a large database of venues - and I would like to display this data in one page that would only change in some sort of an attribute to the id: (Ex: venues.php?id=1, which would get all the data from row #1.)
Edit: Okay, I updated the code and this is what it looks like now:
<?php
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());
$id = (int) $_GET['id'];
$data = mysql_query("SELECT * FROM venues WHERE id = ".(int)$id) ;
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['VENUE_NAME'] . "</td> ";
Print "<th>Address:</th> <td>".$info['ADDRESS'] . " </td></tr>";
}
Print "</table>";
?>
And upon going to venues.php?id=1 I get this error:
Parse error: syntax error, unexpected T_LOGICAL_OR in
/home/nightl7/public_html/demos/venues/venues.php on line 8
Do you mean something like:
$id = (int) $_GET['id'];
$data = mysql_query("SELECT * FROM venues WHERE id = ".(int)$id) ;
In order to "pass" the id into your url "venues.php?id=1"
You need to use a hybrid html/php form with method=get.
You can see an example html form here: w3schools html forms
This is what I would do:
print '<form name="input" action="venues.php" method="get">';
print 'Venue: <select name = "id">';
$con = mysql_connect("","","");
mysql_select_db($dataBase);
if (!$con){die('Could not connect: ' . mysql_error());}
else {
$opt = array();
$optVal = array();
$i = 0;
$sql = "Select * from venues";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
$opt[$i] = $row['VenueName'];
$optVal[$i] = $row['VenueID'];
print "<option value='$optVal[$i]'>$opt[$i]</option>";
$i++;
}
}
mysql_close($con);
print '</select><br />';
print '<input type="submit" value="Submit" />';
print '</form>'
This will give you a form that will give you a drop down list of all your venues and once a venue is selected will direct you to the venues.php page with the respective id.
at the top of your venues,php page just use
$id = $_GET['id'];
This assigns the id number to the variable $id and then you can use this "select"
$data = mysql_query("SELECT * FROM venues WHERE id = ".$id) ;
To collect your venue name from your database using the id supplied in the form.
Good Luck :)
<?php
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());
$id = (int) $_GET['id'];
$data = mysql_query("SELECT * FROM venues WHERE id = ".$id) or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['VENUE_NAME'] . "</td> ";
Print "<th>Address:</th> <td>".$info['ADDRESS'] . " </td></tr>";
}
Print "</table>";
?>

Categories