Jump to content


Photo

A simple question...


  • Please log in to reply
9 replies to this topic

#1 Chevy

Chevy
  • Members
  • PipPipPip
  • Advanced Member
  • 163 posts

Posted 20 August 2006 - 02:38 PM

It is really early and I need to figure out a problem I am having...here is my code:
<?php
session_start();

if ($_SESSION['logged']==yes){}else {echo "Please login!";exit;}

mysql_connect("localhost", "_real", "hidden") or die(mysql_error());
mysql_select_db("_real") or die(mysql_error());

$username = $_SESSION['username'];
$id = $_GET['id'];
$topicid = $_GET['topicid'];

$query = "SELECT * FROM reply WHERE topicid='$topicid' AND groupid='$id'"; 
$result = mysql_query($query) or die(mysql_error());
$query2 = "SELECT * FROM users WHERE username='$username'"; 
$result2 = mysql_query($query2) or die(mysql_error());
$user = mysql_fetch_array($result2);

echo "<table border=\"0\" cellspacing=\"1\" cellpadding=\"1\">";
while($main = mysql_fetch_array($result)){
echo '<tr><td style="border:1px solid black;" width="100">
<center>';
echo "<b>";echo $main['poster'];echo "</b><br>";
echo "<img src=\"";echo $user['avatar'];echo "\"><br>
Posts: <b>";echo $user['posts'];echo "</b>
</center></td>";

echo '<td style="border:1px solid black;" width="450">';
echo "Posted On: <i>";echo $main['time'];echo "</i><hr>";
echo $main['message'];
echo "</td></tr>";
}
echo "</table>";
?>

Okay now when the user posts it adds 1 to a table called users, but they are all the same for every user when it shows up on the page (the post count is the same) and help?

Yes I know my code is messy  ;D

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 August 2006 - 02:48 PM

How are you tracking the number of posts a user makes?

Cleaner, more readable version:

<?php
session_start();

if ($_SESSION['logged'] != "yes"){
	echo "Please login!";
	exit;
}

mysql_connect("localhost", "_real", "hidden") or die(mysql_error());
mysql_select_db("_real") or die(mysql_error());

$username = $_SESSION['username'];
$id = $_GET['id'];
$topicid = $_GET['topicid'];

$query = "SELECT * FROM reply WHERE topicid='$topicid' AND groupid='$id'"; 
$result = mysql_query($query) or die(mysql_error());

$query2 = "SELECT * FROM users WHERE username='$username'"; 
$result2 = mysql_query($query2) or die(mysql_error());

$user = mysql_fetch_array($result2);

echo "
	<table border=\"0\" cellspacing=\"1\" cellpadding=\"1\">";

while($main = mysql_fetch_array($result)) {
	echo '
		<tr>
			<td style="text-align: center; border:1px solid black;" width="100">
				<b>' . $main['poster'] . '</b><br>
				<img src="' . $user['avatar'] . '"><br>
				Posts: <b>' . $user['posts'] . '</b>
			</td>
			<td style="border:1px solid black;" width="450">
				Posted On: <i>' . $main['time'] . '</i><hr>
				' . $main['message'] . '
			</td>
		</tr>';
}

echo "</table>";
?>


#3 Chevy

Chevy
  • Members
  • PipPipPip
  • Advanced Member
  • 163 posts

Posted 20 August 2006 - 02:53 PM

I have a table called users and when they post it adds one to the row in users called posts



#4 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 August 2006 - 02:57 PM

what is the query you are using for that?

#5 Chevy

Chevy
  • Members
  • PipPipPip
  • Advanced Member
  • 163 posts

Posted 20 August 2006 - 03:03 PM

if (isset($_POST['submit'])){
mysql_query("INSERT INTO reply 
(groupid, topicid, poster, message, time) VALUES('$groupid', '$topicid', '$username', '$message', '$time') ");

$posts = $user['posts']++;

mysql_query("UPDATE users SET posts='$posts' WHERE username='$username'") 
or die(mysql_error());
}




#6 Chevy

Chevy
  • Members
  • PipPipPip
  • Advanced Member
  • 163 posts

Posted 20 August 2006 - 03:31 PM

Does anyone know how I could do this?

#7 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 August 2006 - 03:34 PM

Try this:

<?php
session_start();

if ($_SESSION['logged'] != "yes"){
	echo "Please login!";
	exit;
}

mysql_connect("localhost", "_real", "hidden") or die(mysql_error());
mysql_select_db("_real") or die(mysql_error());

$username = $_SESSION['username'];
$id = $_GET['id'];
$topicid = $_GET['topicid'];

$query = "SELECT * FROM reply LEFT JOIN users where reply.poster = users.username WHERE topicid='$topicid' AND groupid='$id'"; 
$result = mysql_query($query) or die(mysql_error());

echo "
	<table border=\"0\" cellspacing=\"1\" cellpadding=\"1\">";

while($main = mysql_fetch_array($result)) {
	echo '
		<tr>
			<td style="text-align: center; border:1px solid black;" width="100">
				<b>' . $main['poster'] . '</b><br>
				<img src="' . $main['avatar'] . '"><br>
				Posts: <b>' . $main['posts'] . '</b>
			</td>
			<td style="border:1px solid black;" width="450">
				Posted On: <i>' . $main['time'] . '</i><hr>
				' . $main['message'] . '
			</td>
		</tr>';
}

echo "</table>";

You were using the same user info for all of the replies.

#8 Chevy

Chevy
  • Members
  • PipPipPip
  • Advanced Member
  • 163 posts

Posted 20 August 2006 - 03:41 PM

Hmm I am getting this error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where reply.poster = users.username WHERE topicid='1' AND groupid='1'' at line 1

#9 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 August 2006 - 03:42 PM

Sorry, wrong SQL syntax.  Your query should be:

SELECT * FROM reply LEFT JOIN users ON reply.poster = users.username WHERE topicid='$topicid' AND groupid='$id'


#10 Chevy

Chevy
  • Members
  • PipPipPip
  • Advanced Member
  • 163 posts

Posted 20 August 2006 - 03:48 PM

Hey, Thank you a lot! You are very cool in my book now, :)

Thanks again!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users