我的个人资料 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']);
?>
最佳答案
你不应该使用 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
}
?>
关于php - 这是我用于查看用户个人资料的 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13670031/