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");
}

// 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或重定向到 home.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']);

?>
4

2 回答 2

1

如果在数据库中找不到数据而不是重定向,则检查该查询是否给出结果

$result = mysql_query(...);

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

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

你不应该使用 MySQL 因为它已经贬值了,要么使用PDO要么mysqli

于 2012-12-02T14:10:18.260 回答
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
    }
?>
于 2012-12-02T14:13:39.317 回答