Problems with populating forms with queried information

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

brookside
Forum Commoner
Posts: 30
Joined: Tue Mar 02, 2004 8:15 pm

Problems with populating forms with queried information

Post by brookside »

Hello. I have figured out how to put information into a form that I have queried from the database. I have done so with txtboxes. How would I do something like that for a dropdown menu. or even a radio button??? There are quite a few drop down menus in the form, but this is one of them. What we are going to do is allow the user to select his/her title and store it in the database. The user will then have an option to view their information, and the only thing I want to see is just the value stored in the database...

Please help...

Here is my code.

<select name="mnuTitle" id="mnuTitle">
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Ms">Ms</option>
<option value="Dr">Dr</option>
</select>

Thank you.
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post by The Monkey »

ALRIGHT! Something I can help with! w00t

Ok.

I'm going to assume you are using the Post method.

Insert this code into your php script:

Code: Select all

<?php
$mnuTitle = $_POST['mnuTitle'];
$sql_query = "INSERT $mnuTitle INTO tablename";
$sql_result = mysql_query($sql_query);
?>
Now, you have more variables to insert into the database, I'm sure. If you would like to post your exact code you have so far I would be glad to finish it up with the $mnuTitle code I posted above!

Now, I'm not sure how much php you know, but the above code won't work unless you first hook it up with your script and make some changes to it!

Hope I've helped!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<select name="mnuTitle" id="mnuTitle">

<?
while ( $row = mysql_fetch_array($result) ) { 
		$value=      $row["value"]; 
		$select=     $row["select"]; 

echo "<option value="$value">$select</option>";

}
?>

</select>
This will create as many fields as you have in the database. Althought you must have the value and the name inputted on the database. Not really clear if this is what you wanted.. just my stab in the dark
brookside
Forum Commoner
Posts: 30
Joined: Tue Mar 02, 2004 8:15 pm

Post by brookside »

<select name="mnuTitle" id="mnuTitle">

<?
while ( $row = mysql_fetch_array($result) ) {
$value= $row["value"];
$select= $row["select"];

echo "<option value=\"$value\">$select</option>";

}
?>

</select>

I tried the code that was posted up by Phenom. Whenever I do this in my drop down box I have $select print out. I tried putting this in quotes and other various techniques, but for some reason it things that it is a string, but we need it to recognize that it's a variable....

Please help,,
Thank you
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

Code: Select all

<?php
( $row = mysql_fetch_array($result) 
?>
do you have a $result variable defined?

like something:

Code: Select all

<?php
$sql = "SELECT * FROM table_name";
$result= mysql_query($sql);
// etc etc
?>
????
brookside
Forum Commoner
Posts: 30
Joined: Tue Mar 02, 2004 8:15 pm

Post by brookside »

Yes, I am able to query the database and populate forms with text information, for instance a first or last name.....But as for a drop down menu, I can't get it to work. Lets say for example a person selects one state from a drop down menu. When a user is done and clicks submit that information is stored in the database. That works fine. That one state that the user picked is stored in the database. Now, the user will be able to view his or her information in the database. How do I select that state from the database and put it in a dropdown....Just that one state.
When I try the code..

<select name="mnuTitle" id="mnuTitle">
$temp = $rows['State'];
$select = $rows['State'];
<option value="$temp">$select</option>
</select>

This will create drop down menu in my form and put the $select into it. But I want the state.

Please help
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You need to show the full/exact code you are using to do it. And why are you putting it in a select box if it's just one value that can't be changed?
brookside
Forum Commoner
Posts: 30
Joined: Tue Mar 02, 2004 8:15 pm

Post by brookside »

$query = 'SELECT * from MEMBER, ADDRESS, HONORARY, INITIATION';
$result = mysql_query($query);
$numRows = mysql_num_rows($result);

if($numRows > 0)
{
while($rows = mysql_fetch_array($result))
{
echo '<form action="" method="post">
<table width="98%" border="0" align="center" cellspacing="2">
<tr>
<td width="20%">Title:</td>
<td colspan="2"> <select name="mnuTitle" id="mnuTitle">
$temp = $rows['State'];
$select = $rows['State'];
<option value="$temp">$select</option>
</select></td>
<td width="17%">First Name:</td>
<td width="34%"><input name="txtFirstName" type="text" id="txtFirstName2" value="'.$rows['Name_First'].'"></td>
</tr>
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

edit:
</useless>
Last edited by tim on Wed Mar 10, 2004 8:58 pm, edited 1 time in total.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

The whole long wrong echo line that echos the form and table and stuff should be before your do the while. If its in the while then its going to add that over and over again. Same for the last bit. All you need in the while loop is

$temp = $rows['State'];
$select = $rows['State'];
<option value="$temp">$select</option>

And just so you now, you can't just put HTML in wiht your php code. You either need to echo it out, or end your php tags and have teh HTML and then start your PHP tags back.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

now that you posted your code, your table and the "echo" line needs to be before the while loop as well.

/edit - as pointed out already
brookside
Forum Commoner
Posts: 30
Joined: Tue Mar 02, 2004 8:15 pm

Post by brookside »

no, because I am building a form......from the query
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

That's the point, you're building 'a' form. With the whole form inside the while loop you'll end up with a form for every row returned from the query.
brookside
Forum Commoner
Posts: 30
Joined: Tue Mar 02, 2004 8:15 pm

Post by brookside »

Sorry markI1999, you are right I get more than one form. But when I do what you said and this prints out on my form and it doesn't query


while($rows = mysql_fetch_array($result)) {
}
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

your question there confused me... can u restate it in a more clear format?
Post Reply