Page 1 of 2

Problems with populating forms with queried information

Posted: Tue Mar 09, 2004 6:34 pm
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.

Posted: Tue Mar 09, 2004 6:43 pm
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!

Posted: Tue Mar 09, 2004 9:12 pm
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

Posted: Wed Mar 10, 2004 8:16 pm
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

Posted: Wed Mar 10, 2004 8:21 pm
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
?>
????

Posted: Wed Mar 10, 2004 8:30 pm
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

Posted: Wed Mar 10, 2004 8:33 pm
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?

Posted: Wed Mar 10, 2004 8:36 pm
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>

Posted: Wed Mar 10, 2004 8:39 pm
by tim
edit:
</useless>

Posted: Wed Mar 10, 2004 8:40 pm
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.

Posted: Wed Mar 10, 2004 8:43 pm
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

Posted: Wed Mar 10, 2004 9:00 pm
by brookside
no, because I am building a form......from the query

Posted: Wed Mar 10, 2004 9:02 pm
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.

Posted: Wed Mar 10, 2004 9:15 pm
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)) {
}

Posted: Wed Mar 10, 2004 9:56 pm
by Illusionist
your question there confused me... can u restate it in a more clear format?