Jump to content

loop through SQL results to print an HTML table


jasonxxx102

Recommended Posts

Ok, so I have a main page that grabs data from a MySQL db and posts it into a dropdown menu

 

see here:

$sql = "SELECT * FROM 4000Series";
$result = $con->query($sql);

print "Select your GPU: <select name='gpuId'>";
while ($row = $result->fetch_assoc())
{
    print "<option value='" .$row["id"] . "'>" . $row["gpu"] . "</option>";
}
print "</select> </br>\n";

This works fine, from here I'm passing the POST data from the option values to a new page (basically the option values correspond directly to the "id" value in my SQL db).

 

The values are passed onto the new page fine, but when I try to setup an SQL select statement to grab the row with the corresponding "id" value and print it out with a while loop I'm shooting a blank.

 

Here's the code for the second page

$id = $_POST['gpuId'];

**Connect to sql db here**

$sql = "SELECT * FROM 4000Series WHERE $id ='id'";
$result = $con->query($sql);

echo "<table>"; 

while ($row = $result->fetch_assoc()){   
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['gpu'] . "</td></tr>";  
}

echo "</table>";

When I view the source it prints out the table tags but nothing in the while loop, any ideas?

you have a $ in front of your id column name in your query, you are also missing the $ in front of the $id variable name in your query OR you could remove the single-quotes from around the id column name in your query.

 

if you include a debugging step where you echo out your sql query statement, you would have likely seen this problem.

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.