jamiemosteller Posted March 26, 2014 Share Posted March 26, 2014 (edited) Below is the code I'm trying to do to create a drop down of schools, that are in my db. <html> <form action="login.php" method="post"> <TABLE> <TR> <TH>School </TH> </TR> <TD> <select name="School"><option value="">-- School Name --</option> <?php include("connect.php"); include("utility.php"); connectDB(); $result = getSchools(); while ($row = mysql_fetch_array($result, MYSQL_NUM)) { foreach ($row as $attribute) { echo "<option>"; echo $attribute; echo "</option>"; } } ?> </select> </TD> </TABLE> <input type="submit" /> </form> </html> Edited March 26, 2014 by jamiemosteller Quote Link to comment https://forums.phpfreaks.com/topic/287277-html-table-drop-down-with-mysql-data/ Share on other sites More sharing options...
Ansego Posted March 26, 2014 Share Posted March 26, 2014 Hi, If I understood the question correctly, you are almost there, echo "<option>"; echo $attribute; echo "</option>"; Change to something like this (Mock Code): echo "<option value='$attribute' selected='selected'>$attribute</option>" Quote Link to comment https://forums.phpfreaks.com/topic/287277-html-table-drop-down-with-mysql-data/#findComment-1473897 Share on other sites More sharing options...
jamiemosteller Posted March 26, 2014 Author Share Posted March 26, 2014 Ansego, did you purposely leave out the semi-colon at the end? I've tried this both ways and still no luck Quote Link to comment https://forums.phpfreaks.com/topic/287277-html-table-drop-down-with-mysql-data/#findComment-1473901 Share on other sites More sharing options...
Ansego Posted March 26, 2014 Share Posted March 26, 2014 Yeah my bad with the semi-colon sorry. You get data I am assuming? from: echo $attribute; (Made sure of semi-colon this time ;-) lol); What happens what you try my version? Any output? Quote Link to comment https://forums.phpfreaks.com/topic/287277-html-table-drop-down-with-mysql-data/#findComment-1473907 Share on other sites More sharing options...
jamiemosteller Posted March 26, 2014 Author Share Posted March 26, 2014 I don't get any output period. even when I simplify it to just be echo $attribute; as your suggested. I tried making a simple php file with just the below and that works as expected, so I think the db stuff is all good <?php include("connect.php"); include("utility.php"); connectDB(); $result = getSchools(); while ($row = mysql_fetch_array($result, MYSQL_NUM)) { foreach ($row as $attribute) { echo $attribute; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/287277-html-table-drop-down-with-mysql-data/#findComment-1473911 Share on other sites More sharing options...
Ansego Posted March 26, 2014 Share Posted March 26, 2014 Need to show what's going on in connect.php, utility.php; (Keep in mind security before pasting. Quote Link to comment https://forums.phpfreaks.com/topic/287277-html-table-drop-down-with-mysql-data/#findComment-1473916 Share on other sites More sharing options...
jamiemosteller Posted March 26, 2014 Author Share Posted March 26, 2014 Here is connect.php with the db info blanked out <?php function connectDB() { $hostname='***************'; $username='***********'; $password='**************'; $dbname='************'; mysql_connect($hostname, $username, $password) OR DIE('Unable to connect to database! Please try again later.'); mysql_select_db($dbname); } ?> And utility.php <?php function showerror() { die("Error " . mysql_errno() . " : " . mysql_error()); } function insertAthlete($Type,$WeightClass,$LastName,$FirstName,$GradYear,$Wins,$Losses,$School) { mysql_query("INSERT INTO athlete VALUES (DEFAULT,'$Type','$WeightClass','$LastName','$FirstName','$GradYear','$Wins','$Losses','$School')"); } function getAthletesAll() { $sql="SELECT * from athlete"; $resultAthletes = mysql_query($sql); return $resultAthletes; } function getAthletes() { $sql="SELECT athleteType, weightClass, firstName, lastName, gradYear, wins, losses from athlete"; $resultAthletes = mysql_query($sql); return $resultAthletes; } function getAthleteTypes() { $sql="SELECT * from athleteType"; $resultAthleteTypes = mysql_query($sql); return $resultAthleteTypes; } function getSchools() { $sql="SELECT schoolName from school"; $resultSchools = mysql_query($sql); return $resultSchools; } function displayAthletes($resultAthletes) { // Start a table, with column headers print "<style> table,th,td { font-family:Arial; font-size:16; color:#000066; border:1px solid black; border-collapse:collapse; } </style>"; print "\n<table style=width:950px>\n<tr>\n" . "\n\t<th>Athlete ID</th>" . "\n\t<th>Athlete Type</th>" . "\n\t<th>Weight Class</th>" . "\n\t<th>First Name</th>" . "\n\t<th>Last Name</th>" . "\n\t<th>Grad Year</th>" . "\n\t<th>Wins</th>" . "\n\t<th>Losses</th>" . "\n\t<th>SchoolID</th>" . "\n</tr>"; // Until there are no rows in the result set, fetch a row into // the $row array and ... while ($row = @ mysql_fetch_row($resultAthletes)) { // ... start a TABLE row ... print "\n<tr>"; // ... and print out each of the attributes in that row as a separate TD (Table Data). foreach($row as $data) print "\n\t<td> {$data} </td>"; // Finish the row print "\n</tr>"; } // Then, finish the table print "\n</table>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/287277-html-table-drop-down-with-mysql-data/#findComment-1474027 Share on other sites More sharing options...
SitdGames Posted March 27, 2014 Share Posted March 27, 2014 How is showerror() being called? Are you doing any type of error capture? A simple easy way to capture mysql errors is: mysql_query("YOUR QUERY") OR die("Error:".mysql_error()); or mysql_query("YOUR QUERY") OR die(showerror()); (I would recommend the first one, but the second one might work...) But you should probably use mysqli. It's honestly not that bad, and I've found it saves a tonne of time in the long run. Hope this helps! Quote Link to comment https://forums.phpfreaks.com/topic/287277-html-table-drop-down-with-mysql-data/#findComment-1474056 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.