get url not working properly
I am working on the following code that is pulling information from a DB.
This is what i want the code to do:
- A user logs in.
- If the image belongs to the user, after the db is queried only this image requested and found by the query that belongs to the user must show up.
- if the image number requested does not belong to the user, the page must either error out or redirect.
This is the code:
$id = mysql_real_escape_string(@$_GET['uid']); // get user ref #
$imgid = mysql_real_escape_string(@$_GET['img']); // get img ref #
$query = mysql_query("SELECT mypage.*, img.*
FROM img
JOIN mypage ON img.user = mypage.user WHERE mypage.id = '$id'");
if ((mysql_num_rows($query) == 0)) {
header("location:page.php?uid=$id");
die(mysql_error());
} else {
while($rows = mysql_fetch_array($query)) {
$img = $rows['img'];
$pfname = $rows['pfname'];
$plname = $rows['plname'];
$puser = $rows['puser'];
$description = $rows['description'];
$ppid_image_name = $rows['ppid_image_name'];
}
When I run the code, I am able to print out the correct result. The problem is the get URL part.
Let's say the URL is http://www/profile.php?uid=5&img=4...
img=4, the referenced image belongs to the user, so yes, this im开发者_如何学Cage shows up when call.
I seem to be mostly having problems with the conocation part of the url. &img=ref#
I'm not sure if the problm is here: if ((mysql_num_rows($query)==0))
.
I tried if ((mysql_num_rows($query)!=imgid)) redirect
, this doesn't work.
What am I doing wrong?
1) There is an error in your query , Use Left Join instead of Join
mysql_query("SELECT mypage.*, img.*
FROM img
LEFT JOIN mypage ON img.user = mypage.user WHERE mypage.id = '$id'");
2) Test this :
while($rows = mysql_fetch_array($query)) {
$img = $rows['img'];
$pfname = $rows['pfname'];
$plname = $rows['plname'];
$puser = $rows['puser'];
$description = $rows['description'];
$ppid_image_name = $rows['ppid_image_name'];
}
if (!isset($img)) {
header("location:page.php?uid=$id");
die(mysql_error());
精彩评论