Log in

View Full Version : php script shows blank page



matthewbluewars
04-11-2008, 10:24 PM
I wrote a forum script in PHP. My web host has PHP and MySQL installed. All of the databases have been set up. I have uploaded it to http://lightbulbproductions.net/forum_home.php. However, it gives me a blank page. Here is the code (note: the MySQL username and password has been replaced with admin and pass in this block of code. The real username/password ARE on the script that is on the server.)


<html>
<head>
<script language="javascript" type="text/javascript">
window.onload = function()
{
var trs = document.getElementsByTagName("tr");
for (var i = 0; i < trs.length; i++)
{
var tr = document.getElementsByTagName("tr")[i];
if (i == (parseInt(trs.length) - 1))
{
tr.innerHTML += '<a name="Most_Recent"></a>';
}

}
}
</script>
<title></title>

</head>

<body>
<h1>Posts</h1>
<a href="#Most_Recent">Most Recent</a>&nbsp;<a href="#Quick_Reply">Quick Reply</a>&nbsp;<a href="forum_form.htm">Advanced Reply</a><br />
<?php
$con = mysql_connect("localhost","admin","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("forum", $con);

$result = mysql_query("SELECT * FROM posts");

echo "<table border='1'>
<tr>
<th>Username</th>
<th>Title</th>
<th>Post</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Username'] . "</td>";
echo "<td>" . $row['Title'] . "</td>";
echo "<td>" . $row['Post'] . "</td>";
echo "</tr>";
}
echo "<a name="Most_Recent"></a></table>";
mysql_close($con);
?>
<br /><button onclick="location.reload()">Reload Posts</button>
<br /><a name="Quick_Reply">Quick Reply:
<form action="" method="post">
Username: <input name="username" id="username" type="text" /><br />
Password: <input name="pass" id="pass" type="password" /><br />
Post title: <input name="title" id="title" type="text" /> Text: <textarea name="post" id="post"></textarea>
<input type="submit" value="Submit"/>
</form></a>
<a href="forum_form.htm">Advanced Reply</a>
<?php
$username = $_POST["username"];
$pass = $_POST["pass"];
$title = $_POST["title"];
$post = $_POST["post"];
$con2 = mysql_connect("localhost","admin","password");
if (!$con2)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("forum", $con2);

$result = mysql_query("SELECT * FROM users
WHERE username == $username");
while($row = mysql_fetch_array($result))
{
echo '<p>Trying ' . $row['username'] . '<img src="ajax-loader.gif" width="220" height="19">';
if (($row['username'] == $username) && ($row['password'] == $pass))
{
echo 'Login Succeeded! Posting<img src="wait.gif" alt="Animated Loading Symbol" name="posting" id="posting" width="32" height="32">';
sql();
echo 'Posted!';
break;
} else {
echo 'Login failed.'
}
echo 'Moving on...';
}
mysql_close($con2);
function sql() {
$con3 = @mysql_connect("localhost", "admin", "password");
if (!$con3) {
echo( "<P>Unable to connect to the " .
"database server at this time.</P>" );
exit();
} else {
mysql_query("INSERT INTO forum (user, title, post)
VALUES ($username, $title, $post)");

}
mysql_close($con3);
}
?>
</body>

</html>

thetestingsite
04-11-2008, 10:26 PM
Try placing this at the very top of the page (above the opening html tag) :



<?php
error_reporting(E_ALL);
?>


This will tell you if you have any errors in the code, but just skimming over the code you posted, I can't see anything that jumps out at me (although, I did not look very hard at it).

Hope this helps.

djr33
04-11-2008, 10:35 PM
This is the worst sort of error to fix in PHP. As suggested, turn on error reporting. If you're lucky it'll give you a line number (or several) and you will know at least where the problem is.

If not, here's the best strategy for debugging:
1. Section by section (then line by line) go through your script and see how far it actually goes. Tests include echo 'it got to line 15';, die('it got here, and stopped processing');, and also just commenting out sections to see if they stop it.
2. You can also track variables. If something is crucial, echo it along the way systematically until you find where it goes wrong.

No matter how complex the script, the above will work, but it might take a long time.

Remember, you can go backwards or forwards in the script, to find either the last line that works, or the first line that works, depending.

Nile
04-11-2008, 10:39 PM
Alright, throughout the whole thing your connect to the same exact mysql. So you don't need to close it till the end of your script. Then change:


mysql_select_db("forum", $con);

To this:


mysql_select_db("forum");

Then I noticed your trying to use exit. Exit is not a function, well, is basically is, but you don't need it in this case you should use die(); But I noticed that you want to return text with your echo, so you can make it a die, so look blow:
Change:


echo( "<P>Unable to connect to the " .
"database server at this time.</P>" );
exit();
(The highlighted part of above isn't needed and may of made your page blank, so I fixed it. But still make all the changes I'm telling you above.)
To:


die( "<P>Unable to connect to the
"database server at this time.</P>");

I hope this Helps!

thetestingsite
04-11-2008, 10:41 PM
I noticed your trying to use exit. Exit is not a function, and doesn't return anything, in this case, you don't need to call the function, but I noticed that you want to return text with your echo.


View http://us.php.net/exit

Basically, it stops any further execution of the script after displaying the error message in the echo statement.

Nile
04-11-2008, 10:42 PM
Don't worry, I fixed that. He didn't need to use exit as a function, which is why I chose to said what I said. ;)

matthewbluewars
04-11-2008, 10:54 PM
I tried using all of the methods above, but it still ends up blank...
If anyone here uses Yahoo! WebHosting, I clicked "View Source at Line..." in the PHP editor. The numbers are line numbers and the echo "checkpoint " . $i was to see what parts worked. This is what it gave me (once again, I edited out the MySQL username/password):

1 <?php
2 error_reporting(E_ALL);
3 ?>
4 <html>
5 <head>
6 <script language="javascript" type="text/javascript">
7 window.onload = function()
8 {
9 var trs = document.getElementsByTagName("tr");
10 for (var i = 0; i < trs.length; i++)
11 {
12 var tr = document.getElementsByTagName("tr")[i];
13 if (i == (parseInt(trs.length) - 1))
14 {
15 tr.innerHTML += '<a name="Most_Recent"></a>';
16 }
17
18 }
19 }
20 </script>
21
22 <title>Untitled</title>
23
24 </head>
25 <body>
26 <h1>Posts</h1>
27 <a href="#Most_Recent">Most Recent</a>&nbsp;<a href="#Quick_Reply">Quick Reply</a>&nbsp;<a href="forum_form.htm">Advanced Reply</a><br />
28 <?
29 echo 'checkpoint 1';
30 $con = mysql_connect("localhost","admin","pass");
31 if (!$con)
32 {
33 die('Could not connect: ' . mysql_error());
34 }
35
36 mysql_select_db("forum");
37 echo 'checkpoint 2';
38 $result = mysql_query("SELECT * FROM posts");
39
40 echo "<table border='1'>
41 <tr>
42 <th>Username</th>
43 <th>Title</th>
44 <th>Post</th>
45 </tr>";
46 echo 'checkpoint 3';
47 $i = 4;
48 while($row = mysql_fetch_array($result))
49 {
50 echo "<tr>";
51 echo "<td>" . $row['Username'] . "</td>";
52 echo "<td>" . $row['Title'] . "</td>";
53 echo "<td>" . $row['Post'] . "</td>";
54 echo "</tr>";
55 echo 'checkpoint ' . $i;
56 $i++
57 }
58 echo "</table>";
59 mysql_close($con);
60 echo 'checkpoint ' . $i;
61 $i++
62 ?>
63 <br /><button onclick="location.reload()">Reload Posts</button>
64 <br /><a name="Quick_Reply">Quick Reply:
65 <form action="" method="post">
66 Username: <input name="username" id="username" type="text" /><br />
67 Password: <input name="pass" id="pass" type="password" /><br />
68 Post title: <input name="title" id="title" type="text" /> Text: <textarea name="post" id="post"></textarea>
69 <input type="submit" value="Submit"/>
70 </form></a>
71 <a href="forum_form.htm">Advanced Reply</a>
72 <?php
73 $username = $_POST["username"];
74 $pass = $_POST["pass"];
75 $title = $_POST["title"];
76 $post = $_POST["post"];
77 $con2 = mysql_connect("localhost","admin","pass");
78 echo 'checkpoint ' . $i;
79 $i++
80 if (!$con2)
81 {
82 die('Could not connect: ' . mysql_error());
83 }
84
85 mysql_select_db("forum", $con2);
86 echo 'checkpoint ' . $i;
87 $i++
88 $result = mysql_query("SELECT * FROM users
89 WHERE username == $username");
90 while($row = mysql_fetch_array($result))
91 {
92 echo '<p>Trying ' . $row['username'] . '<img src="ajax-loader.gif" width="220" height="19">';
93 echo 'checkpoint ' . $i;
94 $i++
95 if (($row['username'] == $username) && ($row['password'] == $pass))
96 {
97 echo 'Login Succeeded! Posting<img src="wait.gif" alt="Animated Loading Symbol" name="posting" id="posting" width="32" height="32">';
98 sql();
99 echo 'Posted!';
100 break;
101 } else {
102 echo 'Login failed.'
103 }
104 echo 'Moving on...';
105 }
106 mysql_close($con2);
107 echo 'checkpoint ' . $i;
108 $i++
109 function sql() {
110 $con3 = @mysql_connect("localhost", "admin", "password");
111 echo 'checkpoint ' . $i;
112 $i++
113 if (!$con3) {
114 die( "Unable to connect to the
115 database server at this time." );
116 } else {
117 mysql_query("INSERT INTO forum (user, title, post)
118 VALUES ($username, $title, $post)");
119 echo 'checkpoint ' . $i;
120 $i++
121 }
122 mysql_close($con3);
123 }
124 ?>
125
126 </body>
127 </html>
128
129
130
131

PS. The phpMyAdmin for the MySQL is in the subdirectory "php". Is it necessary to change the code in any way?

djr33
04-11-2008, 10:55 PM
Exit is not a function, well, is basically is, but you don't need it in this case you should use die();die is actually just a copy of exit. Personally, for some reason (the name, I guess), I like using die(), but technically exit is more proper.


In this case, though, he did NOT use exit. He used echo. In that case, it is sorta a super function. I don't think using parentheses will hurt anything, but it is not standard.
However, changing this to die() will change things significantly.
If you use die() it will END the script if it fails; if you use echo, it will just print out that error statement. Both have their uses, but I'm not sure that die() is necessarily needed in this case.

thetestingsite
04-11-2008, 11:02 PM
In any case, (to the OP) is there any other code above the opening html tag in the snippet you posted above? The reason I ask is because even if the script is erroring out, you should still be able to see the source code (at least to the point where the script fails). In this case, nothing is displayed (not even in the source).

matthewbluewars
04-13-2008, 10:32 PM
I have fixed it by seperating the code for posting and the code for accessing.
The new page is at http://lightbulbnetkidsclub.com/forum_access.php. I still have some problems with the MySQL, but am hoping to fix them promptly.

matthewbluewars
04-16-2008, 07:41 PM
There was something wrong with the MySQL. Abandoned project.

matthewbluewars
05-01-2008, 11:37 PM
I've given up on the project