这是我用于查看用户个人资料的php

时间:2012-12-02 14:07:20

标签: php mysql

我的个人资料php

<?php
//profile.php

require_once 'includes/global.php';

//check to see if they're logged in
if(!isset($_SESSION['logged_in'])) {
    header("Location: login.php");
}

// finding user and viewing it
$tools = new FindUser();
$user = $tools->get($_REQUEST['userID']);

?>

这是我用于查看用户个人资料的php。

http://mywebsite.com/profile.php?userID=5以这种方式工作正常。

我希望我的代码检查用户是否在数据库中可用,例如,如果我添加?userID=10数据库中不存在它会发出mysql错误,或者即使我使用http://mywebsite.com/profile.php然后它也给出了错误。

所以现在我想如果用户在数据库中不可用,它应该提供该用户不可用,当我们使用简单http://mywebsite.com/profile.php时,它应该自动将其添加到userID=1或将其重定向到家。 PHP

如果还有其他方法,请告诉我。我是这个领域的新手

感谢您查看我的问题并回答:)

解决

<?php
//profile.php

require_once 'includes/global.php';

//check to see if they're logged in
if(!isset($_SESSION['logged_in'])) {
    header("Location: login.php");
}

$UserID = $_GET['userID'];
$CheckQuery = mysql_query("SELECT * FROM users WHERE id='$UserID'");

$CheckNumber = mysql_num_rows($CheckQuery);
if ($CheckNumber !== 1)
{
    header("Location: index.php");
}

// finding user and viewing it
$tools = new FindUser();
$user = $tools->get($_REQUEST['userID']);

?>

2 个答案:

答案 0 :(得分:1)

如果在数据库中找不到数据而不是重定向

,则检查查询是否给出结果
$result = mysql_query(...);

if(mysql_num_rows($result) !=1){ //

 header("Location:signup.php");
  exit();
}

你不应该使用MySQL因为它已被折旧,要么使用PDOmysqli

答案 1 :(得分:1)

您不应该使用MySQL因为它已被折旧,

如果你真的想使用MySQL你可以在脚本的开头查看用户ID的行数,例如:

<?
    $UserID = $_GET['UserID'];
    $UserID = mysql_real_escape_string($UserID);
    $CheckQuery = mysql_query("SELECT * FROM users WHERE userID='$UserID'");

    $CheckNumber = mysql_num_rows($CheckQuery);
    if ($CheckNumber !== 1)
    {

    // Do something If user is Not Found
    // Redirect to Another Page OR Something
    }
?>