0

I have the following HTML/PHP/CSS/JavaScript page:

<html>
<head>
    <title>Test Guestbook</title>
    <style type="text/css">
        #titles {
            font-family: "Comic Sans MS";
            font-size: 15px;
        }
        #charremain {
            font-family: "Comic Sans MS";
            font-size: 12px;
        }
        .tb {
            border-top: #000000 1px solid;
            border-bottom: #000000 1px solid;
            border-left: #000000 1px solid;
            border-right: #000000 1px solid;
            background-color: "#000000";
            font-family: "Comic Sans MS";
            resize: none;
        }
        .submit {
            background-color: #FFCC99;
            font-family: "Comic Sans MS";
        }
        table {
            text-align: center;
            width: 100%;
        }
    </style>
    <script type="text/javascript">
        function count(x){
            if(x <= 100) {
                document.getElementById('remain').innerHTML = 100 - x;
            }
        }
    </script>
    <script type="text/javascript">
        function validate(form){
            if(form.post.value == "" || form.name.value == ""){
                alert("Please fill in all fields");
                return false;
            } else {
                return true;
            }
        }
    </script>
</head>
<body bgcolor='#FFFCC'>
    <div id='titles' align='center'>
        <form action='test.php' method='POST' onSubmit='return validate(this);'>
        Submit a post: <br \>
        <textarea id='post' class='tb' cols='25' rows='3' name='post' maxLength='100' onKeyUp="count(this.value.length)"></textarea>
        <p id='charremain'>Characters Remaining: <span id='remain'>100</span></p>
        Your name: <input class='tb' type='text' name='name' id='name'><br \>
        <input class='submit' type='submit' value='Submit'><br \>
        </form>
        <br \><br \>
        <H2>Current Posts:</H2>
    </div>
    <?php
        $con = mysql_connect('localhost','root','') or die("Could not connect to localhost, check connection!");
        mysql_select_db('test') or die("Could not find database");

        $sql = mysql_query("SELECT * FROM `posts`");
        $sqlCnt = mysql_num_rows($sql);

        if($sqlCnt != 0) {
            echo "<table align='center'>
                  <tr><td><u><H4>Message</H4></u></td>
                  <td><u><H4>Poster</H4></u></td></tr>";
            while($row = mysql_fetch_array($sql)) {
                echo "<tr><td>";
                echo $row['message'];
                echo "</td><td>";
                echo $row['poster']
                echo "</td></tr>";
            }
        } else {
            echo "<div align='center'>Sorry, no posts found!</div>";
        }
    ?>
    </table>
</body>

Instead of proceeding to the while() loop, the webpage displays everything after , when it should be processing it. Any ideas?

Update: I added the missing semicolon, but no luck. It is an HTML file, and here is a screenshot of what I see:

http://img846.imageshack.us/img846/9867/phpx.png

Update 2: The else section of the if() statement is working, but when the $sqlCount != 0 the code is not processed. Everything after the </tr> statement on the 3rd line of the first echo statement is echoed out, beginning with the closing parenthesis.

12
  • Please clarify "the webpage displays everything after". What is "everything"? Are you seeing PHP code in your web browser? Commented Aug 2, 2011 at 0:13
  • 2
    Is this a .php file? Also, you are missing a semicolon on echo $row['poster']. Commented Aug 2, 2011 at 0:13
  • If you look at the source of the page, do you see the starting <?php in there? Commented Aug 2, 2011 at 0:14
  • 2
    Random note: Please use slashes, not backslahes, so that's <br />, not <br \>. In fact, <br /> isn't even valid here. This is HTML, <br /> is only correct in XHTML. So just use <br>. Oh, and for the love of all that is holy, please do not use the horrible or die construct... Commented Aug 2, 2011 at 0:15
  • @Jason: when you say it is an html file, does the filename perhaps not end in .php? Commented Aug 2, 2011 at 0:19

4 Answers 4

2

Ensure that your file name ends with an extension that is valid for PHP scripts (eg: .php).

Also ensure that your webserver (Apache, etc) is loading the PHP module.

Sign up to request clarification or add additional context in comments.

Comments

2

I think I found it:

echo $row['poster']

is missing the final semicolon.

4 Comments

You probably have display_errors disabled or error_reporting set to 0. It's best to enable display_errors and use error_reporting(E_ALL) when developing code. Makes catching errors much easier.
Of course. In PHP error handling, pretty much everything is possible. Except displaying errors in a sane manner that is :-)
@Carpetsmoker: how would you like to see it (aside from the usual PHP newbee misconfiguration issues of course)?
Well, that's a topic for a whole discussion and far beyond the scope of these comments :-) Not to mention it's already past my bedtime :)
2

If you are Apache be sure to check your php.ini for error reporting and add:

ini_set('display_errors', 1); 
ini_set('log_errors', 1); 
ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); 
error_reporting(E_ALL);
  • display_errors (we want it to be On or 1)
  • display_startup_errors (we want it to be On or 1)
  • log_errors (we want it to be On or 1)
  • error_log (it should be anything but undefined)
  • error_reporting (it should be 2047 or larger)

It'll make debugging a lot easier!

Comments

2

This happened to me when requesting a localhost php file through a relative path in AS3, then I used the full URL path and it worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.