PHP / SQL禁止状态不回显

时间:2018-08-07 00:53:20

标签: php html sql pdo

我创建了一个Banned.php页面,所以说用户被禁止了,只要他们被禁止,他们就会重定向到banned.php页面!

问题是我将status设置为1很好,但禁令Reason不会为该用户回声

bans的SQL表具有这些表usernamereason

代码:

<?php 
        //Check if the user is banned
        $SQL = $odb -> prepare("SELECT `status` FROM `users` WHERE `username` = :username");
        $SQL -> execute(array(':username' => $username));
        $status = $SQL -> fetchColumn(0);
        if ($status == 1){
            $ban = $odb -> query("SELECT `reason` FROM `bans` WHERE `username` = '$username'") -> fetchColumn(0);
            if(empty($ban)){ $ban = "No reason given."; }
            $error = 'You are banned. Reason: '.htmlspecialchars($ban);
        }                           ?>

如果您可以提供帮助,我尝试了error_reporting,但由于无法获取错误而无法显示原因/从SQL中抢夺

4 个答案:

答案 0 :(得分:1)

select p.ProgramID, min(p.sunday) as StartSunday, dateadd(d,-1,p2.sunday) as EndSaturday 
from prog p
join prog p2 on p2.ProgramID=p.ProgramID and p2.status=0 and p2.sunday>p.sunday
and not exists(select * from prog p3 where p3.ProgramID=p.ProgramID and p3.status=0 and p3.sunday>p.sunday and p3.sunday<p2.sunday)
where p.status=1
group by p.programID, p2.Sunday
union
select ProgramID, Sunday, null 
from prog p
where status=1 
and not exists(select * from prog p2  where p2.ProgramID=p.ProgramID and p2.status=0 and p2.sunday>p.sunday)

然后$sql = "SELECT reason FROM bans WHERE username LIKE '$username' LIMIT 1"; $result = $odb->query($sql); $ban = $result->fetchColumn();

$ban = $result->fetchColumn()[0];可能不太容易混淆,因此$stmt通常用于查询字符串。

$sql至少可以与fetchColumn()一起使用,而不清楚使用哪个驱动程序。

答案 1 :(得分:0)

示例数据库表

(运行代码段以查看“格式化”表。)

<table border="1px solid black;">
  <tr>
    <td>UserID</td>
    <td>Username</td>
    <td>Status</td>
    <td>Reason</td>
  </tr>
  <tr>
    <td>51414</td>
    <td>FelixCrystal</td>
    <td>0</td>
    <td>System exploitation.</td>
  </tr>
</table>


示例代码

$Server = "localhost";
$Username = "myDBUsername";
$Password = "myDBPassword";
$dbName = "myDBName";
$Connect = MySQLi_Connect($Server, $Username, $Password, $dbName);
$SQL = "SELECT Status,Reason FROM Users WHERE UserID='51414'";
$Result = MySQLi_Query($this->Link, $SQL);
// Maybe 'If ($Result->num_rows == 0)' would be better to avoid duplicate accounts (Which shouldn't happen for ID based tables anyways)
If ($Result->num_rows > 0) {
    While ($Row = $Result->fetch_assoc()) {
        $Status = $Row["Status"];
        $Reason = $Row["Reason"];
    }
    If ($Status == 0 /* Or whatever your 'banned' value is */) {
        Header("Location: https://www.example.com/MyBannedPage.php?Reason=$Reason");
        /*
            I used a GET request for this example. You can use anything you'd like such as GET, POST, Sessions, Cookies, etc.
        */
    } Else {
        // Account is active!
    }
} Else {
    Echo "Error looking up account.";
}

MyBannedPage.php

<?php
    If (Isset($_GET["Reason"])) {
        $Reason = $_GET["Reason"];
    } Else {
        $Reason = "Banned reason not supplied.";
    }
    Echo "Your account has been banned. Reason: $Reason";
?>

答案 2 :(得分:0)

好吧,我设法显示了Reason,但是当从面板输入状态时,它使用INSERT未更新,因此不显示最新的禁止原因,如此处所示:http://prntscr.com/kfq2pi gggggggg是旧禁令,Banned as a test是新禁令,但它一遍又一遍地插入了我尝试使用UPDATE但无济于事的代码

代码:             if ($status != $_POST['status']){ $SQL = $odb -> prepare("UPDATE个用户SET状态= :status WHERE ID = :id"); $SQL -> execute(array(':status' => $_POST['status'], ':id' => $id)); $status = htmlspecialchars($_POST['status']); $reason = htmlspecialchars($_POST['reason']); $SQLinsert = $odb -> prepare('INSERT INTO禁令VALUES(:username, :reason)'); $SQLinsert -> execute(array(':username' => $username, ':reason' => $reason)); $update = true; $updateMsg = "Users status updated from {$status} to {$_POST['status']} with Reason: {$_POST['reason']} "; }

答案 3 :(得分:-1)

SQL查询: SELECT * FROM users where where status ='1'AND username =:username