Forum Moderators: coopster

Message Too Old, No Replies

Prefilling a form

Calling SQL rows into a form

         

shaundunne

12:43 pm on Jan 14, 2008 (gmt 0)

10+ Year Member



I have a database with two row

ID and Name

I want to call these into a HTML form drop down select menu, how would i go about this?

my sql query would be

select id, name from db;

this would list several rows which i would like each to be selectable in the HTML form.

<form action="something.php" method="post">
<select name="ID select">
<option>ROW1
<option>ROW2
<option>ROW3
<option>ROW4
<option>ROW5
<option>ROW6
<option>ROW7
</select>

I want this so that if a new id is added to the db, then a new option is created automatically, also the same if one is taken away, the option is removed.

Cheers

venelin13

12:59 pm on Jan 14, 2008 (gmt 0)

10+ Year Member



The php code should looks like:

$sql = "select id, name from table order by name";
$resultSet = mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($resultSet) > 0)
{
while($data = mysql_fetch_row($resultSet)){
$html_options .= "<option value=\"".$data['id'].""\">".$data['name']."</option>";
}
}

and the HTML should looks like:

<select name="Id">
<?=$html_options?>
</select>

shaundunne

4:27 pm on Jan 14, 2008 (gmt 0)

10+ Year Member



<?php
/*--------- DATABASE CONNECTION INFO---------*/
function connect_mrsql()
{
$dbhost="connection";
$dbuser = "user";
$dbpass = "password";
$db ="database";
$link = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
return $link;
}
//connect to mrsql
$connection = connect_mrsql();
mysql_select_db("lead");
//sql query
$sql = "select id, name from database";
$resultSet = mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($resultSet) > 0)
{
while($data = mysql_fetch_row($resultSet)){
$html_options .= "<option value=\"".$data['id'].""\">".$data['name']."</option>";
}
}
?>

<form action="winner.php" method="get">
<select name="Id">
<?=$html_options?>
</select>
</form>

After hosting this, the page is just blank, where am i going wrong?

Cheers

shaundunne

5:15 pm on Jan 14, 2008 (gmt 0)

10+ Year Member



it cool, i did this

<form action="winner.php" method="get">
<select name="comp">
<?
if (mysql_num_rows($resultSet) > 0){
while($row = mysql_fetch_array($resultSet))
{
?><option value ="<?=$row['id'];?>"><?=$row['name'];?> </option>
<?
}
}
?>
</select>
</form>

and it works

shaundunne

2:18 pm on Jan 15, 2008 (gmt 0)

10+ Year Member



OK, now i am at the second hurdle. I want to now pull a few rows from my database depending on the selection made from the drop down menu...

The SQL i want to run on submit is

select competition_entry.competition_id, user.user_id, email_address,
first_name, last_name, gender, date_of_birth, telephone_number,
address1, address2, address3, town, county, postcode
from competition_entry, user
WHERE competition_entry.competition_id ='#*$!#*$!'
ORDER BY RAND() limit 1\G

The #*$!#*$! is replaced by the string selected in the drop down menu i made

<option value ="<?=$row['id'];?>"><?=$row['name'];?> </option>

so .competion_id should be $row['id']?

I then want to echo the result. If submit is pushed again, i want the script to run again.

cheers for any help.

coopster

12:26 am on Jan 16, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You retrieve that value from your posted <form> data. PHP populates certain superglobals [php.net] based on the method (see the <form method=""> attribute) you used when submitting the form.