link restriction help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

link restriction help

Post 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]
bobaf3tt
Forum Newbie
Posts: 7
Joined: Thu Mar 03, 2005 12:43 am
Contact:

Post 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;
	}
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

your post is just the same except your comment....what should i add?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
bobaf3tt
Forum Newbie
Posts: 7
Joined: Thu Mar 03, 2005 12:43 am
Contact:

Post by bobaf3tt »

Yea, just realized ur's is a logic problem...umm yea...
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post 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:
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

did you try my suggestion?
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

yes pimp....just returned the same result....
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

what do you suggest pimp..???
bobaf3tt
Forum Newbie
Posts: 7
Joined: Thu Mar 03, 2005 12:43 am
Contact:

Post 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:
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

nope....not effective... :(
bobaf3tt
Forum Newbie
Posts: 7
Joined: Thu Mar 03, 2005 12:43 am
Contact:

Post 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.
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

i did that code before you post it....it never worked the way i like it....
Post Reply