Jump to content

[SOLVED] Populating a Drop Down from a MySQL Table


BeingNickB

Recommended Posts

Hello.  I am new to the forums and a beginner with PHP. 

 

I have a page here: http://www.nickandteresa.net/guests/guestlist/rsvp.html.  What I want to do is have the Name drop down populate with values in the column "name" from the table "invite." 

 

When they click submit, I want the name dropped from "invite" and added to a table "guests" with their answer if they are coming and how many.  I know the SQL to make it happen, but I am pretty sketchy about the PHP.  So any help would be very much welcomed. 

 

Here is my form's code:

<form action="" method="post" name="frmGuest" id="frmGuest">
  <label><span class="style3">Guest Name: 
  <select name="name" id="name" tabindex="0">
  </select>
  </span></label>
  
  <span class="style3">
  <label>Will You Be Attending?
  <select name="attend" id="attend" tabindex="1">
    <option selected="selected">...</option>
    <option value="Yes">Yes</option>
    <option value="No">No</option>
  </select>
  </label>  
  <label>Number In Party:
  <select name="select" tabindex="2">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
  </select>
  </label>
  </span>
  <p class="style3">
    <label></label>
    <input type="submit" name="Submit" value="Submit" tabindex="3" />
  </p>
  <p class="style1"> </p>
</form>

 

Thanks!

between your select tags put a php block to iterate through the query.

  <select name="select" tabindex="2">
    <?php
    while (!$query->EOF)
    {
        echo "<option value=\"" . $query['fieldname'] . "\">" . $query['fieldname'] . "</option>
    }
    ?>
  </select>

Ok,  I see where the logic is, I think.  But I don't think the statement is closed correctly or something. 

 

If I run it as is, I get: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/nickandt/www/www/guests/guestlist/rsvp.php on line 43

 

Line 43 is where my <span class> tag starts for the "are you coming" drop down.

 

If I add another " to close all the of quotes, I get: Parse error: syntax error, unexpected '}', expecting ',' or ';' in /home/nickandt/www/www/guests/guestlist/rsvp.php on line 37

 

Line 37 is where the closing bracket of the PHP provided lemmin is.

Yeah sorry, it is missing quotes:

 

  <select name="select" tabindex="2">
    <?php
    while (!$query->EOF)
    {
        echo "<option value=\"" . $query['fieldname'] . "\">" . $query['fieldname'] . "</option>";
    }
    ?>
  </select>

 

That should do it.

You also need a semicolon at the end of that line. And since you are using double quotes I wouldn't break out of the quotes for the variables.

  <select name="select" tabindex="2">
   <?php

   $query = "SELECT name FROM invite ORDER BY name ASC";
   $result = mysql_query($query) or DIE (mysql_error());

   while ($option = mysql_fetch_array($result))
   {
       echo "<option value=\"$option['name']\">$option['name']</option>";
   }
   ?>
 </select> 

Ok, when I use lemmin's solution, my page comes up, but only the name drop down appears.  The page seems to load perpetually and when I can click on the drop down, all I see is a ton of Ss.  Eventually, my browser freezes and I have to kill it.  So I put a close database function and that solved that.  But the drop down still is not functioning.  Here:  http://www.nickandteresa.net/guests/guestlist/rsvp.php.

 

When I try mjdamato's solution, I get this:  Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/nickandt/www/www/guests/guestlist/rsvp.php on line 38

You need to select a database:

@mysql_select_db("dbName") or die(mysql_error() . "<br>");

 

Then change my while loop because mine was for a different type of database.

  <select name="select" tabindex="2">
    <?php
    while ($row = mysql_fetch_array($result))
    {
        echo "<option value=\"" . $row['fieldname'] . "\">" . $row['fieldname'] . "</option>";
    }
    ?>
  </select>

 

It is the format that mjdamato used.

Ok.  I have my other drop downs, but the name drop down still is not populating.  Here is the code I am using:

 

<form action="" method="post" name="frmGuest" id="frmGuest">
  <label><span class="style3">Guest Name: 
  <select name="name" tabindex="0">
    <?php

$dbhost = 'localhost:/tmp/mysql5.sock';
$dbuser = '********';
$dbpass = '********';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'nickandt_db';
@mysql_select_db($dbname);


    $query = "SELECT name FROM invite ORDER BY name ASC";
    $result = mysql_query($query) or DIE (mysql_error());

while ($row = mysql_fetch_array($result))
    {
        echo "<option value=\"" . $row['fieldname'] . "\">" . $row['fieldname'] . "</option>";
   	}

    ?>

  </select>
  </span></label>
  
  <span class="style3">
  <label>Will You Be Attending?
  <select name="attend" id="attend" tabindex="1">
    <option selected="selected">...</option>
    <option value="Yes">Yes</option>
    <option value="No">No</option>
  </select>
  </label>  
  <label>Number In Party:
  <select name="select" tabindex="2">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
  </select>
  </label>
  </span>
  <p class="style3">
    <label></label>
    <input type="submit" name="Submit" value="Submit" tabindex="3" />
  </p>
  <p class="style1"> </p>
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.