BeingNickB Posted August 9, 2007 Share Posted August 9, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/64105-solved-populating-a-drop-down-from-a-mysql-table/ Share on other sites More sharing options...
lemmin Posted August 9, 2007 Share Posted August 9, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/64105-solved-populating-a-drop-down-from-a-mysql-table/#findComment-319486 Share on other sites More sharing options...
BeingNickB Posted August 9, 2007 Author Share Posted August 9, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/64105-solved-populating-a-drop-down-from-a-mysql-table/#findComment-319571 Share on other sites More sharing options...
lemmin Posted August 9, 2007 Share Posted August 9, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/64105-solved-populating-a-drop-down-from-a-mysql-table/#findComment-319579 Share on other sites More sharing options...
Psycho Posted August 9, 2007 Share Posted August 9, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/64105-solved-populating-a-drop-down-from-a-mysql-table/#findComment-319580 Share on other sites More sharing options...
BeingNickB Posted August 9, 2007 Author Share Posted August 9, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/64105-solved-populating-a-drop-down-from-a-mysql-table/#findComment-319617 Share on other sites More sharing options...
lemmin Posted August 9, 2007 Share Posted August 9, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/64105-solved-populating-a-drop-down-from-a-mysql-table/#findComment-319671 Share on other sites More sharing options...
BeingNickB Posted August 9, 2007 Author Share Posted August 9, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/64105-solved-populating-a-drop-down-from-a-mysql-table/#findComment-319703 Share on other sites More sharing options...
plutomed Posted August 9, 2007 Share Posted August 9, 2007 You would have to change feildname to name seeing as that is the only feild you are getting from the db Quote Link to comment https://forums.phpfreaks.com/topic/64105-solved-populating-a-drop-down-from-a-mysql-table/#findComment-319715 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.