Page 1 of 2

link restriction help

Posted: Thu Mar 10, 2005 3:13 am
by pleigh
i have here a code that restricts the edit link from appearing if it is not my thread or post

Code: Select all

$uid = $_SESSION['userID'];
	
	$query = "SELECT userID FROM posts WHERE userID='$uid'";
	$result = @mysql_query($query) or die(mysql_error());	
	if (mysql_fetch_assoc($result))
	{
		echo '<a href=reportedit.php class=under>EDIT</a>';
	}
	else
	{
		NULL;
	}
i am comparing the userID of the post with the session key...if they match, the edit link will appear....but this is not happening....the edit link appears even if it is not thread or post....any help from you guys will be greatly appreciated...


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Thu Mar 10, 2005 3:20 am
by bobaf3tt
try this...

Code: Select all

$uid = $_SESSION['userID'];
	
	$query = "SELECT userID FROM posts WHERE userID='$uid'";
	$result = @mysql_query($query) or die(mysql_error());	
	if (mysql_fetch_assoc($result))
	{
		echo '<a href="reportedit.php" class="under">EDIT</a>'; //added quotes
	}
	else
	{
		NULL;
	}

Posted: Thu Mar 10, 2005 3:21 am
by JayBird
try changing

Code: Select all

if (mysql_fetch_assoc($result))
to

Code: Select all

if (mysql_num_rows($result) != 0) {
Althogh, from what i can gather, it seems a very inefficient way of doing things running that query for every post you want to output

Posted: Thu Mar 10, 2005 3:22 am
by pleigh
your post is just the same except your comment....what should i add?

Posted: Thu Mar 10, 2005 3:27 am
by JayBird
pleigh wrote:your post is just the same except your comment....what should i add?
He added quotes, but it wont make a difference to your problem

Posted: Thu Mar 10, 2005 3:30 am
by bobaf3tt
Yea, just realized ur's is a logic problem...umm yea...

Posted: Thu Mar 10, 2005 3:31 am
by pleigh
yeah...thanks....so any more suggestion??i've been thinkin of this for 3 hours already...hehehe...what can you expect from a newbie? :lol:

Posted: Thu Mar 10, 2005 3:36 am
by JayBird
did you try my suggestion?

Posted: Thu Mar 10, 2005 3:44 am
by pleigh
yes pimp....just returned the same result....

Posted: Thu Mar 10, 2005 3:50 am
by JayBird
thinking about it, you code will always return true because as long as the current user has made a post, the edit link will appear on ALL posts, regardless whether it is their post ot not

Posted: Thu Mar 10, 2005 3:51 am
by pleigh
what do you suggest pimp..???

Posted: Thu Mar 10, 2005 4:38 am
by bobaf3tt
Are you comparing the userid of the session with a specific post, as in going through each post individually and checking....if so you'd need to check the posts userID against the $_SESSION userID and filter only the current post your checking. All the code you have is doing is checking if the user has any posts, and if they do, displays a link.
Try this:

Code: Select all

$uid = $_SESSION['userID'];
	
	$query = "SELECT userID, postID FROM posts WHERE userID='$uid', postID='$postID'";
	$result = @mysql_query($query) or die(mysql_error());	
	if(mysql_fetch_assoc($result) != 0){
		echo '<a href="reportedit.php" class="under">EDIT</a>';
	} 
	else {
		NULL;
	}
You probably have an equivalent of postID in your DB. Or mebbe this isn't what your even trying to do? :lol:

Posted: Thu Mar 10, 2005 4:42 am
by pleigh
nope....not effective... :(

Posted: Thu Mar 10, 2005 4:44 am
by bobaf3tt
Did you just cut and paste? I wasn't providing cut+paste code, just showing you that the query you originally had gets all posts from a specific user. You probably need to add more restrictions to the query.

Posted: Thu Mar 10, 2005 4:47 am
by pleigh
i did that code before you post it....it never worked the way i like it....