HTML table not working in php

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Jeggae
Forum Newbie
Posts: 10
Joined: Fri Oct 10, 2008 4:53 am

HTML table not working in php

Post by Jeggae »

Hi.
I'm new to mysql and php and learning. I'm trying to get a program working but for some reason the table isnt showing up in ‘showtopic.php’ when the program is run, except the header, the header seems okay. I've pasted the table as an html document and the table ran perfect, but as part of the php, does not seem to be running except the header. The ‘topiclist.php’ part of the program the table, which is very similar, runs fine.

I think the query in showtopic.php is not returning any results. Not sure if its because of the query in topiclist.php.

Any ideas why please? I’ve spent hours looking over it and trying to rectify it.
Thanks in advance

forum database

Code: Select all

create forum_topics(
 topic_id int not null primary key auto_increment,
 topic_title varchar (150),
 Topic_create_time datetime,
 topic_owner varchar (150)
 );
 
create table forum_posts(
 post_id int not null primary key auto_increment,
 topic_id int not null,
 post_text text,
 post_create_time datetime,
 post_owner varchar (150)
 );

showtopic.php:

Code: Select all

<?php 
//check for required info from the query string
if (!$_GET[topic_id]) {
    header("Location: topiclist.php");
}
 
//connect to server and select database
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("forum", $conn) or die(mysql_error());
 
//verify the topic exists
$verify_topic = "select topic_title from forum_topics  where topic_id = $_GET[topic_id]";
$verify_topic_res = mysql_query($verify_topic,$conn) or die (mysql_error());
 
if (mysql_num_rows($verify_topic_res) < 1) {
    //this topic does not exist
    $display_block = "<p><em>You have selected an invalid topic, please<a href=\"topiclist.php\">try again</a></em></p>";
} else {
    //get topic title
    $topic_title = stripslashes(mysql_result($verify_topic_res,0,'topic_title'));
 
    //gather the posts
    $get_posts = "select post_id, post_text, date_format(post_create_time, '%b %e %y at %r') as fmt_post_create_time, post_owner from forum_posts where topic_id = $_GET[topic_id] order by post_create_time asc";
 
    $get_posts_res = mysql_query($get_posts,$conn) or die (mysql_error());
 
    //create the display string
    $display_block = "
    <p>Showing posts for the <strong>$topic_title</strong> topic:</p>
 
    <table width=100% cellpadding=3 cellspacing=1 border=1>
    <tr>
    <th>AUTHOR</th>
    <th>POST</th>
    </tr>";
 
        while ($posts_info = mysql_fetch_array($get_posts_res))  {
        $post_id = $posts_info['post_id'];
        $post_text = nl2br(stripslashes($posts_info['post_text']));
        $post_create_time = $posts_info['fmt_post_create_time'];
        $post_owner = stripslashes($posts_info['post_owner']); 
 
        //add to display
        $display_block .= "
        
        <tr>
        <td width=35% valign=top>$post_owner<br>[$post_create_time]</td>
        <td width=65% valign=top>$post_text<br><br>
        <a href=\"replytopost.php?post_id=$post_id\"><strong>REPLY TO POST</strong></a></td>
        </tr>";
    }
 
    //close table
    $display_block .= "</table>";
}
?>
 
<html>
<head>
<title>Posts in Topic</title>
</head>
<body>
<h1>Posts in Topic</h1>
<?php print $display_block; ?>
</body>
</html>

Table that doesn't show up except the header, from showtopic.php:

Code: Select all

<p>Showing posts for the <strong>$topic_title</strong> topic:</p>
 
    <table width=100% cellpadding=3 cellspacing=1 border=1>
    <tr>
    <th>AUTHOR</th>
    <th>POST</th>
    </tr>";
 
        while ($posts_info = mysql_fetch_array($get_posts_res))  {
        $post_id = $posts_info['post_id'];
        $post_text = nl2br(stripslashes($posts_info['post_text']));
        $post_create_time = $posts_info['fmt_post_create_time'];
        $post_owner = stripslashes($posts_info['post_owner']); 
 
        //add to display
        $display_block .= "
        
        <tr>
        <td width=35% valign=top>$post_owner<br>[$post_create_time]</td>
        <td width=65% valign=top>$post_text<br><br>
        <a href=\"replytopost.php?post_id=$post_id\"><strong>REPLY TO POST</strong></a></td>
        </tr>";
    }
 
    //close table
    $display_block .= "</table>";



topiclist.php [table runs fine]:

Code: Select all

<?php
//connest to server and select database
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("forum", $conn) or die(mysql_error());
 
//gather the topics
$get_topics = "select topic_id, topic_title, date_format(topic_create_time, '%b %e %y at %r') as fmt_topic_create_time, topic_owner from forum_topics order by topic_create_time desc";
$get_topics_res = mysql_query($get_topics,$conn) or die(mysql_error());
if (mysql_num_rows($get_topics_res) < 1) {
    //there are no topics, so say so
    $display_block = '<p><em>No Topics exist,</em></p>';
    } else {
    //create the display topics
    $display_block = "
    <table cellpadding=3 cellspacing=1 border=1>
    <tr>
    <th>Topic Title</th>
    <th>Number of posts</th>
    </tr>";
    
    while ($topic_info = mysql_fetch_array($get_topics_res)) {
        $topic_id = $topic_info['topic_id'];
        $topic_title = stripslashes($topic_info['topic_title']);
        $topic_create_time = $topic_info['fmt_topic_create_time'];
        $topic_owner = stripslashes($topic_info['topic_owner']);
 
        //get number of posts
        $get_num_posts = "select count(post_id) from forum_posts where topic_id = $topic_id";
        $get_num_posts_res = mysql_query($get_num_posts,$conn) or die(mysql_error());
        $num_posts = mysql_result($get_num_posts_res,0,'count(post_id)');
 
        //add to display
        $display_block .= "
        <tr>
        <td><a href=\"showtopic.php?topic_id=$topic_id\"><strong>$topic_title</strong></a><br>
        Created on $topic_create_time by $topic_owner</td>
        <td align=center>$num_posts</td>
        </tr>";
        }
        
        //close up the table
        $display_block .= "</table>";
    }
    ?>
 
    <html>
    <head>
    <title>Topics in the Forum</title>
    <body>
    <h1>Topics in the Forum</h1>
    <?php print $display_block; ?>
    <p>Would you like to <a href="addTopicForm.html"> add a Topic</a>?</p>
    </body>
    </head>
    </html>
 
FyreHeart
Forum Newbie
Posts: 5
Joined: Tue Feb 17, 2009 12:27 am

Re: HTML table not working in php

Post by FyreHeart »

I see a couple of problems:

First, syntactically, using superglobals inside double quotes like you are will throw a warning. Be sure to use curly braces and single quotes to avoid that:
Instead of
$get_posts = "select post_id, BLAH where topic_id = $_GET[topic_id]";
Do this:
$get_posts = "select post_id, BLAH where topic_id = {$_GET['topic_id']}";

Also, from a security standpoint, you should never put superglobals directly in a query. Instead, do this:
$topic_id = mysql_real_escape_string(strip_tags($_GET['topic_id']));
$get_posts = "select post_id, BLAH where topic_id = $topic_id";

OK - stepping off the soapbox...

It looks like what's wrong is you aren't using the concatenate-equals ( .= ), so you're overwriting your $display_block with every iteration of your WHILE loop:

while ($posts_info = mysql_fetch_array($get_posts_res)) {
BLAH

$display_bloc= "

<tr>
<td width=35% valign=top>$post_owner<br>[$post_create_time]</td>
<td width=65% valign=top>$post_text<br><br>
<a href=\"replytopophp?post_id=$post_id\"><strong>REPLY TO POST</strong></a></td>
</tr>";
}

Should be:

while ($posts_info = mysql_fetch_array($get_posts_res)) {
BLAH

$display_bloc .= "

<tr>
<td width=35% valign=top>$post_owner<br>[$post_create_time]</td>
<td width=65% valign=top>$post_text<br><br>
<a href=\"replytopophp?post_id=$post_id\"><strong>REPLY TO POST</strong></a></td>
</tr>";
}

Hope that helps.

-John
Post Reply