Jump to content

HTML table drop down with mySQL data


jamiemosteller

Recommended Posts

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>
Link to comment
https://forums.phpfreaks.com/topic/287277-html-table-drop-down-with-mysql-data/
Share on other sites

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;
}
}
?>

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";
}
 
?>

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!

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.