doufei3152 2016-02-16 16:40
浏览 117
已采纳

PHP和MySQL; 如果带有查询变量的语句不起作用

I have been having some problem getting the header() to initialize with this code. It seems that the if statement doesn't work with query variables?

In init.php the function $user_data is defined. $url is the $_GET code in the url-bar of the browser (in this case a 6 digit random code). The variables seem to work, since I tried to output them already. The problem seems to be the if statement though. I do not get an error message. The header() just does not initiate, even though I am not logged in and the url is set to public === 0 in MySQL. Where am I going wrong?

include 'core/init.php';
include 'includes/head.php';
$url = $_SERVER['QUERY_STRING'];
$url = sanitize($url);

$public_arr = mysql_query("SELECT `public` FROM `uploads` WHERE `url` = '$url' AND `active` = 1") or die(mysql_error());
$public_arr = mysql_fetch_assoc($public_arr);
$public = $public_arr['public'];

$owner_arr = mysql_query("SELECT `owner` FROM `uploads` WHERE `url` = '$url' AND `active` = 1") or die(mysql_error());
$owner_arr = mysql_fetch_assoc($owner_arr);
$owner = $owner_arr['owner'];

global $user_data;
if ($public === 0 AND $owner !== $user_data['username'] || logged_in() === false) {
    header('Location: mainpage.php');
    exit();
}

$name_arr = mysql_query("SELECT `name` FROM `uploads` WHERE `url` = '$url' AND `active` = 1") or die(mysql_error());
$name_arr = mysql_fetch_assoc($name_arr);
$name = $name_arr['name'];
  • 写回答

2条回答 默认 最新

  • dongzhila3786 2016-02-16 17:34
    关注

    Assuming the variable values are as you say they are, try checking these vlaues are equal to and not identical, some times the types can vary (integers like 0 may be strings like "0").

    if ($public == 0 AND $owner != $user_data['username'] || logged_in() === false) {
        header('Location: mainpage.php');
        exit();
    }
    

    As @jay-blanchard says, you should be using MySQLi functions instead of mysql_* for many reasons - the main that they're not supported as standard in PHP any more!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line