如果数据丢失,则重定向到页面

时间:2012-11-14 22:32:29

标签: php mysql

好的,所以我今天工作的时间太长了,而且我很困难(我认为)

function commonData($uid)
{
    if ($uid)
    {
            $sql = "
                    SELECT a.user_id, a.email, a.username, a.displayname, a.level_id, a.photo_id
                    FROM engine4_users AS a
                    WHERE a.user_id = ".$uid;
    }

    $UserInfo = @mysql_fetch_assoc(mysql_query($sql));

    if ($UserInfo[user_id])
    {

            if ($UserInfo[photo_id] && $UserInfo[photo_id]!="NULL")
            {
                    $PPhoto = @mysql_fetch_assoc(mysql_query("SELECT a.* FROM engine4_storage_files AS a WHERE a.file_id = ".$UserInfo[photo_id]));
                    $photo = SITE.$PPhoto[storage_path];
            }
            else $photo = NO_PHOTO; 

    $queryMoreProfile = mysql_query("SELECT * FROM engine4_user_fields_values AS a WHERE a.item_id = ".$UserInfo[user_id]);

    while ($moreProfile = @mysql_fetch_assoc($queryMoreProfile))
    {

            //$location = '';
            if ($moreProfile['field_id']==24)
            {
                    $locationNumber = $moreProfile['value'];
                    $locationsql = @mysql_fetch_assoc(mysql_query("SELECT a.* FROM engine4_user_fields_options AS a WHERE a.option_id = ".$locationNumber));

                    if (isset($locationsql['label']) && !empty($locationsql['label']))
                    {
                            $location = $locationsql['label'];
                    }
            }

            //if(empty($location))
            //{
            //      header("Location: http://www.fetishmen.net/members/edit/profile");
            //      exit;
            //}
}

我正在尝试做的是,如果$location没有值(完全从数据库中丢失)重定向到页面。我在这里做错了什么?

2 个答案:

答案 0 :(得分:2)

数组$moreProfile$locationsql的索引必须是变量或字符串,

if ($moreProfile['field_id']==24) //or $field_id
{
    $locationNumber = $moreProfile['value']; //or $value
    $locationsql = @mysql_fetch_assoc(mysql_query("SELECT a.* FROM engine4_user_fields_options AS a WHERE a.option_id = ".$locationNumber));
    $location = $locationsql['label']; //or $label 
}

if(empty($location))
{
    header("Location: http://website.com");
    exit;
}

编辑根据OP的评论,

  if ($moreProfile['field_id'] == 24 ) 
{
    $locationNumber = $moreProfile['value']; //or $value
    $locationsql = @mysql_fetch_assoc(mysql_query("SELECT a.* FROM engine4_user_fields_options AS a WHERE a.option_id = ".$locationNumber));
    $location = $locationsql['label']; //or $label 
} else if(empty($moreProfile['field_id'])) {
     header("Location: http://website.com");
     exit;
}

答案 1 :(得分:0)

你应该停止使用@并正确检查mysql_result和mysql_fetch_assoc是否正在返回一些东西。试试这个,看看它是否为你重定向:

$location = '';

if ($moreProfile['field_id']==24)
{
    $locationNumber = $moreProfile['value'];
    $locationsql = @mysql_fetch_assoc(mysql_query("SELECT a.* FROM engine4_user_fields_options AS a WHERE a.option_id = ".$locationNumber));

    if (isset($locationsql['label']) && !empty($locationsql['label'])) {
            $location = $locationsql['label'];
    }
}

if(empty($location))
{
    header("Location: http://website.com");
    exit;
}