开发者

get url not working properly

开发者 https://www.devze.com 2023-03-17 11:09 出处:网络
I am working on the following code that is pulling information from a DB. This is what i want the code to do:

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.

If img=5, this referenced image does not belong but yet it shows up under the user, but not when i query the result.

If img=12, a ref image number that does not exists in the DB, I get an error instead of a redirect.

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());
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号