I have a problem... In my forum, whenever you view a post, where it should say the user's username, it says "invalid User". It has done this before and I have solved it, but I can seem to solve it now. I'm not sure what files you will need, so here is my code:
Forum.php
PHP Code:
<?php
$id = mss($_GET['id']);
if($id){
$sql = "SELECT * FROM `forum_sub_cats` WHERE `id`='".$id."'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 0){
echo "The forum category you supplied does not exist!\n";
}else {
$row = mysql_fetch_assoc($res);
if($row['admin'] == 1 && $admin_user_level == 0){
echo "You must be an administrator to view this forum!\n";
}else {
$sql2 = "SELECT * FROM `forum_topics` WHERE `cid`='".$row['id']."' ORDER BY time DESC";
$res2 = mysql_query($sql2) or die(mysql_error());
if(mysql_num_rows($res2) == 0){
echo "There are no topics in this forum, <a href=\"./index.php?act=create&id=".$row['id']."\">click here</a> to create a topic!\n";
}else {
echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\" width=\"100%\">\n";
echo "<tr><td colspan=\"4\" align=\"right\"><a href=\"./index.php?act=create&id=".$row['id']."\">create a topic</a></td></tr>\n";
echo "<tr align=\"center\"><td class=\"forum_header\">Title</td><td class=\"forum_header\">User</td><td class=\"forum_header\">Date Created</td><td class=\"forum_header\">Replies</td></tr>\n";
while($row2 = mysql_fetch_assoc($res2)){
$sql3 = "SELECT count(*) AS num_replies FROM `forum_replies` WHERE `tid`='".$row2['id']."'";
$res3 = mysql_query($sql3) or die(mysql_error());
$row3 = mysql_fetch_assoc($res3);
echo "<tr align=\"center\"><td><a href=\"./index.php?act=topic&id=".$row2['id']."\">".s($row2['title'])."</a></td><td>".uid($row2['uid'])."</td><td>".$row2['date']."</td><td>".$row3['num_replies']."</td></tr>\n";
}
echo "</table>\n";
}
}
}
}else {
echo "Please supply a category ID!\n";
}
if(!$tid){
echo "You did not supply a topic to add a reply to";
}else {
$sql = "SELECT * FROM `forum_topics` WHERE `id`='".$tid."'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 0){
echo "This topic does not exist";
}else {
$row = mysql_fetch_assoc($res);
$sql2 = "SELECT admin FROM `forum_sub_cats` WHERE `id`='".$row['cid']."'";
$res2 = mysql_query($sql2) or die(mysql_error());
$row2 = mysql_fetch_assoc($res2);
if($row2['admin'] == 1 && $admin_user_level == 0){
echo "You do not have sufficient priveleges to add a reply to this topic";
}else {
if(!$msg){
echo "You did not supply a reply";
}else {
if(strlen($msg) < 3 || strlen($msg) > 10000){
echo "Your reply must be between 3 and 10,000 characters!";
}else {
$date = date("m-d-y") . " at " . date("h:i:s");
$time = time();
$sql3 = "INSERT INTO `forum_replies` (`tid`,`uid`,`message`,`date`,`time`) VALUES('".$tid."','".$_SESSION['uid']."','".$msg."','".$date."','".$time."')";
$res3 = mysql_query($sql3) or die(mysql_error());
$sql4 = "UPDATE `forum_topics` SET `time`='".time()."' WHERE `id`='".$tid."'";
$res4 = mysql_query($sql4) or die(mysql_error());
header("Location: ./index.php?act=topic&id=".$tid);
}
}
}
}
}
}
$sql2 = "SELECT * FROM `forum_sub_cats` WHERE `cid`='".$row2['id']."' AND `admin` < ".$row['admin']."+1";
$res2 = mysql_query($sql2) or die(mysql_error());
?>
</div>
</div>
</div>
<!-- Designed by w w w . t e m p l a t e m o . c o m -->
<div id="templatemo_footer">
<script src="site_footer.js"></script>
</div>
</center>
</body>
</html>
<?php
ob_end_flush();
?>
global.php
PHP Code:
<?php
$con = mysql_connect("********","******","*********") or die(mysql_error());
$db = mysql_select_db("*******", $con);
function mss($value){
return mysql_real_escape_string(trim(strip_tags($value)));
}
function topic_go($id){
echo "<meta http-equiv=\"refresh\" content=\"0;url=index.php?act=topic&id=".$id."\">";
}
function s($value){
return stripslashes($value);
}
function topic($input){
// bbcode
return nl2br(strip_tags(stripslashes(htmlentities(htmlspecialchars($input)))));
}
function uid($uid, $link = FALSE){
$sql = "SELECT username FROM `users` WHERE `id`='".$uid."'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 0){
return "Invalid User";
}else {
$row = mysql_fetch_assoc($res);
if(!$link){
return $row['username'];
}else {
return "<a href=\"../view.php?id=".$uid."\">".$row['username']."</a>";
}
}
}
function post($uid){
$sql = "SELECT * FROM `forum_replies` WHERE `uid`='".$uid."'";
$res = mysql_query($sql) or die(mysql_error());
return mysql_num_rows($res);
}
function isa($uid){
$sql = "SELECT admin FROM `users` WHERE `id`='".$uid."'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res);
return $row['admin'];
}
I have a problem... In my forum, whenever you view a post, where it should say the user's username, it says "invalid User". It has done this before and I have solved it, but I can seem to solve it now. I'm not sure what files you will need, so here is my code:
Forum.php
PHP Code:
<?php
$id = mss($_GET['id']);
if($id){
$sql = "SELECT * FROM `forum_sub_cats` WHERE `id`='".$id."'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 0){
echo "The forum category you supplied does not exist!\n";
}else {
$row = mysql_fetch_assoc($res);
if($row['admin'] == 1 && $admin_user_level == 0){
echo "You must be an administrator to view this forum!\n";
}else {
$sql2 = "SELECT * FROM `forum_topics` WHERE `cid`='".$row['id']."' ORDER BY time DESC";
$res2 = mysql_query($sql2) or die(mysql_error());
if(mysql_num_rows($res2) == 0){
echo "There are no topics in this forum, <a href=\"./index.php?act=create&id=".$row['id']."\">click here</a> to create a topic!\n";
}else {
echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\" width=\"100%\">\n";
echo "<tr><td colspan=\"4\" align=\"right\"><a href=\"./index.php?act=create&id=".$row['id']."\">create a topic</a></td></tr>\n";
echo "<tr align=\"center\"><td class=\"forum_header\">Title</td><td class=\"forum_header\">User</td><td class=\"forum_header\">Date Created</td><td class=\"forum_header\">Replies</td></tr>\n";
while($row2 = mysql_fetch_assoc($res2)){
$sql3 = "SELECT count(*) AS num_replies FROM `forum_replies` WHERE `tid`='".$row2['id']."'";
$res3 = mysql_query($sql3) or die(mysql_error());
$row3 = mysql_fetch_assoc($res3);
echo "<tr align=\"center\"><td><a href=\"./index.php?act=topic&id=".$row2['id']."\">".s($row2['title'])."</a></td><td>".uid($row2['uid'])."</td><td>".$row2['date']."</td><td>".$row3['num_replies']."</td></tr>\n";
}
echo "</table>\n";
}
}
}
}else {
echo "Please supply a category ID!\n";
}
if(!$tid){
echo "You did not supply a topic to add a reply to";
}else {
$sql = "SELECT * FROM `forum_topics` WHERE `id`='".$tid."'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 0){
echo "This topic does not exist";
}else {
$row = mysql_fetch_assoc($res);
$sql2 = "SELECT admin FROM `forum_sub_cats` WHERE `id`='".$row['cid']."'";
$res2 = mysql_query($sql2) or die(mysql_error());
$row2 = mysql_fetch_assoc($res2);
if($row2['admin'] == 1 && $admin_user_level == 0){
echo "You do not have sufficient priveleges to add a reply to this topic";
}else {
if(!$msg){
echo "You did not supply a reply";
}else {
if(strlen($msg) < 3 || strlen($msg) > 10000){
echo "Your reply must be between 3 and 10,000 characters!";
}else {
$date = date("m-d-y") . " at " . date("h:i:s");
$time = time();
$sql3 = "INSERT INTO `forum_replies` (`tid`,`uid`,`message`,`date`,`time`) VALUES('".$tid."','".$_SESSION['uid']."','".$msg."','".$date."','".$time."')";
$res3 = mysql_query($sql3) or die(mysql_error());
$sql4 = "UPDATE `forum_topics` SET `time`='".time()."' WHERE `id`='".$tid."'";
$res4 = mysql_query($sql4) or die(mysql_error());
header("Location: ./index.php?act=topic&id=".$tid);
}
}
}
}
}
}
$sql2 = "SELECT * FROM `forum_sub_cats` WHERE `cid`='".$row2['id']."' AND `admin` < ".$row['admin']."+1";
$res2 = mysql_query($sql2) or die(mysql_error());
?>
</div>
</div>
</div>
<!-- Designed by w w w . t e m p l a t e m o . c o m -->
<div id="templatemo_footer">
<script src="site_footer.js"></script>
</div>
</center>
</body>
</html>
<?php
ob_end_flush();
?>
global.php
PHP Code:
<?php
$con = mysql_connect("********","******","*********") or die(mysql_error());
$db = mysql_select_db("*******", $con);
function mss($value){
return mysql_real_escape_string(trim(strip_tags($value)));
}
function topic_go($id){
echo "<meta http-equiv=\"refresh\" content=\"0;url=index.php?act=topic&id=".$id."\">";
}
function s($value){
return stripslashes($value);
}
function topic($input){
// bbcode
return nl2br(strip_tags(stripslashes(htmlentities(htmlspecialchars($input)))));
}
function uid($uid, $link = FALSE){
$sql = "SELECT username FROM `users` WHERE `id`='".$uid."'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 0){
return "Invalid User";
}else {
$row = mysql_fetch_assoc($res);
if(!$link){
return $row['username'];
}else {
return "<a href=\"../view.php?id=".$uid."\">".$row['username']."</a>";
}
}
}
function post($uid){
$sql = "SELECT * FROM `forum_replies` WHERE `uid`='".$uid."'";
$res = mysql_query($sql) or die(mysql_error());
return mysql_num_rows($res);
}
function isa($uid){
$sql = "SELECT admin FROM `users` WHERE `id`='".$uid."'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res);
return $row['admin'];
}
?>
Thanks in advance!
The problem, I think, is the query in the function uid(). Echo out the query before using it in mysql_query() to debug, just in case one of the calls to uid() has something unexpected in the query.
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
function uid($uid, $link = FALSE){
$sql = "SELECT username FROM `users` WHERE `id`='".$uid."'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 0){
return "Invalid User";
}else {
$row = mysql_fetch_assoc($res);
if(!$link){
return $row['username'];
}else {
return "<a href=\"../view.php?id=".$uid."\">".$row['username']."</a>";
}
}
}
Change it to this:
PHP Code:
function uid($uid, $link = FALSE){
$sql = "SELECT username FROM `users` WHERE `id`='".$uid."'";
echo 'sql was '.$sql.'<br/>';
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 0){
return "Invalid User";
}else {
$row = mysql_fetch_assoc($res);
if(!$link){
return $row['username'];
}else {
return "<a href=\"../view.php?id=".$uid."\">".$row['username']."</a>";
}
}
}
and make sure the query makes sense every time (i.e. it's returning the results it should). If it's not, you know there's something wrong with the $uid you're passing.
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
There's your problem. You're calling that function somewhere, and passing $uid as 4. It returns 'invalid user' because the sql doesn't match anything (returns 0 rows to be more specific, hence why if(mysql_num_rows($res) == 0) evaluted true).
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
To narrow it down do a search in all of your forum code that you find
PHP Code:
uid(
You will be able to see what you are passing to that function. Then print out the variables going into that function just before it is called. Also try changing your function to this
If id is an int there is no need for the single quotes. I can't be sure but that might make the query fail. Does it happen for every user? Every post? Also in reply.php you have this
Without the session_start() there anyone who goes to reply.php will get redirected to index.php, whoever did your forum for you, they made a lot of mistakes. Here is where you are obviously getting the wrong value
What are you getting for that value? $row['uid'] seems to be coming from your topics table. Check that table to see the uid in that table for your topics. What are they?
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||