PHP - Multiple Drop Down Lists Via Php Mysql
Hello Friends!.... here is the big idea. I am trying to make a form in which there will be 2 drop down lists which will be populating directly from MySQL DB ....
Actually i am developing a student management system as my first PHP project... here i want to have 2 drop down lists first is roll number and second is student name. i want that when some one select the roll number in the first drop down the student name against it is automatically populated in the next drop down. please help me friends i am new to php and dont know soo much.! any help PLZZZZZZZZZZ Similar TutorialsHi I have tried the mysql forum but have had no joy with an answer to my problem so wondered if php would be better. I want my users to be able to select from 5 different drop down lists where they can chose any combination from 1 up to all 5, I have attached the front end. These lists are being populated from mysql tables. Code for the drop down lists is as follows Code: [Select] <form action="horse-events-devon.php?url_countyid=<?php echo urlencode ($url_countyid ['url_countyid']) ; ?>&go" method="POST"> <table id="searchtable"> <tr> <th>Find By Discipline</th> <th>Find By Venue</th> <th>Find By Championship</th> <th>Find By Organiser</th> <th>Equine Association</th> <th>Submit Your Selections</th> </tr> <tr> <td><select name="dis_id"> <?php $upcomingdis = upcomingdis($url_countyid); $upcoming_dis_bycounty = mysql_fetch_assoc ($upcomingdis); ?> <?php do { ?> <option value="<?php echo $upcoming_dis_bycounty ['dis_id']; ?>" > <?php echo $upcoming_dis_bycounty ['dis_description']; ?></option> <?php } while ($upcoming_dis_bycounty = mysql_fetch_assoc ($upcomingdis)); ?></select></td> <td><select name="ven_id"> <?php $upvenbycounty_set = upcoming_venevents_bycounty($url_countyid); $upcoming_ven_bycounty = mysql_fetch_assoc ($upvenbycounty_set); ?> <?php do { ?> <option value="<?php echo $upcoming_ven_bycounty ['ven_id']; ?>" > <?php echo $upcoming_ven_bycounty ['ven_name']; ?></option> <?php } while ($upcoming_ven_bycounty = mysql_fetch_assoc ($upvenbycounty_set)); ?></select></td> <td><select name="champ_id"> <?php $championship_set = findchampionships(); $champlist = mysql_fetch_assoc ($championship_set); ?> <?php do { ?> <option value="<?php echo $champlist ['champ_id']; ?>" > <?php echo $champlist ['champ_description']; ?></option> <?php } while ($champlist = mysql_fetch_assoc ($championship_set)); ?></select></td> <td> <select name="org_id"> <?php $uporgbycounty_set = upcoming_organevents_bycounty($url_countyid); $upcoming_org_bycounty = mysql_fetch_assoc ($uporgbycounty_set); ?> <?php do { ?> <option value="<?php echo $upcoming_org_bycounty ['org_id']; ?>" ><?php echo $upcoming_org_bycounty ['org_name']; ?></option> <?php } while ($upcoming_org_bycounty = mysql_fetch_assoc ($uporgbycounty_set)); ?></select> </td> <td><select name="ass_id"> <?php $upassbycounty_set = upcoming_assevents_bycounty($url_countyid); $upcoming_assbycounty = mysql_fetch_assoc ($upassbycounty_set); ?> <?php do { ?> <option value="<?php echo $upcoming_assbycounty ['ass_id']; ?>" > <?php echo $upcoming_assbycounty ['ass_description']; ?></option> <?php } while ($upcoming_assbycounty = mysql_fetch_assoc ($upassbycounty_set)); ?></select></td> <td><input name="submit" type="submit" /><input name="countyid" type="hidden" value="<?php echo $url_countyid ['url_countyid']; ?>" /></td> </tr> </table> </form> My search processing is as follows Code: [Select] <?php if (isset($_POST['submit'])){ if (isset($_GET['go'])){ $countyid = $_POST['countyid']; $ven_id = $_POST['ven_id']; $dis_id = $_POST['dis_id']; $champ_id = $_POST['champ_id']; $org_id = $_POST['org_id']; $event_id = $row['event_id']; $sql = "SELECT DATE_FORMAT (events.startdate, '%a, %d, %b') AS stdate, events.event_id, events.title, events.ven_id, events.org_id, venue.county_id, venue.ven_id, eventdisciplines.event_id, eventdisciplines.dis_id, county.county_id, discipline.dis_id \n" . "FROM events \n" . "LEFT OUTER JOIN eventdisciplines \n" . "ON events.event_id = eventdisciplines.event_id \n" . "LEFT OUTER JOIN discipline \n" . "ON eventdisciplines.dis_id = discipline.dis_id \n" . "LEFT OUTER JOIN venue \n" . "ON events.ven_id = venue.ven_id \n" . "LEFT OUTER JOIN county \n" . "ON venue.county_id = county.county_id \n" . "WHERE events.ven_id = ({$ven_id} OR events.org_id = {$org_id})\n" . "AND events.startdate > NOW()\n" . "AND venue.county_id = {$countyid} \n" . "ORDER BY startdate ASC"; $result = mysql_query ($sql, $connection); ?> How am I best to do this please? my OR within the mysql does not work, should I not be doing this with php in the search processing? someones help would really be appreciated, just to point me in the right direction. [attachment deleted by admin] Hi, A quick question please , I am trying to assign a selected index to a drop down list based on data retrieved from the database. I tried using JavaScript : document.getElementById('Country').selectedIndex ='".$array['Country']."'; but it does work. Any suggestion? Hi, I need help to create a list like this with data from a database im having trouble doing so i have a table called company with 5 fields (installer, assessor, provider, trainer, other) if for example installer in the filed list = 1 then add to list else dont and see if assessor needs added if = 1 add then close list and see if provider = 1 if it does'tn then ask next. <ul> <li>Item 1</li> <li>Item 2</li> </ul> <ul> <li>Item 3</li> <li>Item 4</li> </ul> Ok, so i got this website project dropped in my lap, and I'm stuck not really having done much php coding ever. I have a database which is linked already, and it was working when pulling data from a specific table. The problem arose when I was asked to make it work with other tables. I have a table with the table names in it, and the drop down field to select the table populates. The next field is a multiple select field that is supposed to populate report dates from the previously selected table, and then finally the last multiple select field is supposed to populate with names that match the dates selected from the previous field. The last one I can live without, as the existing code takes into account if the name selected doesn't exist in the table with the selected date, but at the moment the date field just comes up blank. Any help would be greatly appreciated. This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=310879.0 hello. i have an issue where the data stored with an image is not saving to a mysql table. the image data is ok, just not the selections from the dropdown lists. here is the code <?php include ('connect.php'); // Insert any new image into database if(isset($_POST['xsubmit']) && $_FILES['imagefile']['name'] != "") { $fileName = $_FILES['imagefile']['name']; $fileSize = $_FILES['imagefile']['size']; $fileType = $_FILES['imagefile']['type']; $content = addslashes (file_get_contents($_FILES['imagefile']['tmp_name'])); $jeweltype = $_POST['jeweltype']; $jewelsize = $_POST['jewelsize_in']; $jewelcolour = $_POST['jewelcolour_in']; $jewelmaterial = $_POST['jewelmaterial_in']; $jewelgender = $_POST['jewelgender_in']; if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } // Checking file size if ($fileSize < 150000) { mysql_query ("INSERT into jewel_images (name,size,type,content,jeweltype,jewelsize,jewelcolour,jewelmaterial,jewelgender) " . "values ('$fileName','$fileSize','$fileType','$content','$jeweltype','$jewelsize','$jewelcolour','$jewelmaterial','$jewelgender')"); } else { $err = "The Image file to too large!"; } } // Find out about latest image $gotten = mysql_query("select * from jewel_images order by row_id desc"); $row = mysql_fetch_assoc($gotten); $bytes = $row['content']; // If this is the image request, send out the image if ($_REQUEST['pic'] == 1) { header("Content-type: $row[type];"); print $bytes; } ?> <html> <head> <title>Upload an image to a database</title> </head> <body> <font color="#FF3333"><?php echo $err ?></font> <table> <form name="Upload" enctype="multipart/form-data" method="post"> <tr> <td>Upload <input type="file" name="imagefile"><br /> Jewelery Type: <select> <?php $sql="SELECT jeweltype FROM jeweltypes"; $result =mysql_query($sql); while ($data=mysql_fetch_assoc($result)){ ?> <option value ="jeweltype" ><?php echo $data['jeweltype'] ?></option> <?php } ?> </select> <br /> Jewelery Size: <select> <?php $sql="SELECT jewelsize FROM jewelsizes"; $result =mysql_query($sql); while ($data=mysql_fetch_assoc($result)){ ?> <option value ="jewelsize" ><?php echo $data['jewelsize'] ?></option> <?php } ?> </select> <br /> Jewelery Colour: <select> <?php $sql="SELECT jewelcolour FROM jewelcolours"; $result =mysql_query($sql); while ($data=mysql_fetch_assoc($result)){ ?> <option value ="jewelcolour_in" ><?php echo $data['jewelcolour'] ?></option> <?php } ?> </select> <br /> Jewelery Material: <select> <?php $sql="SELECT jewelmaterial FROM jewelmaterials"; $result =mysql_query($sql); while ($data=mysql_fetch_assoc($result)){ ?> <option value ="jewelmaterial_in" ><?php echo $data['jewelmaterial'] ?></option> <?php } ?> </select> <br /> Jewelery Gender: <select> <?php $sql="SELECT jewelgender FROM jewelgenders"; $result =mysql_query($sql); while ($data=mysql_fetch_assoc($result)){ ?> <option value ="jewelgender_in" ><?php echo $data['jewelgender'] ?></option> <?php } ?> </select> <br /> <input type="submit" name="xsubmit" value="Upload"> </td> </tr> <tr> <td>Latest Image</td> </tr> <tr> <td><img src="?pic=1"></td> </tr> </form> </table> </body> </html> ============================================== here is the query =============================================== <html> <head><title>Your Page Title</title></head> <body> <?php $database="josh_jewel"; mysql_connect ("localhost", "xxxxxxxxx", "yyyyyyyyyyyy"); @mysql_select_db($database) or die( "Unable to select database"); $result = mysql_query( "SELECT jewelcolour FROM jewel_images" ) or die("SELECT Error: ".mysql_error()); $num_rows = mysql_num_rows($result); print "There are $num_rows records.<P>"; print "<table width=400 border=1>\n"; while ($get_info = mysql_fetch_row($result)){ print "<tr>\n"; foreach ($get_info as $field) print "\t<td><font face=arial size=1/>$field</font></td>\n"; print "</tr>\n"; } print "</table>\n"; ?> </body> </html> Hello to all. I'm new here and i am very glad to be a part of this community. I am new in PHP/mysql but i try to learn as much as i can in php & mysql. And here is the problem: Into a php or html page i want to display 2 dropdown list to act as a filter for displaying a table. I managed to display a table based on option select from first dropdown but i don'y know how to make or activate the second dropdown list to act as a filter, and when i press "result" to display on the same page the results. Thanks a lot. Ok I'm trying to insert multiple rows by using a while loop but having problems. At the same time, need to open a new mysql connection while running the insert query, close it then open the previous mysql connection. I managed to insert multiple queries before using a loop, but for this time, the loop does not work? I think it is because I am opening another connection... yh that would make sense actually? Here is the code: $users = safe_query("SELECT * FROM ".PREFIX."user"); while($dp=mysql_fetch_array($users)) { $username = $dp['username']; $nickname = $dp['nickname']; $pwd1 = $dp['password']; $mail = $dp['email']; $ip_add = $dp['ip']; $wsID = $dp['userID']; $registerdate = $dp['registerdate']; $birthday = $dp['birthday']; $avatar = $dp['avatar']; $icq = $dp['icq']; $hp = $dp['homepage']; echo $username." = 1 username only? :("; // ----- Forum Bridge user insert ----- $result = safe_query("SELECT * FROM `".PREFIX."forum`"); $ds=mysql_fetch_array($result); $forum_prefix = $ds['prefix']; define(PREFIX_FORUM, $forum_prefix); define(FORUMREG_DEBUG, 0); $con = mysql_connect($ds['host'], $ds['user'], $ds['password']) or system_error('ERROR: Can not connect to MySQL-Server'); $condb = mysql_select_db($ds['db'], $con) or system_error('ERROR: Can not connect to database "'.$ds['db'].'"'); include('../_phpbb_func.php'); $phpbbpass = phpbb_hash($pwd1); $phpbbmailhash = phpbb_email_hash($mail); $phpbbsalt = unique_id(); safe_query("INSERT INTO `".PREFIX_FORUM."users` (`username`, `username_clean`, `user_password`, `user_pass_convert`, `user_email`, `user_email_hash`, `group_id`, `user_type`, `user_regdate`, `user_passchg`, `user_lastvisit`, `user_lastmark`, `user_new`, `user_options`, `user_form_salt`, `user_ip`, `wsID`, `user_birthday`, `user_avatar`, `user_icq`, `user_website`) VALUES ('$username', '$username', '$phpbbpass', '0', '$mail', '$phpbbmailhash', '2', '0', '$registerdate', '$registerdate', '$registerdate', '$registerdate', '1', '230271', '$phpbbsalt', '$ip_add', '$wsID', '$birthday', '$avatar', '$icq', '$hp')"); if (FORUMREG_DEBUG == '1') { echo "<p><b>-- DEBUG -- : User added: ".mysql_affected_rows($con)."<br />"; echo "<br />-- DEBUG -- : Query used: ".end($_mysql_querys)."</b></p><br />"; $result = safe_query("SELECT user_id from ".PREFIX_FORUM."users WHERE username = '$username'"); $phpbbid = mysql_fetch_row($result); safe_query("INSERT INTO `".PREFIX_FORUM."user_group` (`group_id`, `user_id`, `group_leader`, `user_pending`) VALUES ('2', '$phpbbid[0]', '0', '0')"); safe_query("INSERT INTO `".PREFIX_FORUM."user_group` (`group_id`, `user_id`, `group_leader`, `user_pending`) VALUES ('7', '$phpbbid[0]', '0', '0')"); mysql_close($con); } include('../_mysql.php'); mysql_connect($host, $user, $pwd) or system_error('ERROR: Can not connect to MySQL-Server'); mysql_select_db($db) or system_error('ERROR: Can not connect to database "'.$db.'"'); } So I need to be able to insert these rows using the while loop.. how can I do this? I really appreciate any help. hii,everyone... i am new to the forum ..so forgive me if i make some errors in describing my problem i want to create a drop down menu...the values of which i want to retrieve from a table i have created in my database... i have been able to acheive this...but i want a second drop down menu...in which i want the values to get filtered according to the selection of the first drop down menu... i hav a table with the name test...which i am connecting to the first drop down menu i hav created another table with the name testtype...for second menu.. the id of first table is the testid of second... plzz help me with creating the second menu... my code is Code: [Select] <?php $sql_result = "select test_series FROM test"; $result = mysql_query($sql_result); ?> <td>Select Test Series </td> <td> <select name="testseries" id="testseries" )"> <option>Select</option> <?php if(mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)) { $test = $row['test_series'];?> <option><?php echo $test ?></option> <?php } } else {?> <option>No Names Present</option>"; <?php } ?> </select> </td> </tr> <tr> <?php $testseries = $_POST['testseries']; $query = "select id from test where test_series = '$testseries'"; $resultt = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($resultt); $id = $row['id']; $sql_result = "select test_name FROM testtype where testid = $id"; $result = mysql_query($sql_result); ?> <td>Select Test Name </td> <td> <select name="testtype"> <option>Select</option> <?php if(mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)) { $test2 = $row['test_name'];?> <option><?php echo $test2 ?></option> <?php } } else {?> <option>No Names Present</option>"; <?php } ?> </select> </td> </tr> IN THIS CODE I AM GETTIN AN ERROR Undefined index: testseries in ..addques.php on line 155 WHICH IS THIS ONE $testseries = $_POST['testseries']; I am using the following code to generate a dynamic drop down menu from a column in table1(name) - and then inserting the selected option into table2. What I am trying to do is, select data from 2 columns from table1(name,age) and then store and insert both of those values into table2 I am hoping someone can help me out or at least point me in the rigth direction. ..I would like to insert into table 2 the age that matches the person selected by the drop down menu from table1. For example - if from the drop down menu I choose John - when the form is sumbitted i want to insert into table2 John's name and age (based on what is stored in table1, in this case John,22) ...Any ideas??? table1 id|name|age|score 01-john-22-1547 02-jane-22-1245 table2 id|county|name|age the dynamic drop down is generated from table1 (name) with following code Code: [Select] <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table width="100%" border="0" cellpadding="5" cellspacing="0"> <?php require_once('sql.php'); $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $query="SELECT name FROM table1 ORDER BY name ASC"; $result = mysqli_query ($dbc,$query) or die(mysqli_error()); $dropdown = "<select name='name'>"; while($row = mysqli_fetch_array($result)) { $dropdown .= "\r\n<option value='{$row['name']}'>{$row['name']}</option>"; } $dropdown .= "\r\n</select>"; echo $dropdown; ?> <tr> <td height="50" align="right"> <label for="scheduled_time">Country</label><br/> </td> <td align="left"> <input type="text" id="country" name="country" class="input" maxlength="30"" /> </td> </table> <p></p> <input type="submit" value="Add Info" name="submit" /> </form> I am inserting into table2 with the following code Code: [Select] <?php $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); if (isset($_POST['submit'])) { $county = mysqli_real_escape_string($dbc, trim($_POST['country'])); $name = mysqli_real_escape_string($dbc, trim($_POST['name'])); $query = "INSERT INTO table2 (country, name) VALUES ('$country', '$name'')"; $result = mysqli_query($dbc, $query); if (!$result) { printf("query error : <br/> %s\n", mysqli_error($dbc)); } if ($result) { echo 'Success'; } // close dbc mysqli_close($dbc); exit(); } ?> I have attempted to get a php reporting page built for our off site managers to run reports. Because of the number of employees I think it ouwld be best to sort by foreman, then be able to pick from the resulting drop down list of current employees under that foreman.
My issue I think really comes from having the first drop down be dynamic in that you would see a list of foremen which comes from a query of the main employees table and then picks up only active employees and their foreman. The tutorials I have seen all reference an 'id" field but since I am pulling the info for the drop down from a query, should I have some sort of rowID? If so how? The tutorial I have found checks to see if you pick a number which I seem to have gotten around but when I do the query and look for $GF (general foreman) then the query comes back empty (this is bolded and underlined below). Should this be some sort of rowID for the general foreman drop down?
Below is what I have been able to sort-of get working (adding in the URL handlers I think will be useful for people in the future to be able to bookmark it/send out emails with links already in it etc)
Here is the tutorial http://www.plus2net....p_down_list.php
Not sure if i understand why he used the $nocitia tags either
I know Im close on this but am starting to go crosseyed! Thanks for pointing me in the right direction.
<?php require 'config.php'; // Database connection //////// End of connecting to database //////// ?> <!doctype html public "-//w3c//dtd html 3.2//en"> <html> <head> <title>Multiple drop down list box</title> <SCRIPT language=JavaScript> function reload(form) { var val=form.GF.options[form.GF.options.selectedIndex].value; var val2=form.EMPLOYEE_ID.options[form.EMPLOYEE_ID.options.selectedIndex].value; var val3=form.SEPARATED.options[form.SEPARATED.options.selectedIndex].value; self.location='dd3.php?GF=' + val + '&EMPLOYEE_ID=' + val2 + '&SEPARATED=' + val3 ; } </script> </head> <body> <?Php @$GF=$_GET['GF']; // Use this line or below line if register_global is off @$EMPLOYEE_ID=$_GET['EMPLOYEE_ID']; // Use this line or below line if register_global is off @$SEPARATED=$_GET['SEPARATED']; // Use this line or below line if register_global is off ///////// Getting the data from Mysql table for first list box////////// $quer2="SELECT tbl_Employee_Master.Current_GF as GF FROM tbl_Employee_Master GROUP BY tbl_Employee_Master.Current_GF, tbl_Employee_Master.Separated HAVING (((tbl_Employee_Master.Separated)=0))"; ///////////// End of query for first list box//////////// /////// for second drop down list we will check if category is selected else we will display all the subcategory///// if(strlen($GF) > 0){ $quer="SELECT Employee_ID tbl_Employee_Master WHERE Current_GF=$GF"; }else{$quer="SELECT DISTINCT Employee_ID, Last_Name, First_Name FROM tbl_Employee_Master order by Last_Name"; } ////////// end of query for second subcategory drop down list box /////////////////////////// echo "<form method=post name=f1 action='dd-check.php'>"; /// Add your form processing page address to action in above line. Example action=dd-check.php//// ////////// Starting of first drop downlist ///////// echo "<select name='GF' onchange=\"reload(this.form)\"><option value=''>Select one</option>"; foreach ($dbo->query($quer2) as $noticia2) { if($noticia2['GF']==@$GF){echo "<option selected value='$noticia2[GF]'>$noticia2[GF]</option>"."<BR>";} else{echo "<option value='$noticia2[GF]'>$noticia2[GF]</option>";} } echo "</select>"; ////////////////// This will end the first drop down list /////////// ////////// Starting of second drop downlist ///////// echo "<select name='EMPLOYEE_ID'><option value=''>Select one</option>"; foreach ($dbo->query($quer) as $noticia) { echo "<option value='$noticia[Employee_ID]'>$noticia[Employee_ID] $noticia[Last_Name] $noticia[First_Name]</option>"; } echo "</select>"; ////////////////// This will end the second drop down list /////////// //// Add your other form fields as needed here///// echo "<input type=submit value=Submit>"; echo "</form>"; ?> <br><br> <a href=dd.php>Reset and start again</a> <br><br> <center><a href='http://www.plus2net.com' rel="nofollow">PHP SQL HTML free tutorials and scripts</a></center> </body> </html> hey guys just a quick one i want a drop down box which is populated from a mysql query which is eay enough but also at the same time i also want the dropdown box to look at the current record and have the value in the drop down box e.g. the drop down has values 1, 2, 3, 4, 5 in it which are pulled from a table called values the current record is from a table called staff and it has an id of 45 and a user level of 3 i want the drop down to have all the values selectable to update the record but on load it will have 3 as the current, i want to be able to chage this 3 to a 5 and update the record how can i do this.? just a brief code example would be awsome and ill adopt it to fit Hey all - I (think) that I'm following the code exactly, but no luck... Essentially I want to populate a drop down menu from text stored in mysql (varchar). Here is my code (and where I'm pulling the code found below). Any ideas what's going wrong. (My error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in....). $sql = "SELECT * FROM table"; $results = mysql_query($sql); //or die("Query failed with error: ".mysql_error()); // execute the query $options=""; while ($row=mysql_fetch_array($results)){ $id=$row["id"]; $thing=$row["thing"]; $options.="<OPTION VALUE=\"$id\">".$thing; } <select name="id" style="font-size:20px;font-family:Arial;width:275px"> <option value=0>Choose<? echo $options?></option> </select> Here is where I'm getting my code sample: http://forums.devarticles.com/mysql-development-50/drop-down-menu-populated-from-a-mysql-database-1811.html Thank for any advice!! I got some help here about 6 months ago with drop down menus & I need a little more. I'm working on a form that has about 20 drop down menus, each populated from a mysql database table with about 50 entries each. I need to keep the selected menu option on the form after the form is submitted but each time the form is submitted the selected menu option reverts back to the first item in the database table. Is there any practical way to fix this problem other than using javascript to finish up? Hi, At present the PHP page reads the MySQL data and prints on screen and if the result if 'FREE' it shows a clickable image for the user to book. I am looking for something that allows the user to drag the result they fund to another location with in the table. I.e. user has box 1 filled with their name, they can drag their name to box 9 and it saves it, so they can go back on the it will be their. If anyone knows where I can firn maybe a tutorial, or guide to get started with this, as I have looked and cannot see anything. Cheers. Stu I'm a newbie on php. I'm really a system administrator and I was just task to do this simple task. For me its hard but I'm sure for a programmer this is very simple. My agenda is to pull out data on one of my column in mysql, select it and dump it on mysql. Here is the php for retrieving mysql data Code: [Select] <?php function database_connect($users) { $resource_link = mysql_connect("localhost", "root", "root"); if (mysql_select_db($users, $resource_link)) { return $resource_link; } else { echo "Cannot connect to DB"; return false; } } function print_dropdown($query, $link){ $queried = mysql_query($query, $link); $menu = '<select username="username">'; while ($result = mysql_fetch_array($queried)) { $menu .= ' <option value="' . $result['id'] . '">' . $result['username'] . '</option>'; } $menu .= '</select>'; return $menu; } //Some other form elements, or just start a form. echo '<form method="post" action="create2.php">'; //The important bit echo print_dropdown("SELECT username FROM mailbox;", database_connect("users")); //Some other form elements, or just end the form. echo '<input type="submit" name="submit" value="submit"/></form>'; Here is the content of my create2.php. This is the php page who do the insert on my mysql. Code: [Select] <?php // open the connection $conn = mysql_connect("localhost", "root", "root"); // pick the database to use mysql_select_db("users",$conn); // create the SQL statement $sql2 = "INSERT INTO mailbox values ('','locked','','$_POST[username]','',NOW(),'','locked','')"; // for troubleshooting $result = mysql_query($sql2, $conn) or die(mysql_error()); // execute the SQL statement //if (mysql_query($sql2, $conn)) { // echo "Success"; //} else { // echo "Fail"; //} } ?> When I click the submit button, I don't see any record being inserted on my table. I'm using the create2.php on my other page though it is only an insert/fill up form not like this one that I need to pull up the date, select and insert to mysql. hey all so I have this bit down: Code: [Select] $query="SELECT `2010 Region Code` AS codes FROM locations"; $results = mysql_query($query); $options=""; $options = "<select location='codes'>"; while($nt=mysql_fetch_assoc($results)) { $thing=$nt["codes"]; $options.="\r\n<option value ='{$nt['codes']}'> {$nt['codes']}</option>"; } $options .="\r\n</select>"; echo $options; what I'm trying to do is grab the selection from the drop down and display it as a table (the sql query would be extended should we manage to figure this one out I've tried Code: [Select] echo"<form name='LOCATIONS' action='".$_SERVER['PHP_SELF']."' target='iframe' method='post'>"; any ideas? I've got a HTML drop down box as similar to this: <select name="dropdown"> <option>Option 1</option> <option>Option 2</option> <option>Option 3</option> </select> If I run a mysql query and get a result of "Option 3", is there anyway using PHP to give Option 3 the selected value? |