【问题标题】:Admin panel script not functioning properly管理面板脚本无法正常运行
【发布时间】:2011-08-29 18:39:06
【问题描述】:

脚本如下...

<?php

// ########## GAME UPDATE FORM ##########

if (isset($_GET['tab']) && $_GET['tab'] === 'gameupdates') {

$delete = 'update';

if (isset($_SESSION['success'])) {

    echo $_SESSION['success'];
    unset($_SESSION['success']);
}

echo '<p>To make a new game update fill out the form below along with the type of update it corresponds to.</p><p>If you have completed a task make sure to remove it from the <a href="/adminpanel?tab=tasklist">task list</a>.</p><form action="/adminpanel?tab=process" method="post">
<fieldset>
<input type="hidden" name="newupdate" />
<textarea name="message"></textarea>
<select name="type">
    <option value="Community">Community</option>
    <option value="Security">Security</option>
    <option value="Gameplay">Gameplay</option>
    <option value="Account">Account</option>
    <option value="Miscellaneous">Misc.</option>
</select>
<input id="submit" type="submit" value="Insert Update" />
</fieldset>
</form><p>The last six updates were as follows from newest to oldest:</p><p>(To delete an entry hover over the update and press delete)</p>';

$q = $dbc -> prepare ("SELECT * FROM updates ORDER BY date_time DESC LIMIT 0, 6");
$q -> execute();

    while ($update = $q -> fetch(PDO::FETCH_ASSOC)) {

        echo '<div id="displaybox"><div><strong>' . $update['date_time'] . '</strong><span>' . $update['type'] . '</span></div><p>' . $update['message'] . '</p><a href="/adminpanel?tab=process&update=' . $update['date_time'] . '">Delete</a></div>';
    }
}

// ########## TO DO LIST ##########

elseif (isset($_GET['tab']) && $_GET['tab'] === 'tasklist') {

$delete = 'task';

if (isset($_SESSION['success'])) {

    echo $_SESSION['success'];
    unset($_SESSION['success']);
}

echo '<p>This is the to do list anything that needs to be done whether it be urgent or not put it here.</p><p>Remember once the task has been completed remember to add it to the <a href="/adminpanel?tab=gameupdates">game updates</a>.</p><p>Below (if any) are the current tasks that need doing, to add a new task complete the form at the bottom of this page.</p>';

$q = $dbc -> prepare ("SELECT * FROM tasks ORDER BY date_time");
$q -> execute();

    while ($task = $q -> fetch(PDO::FETCH_ASSOC)) {

        echo '<div id="displaybox"><div><strong>' . $task['date_time'] . '</strong><span>' . $task['type'] . '</span></div><p>' . $task['message'] . '</p><a href="/adminpanel?tab=process&task=' . $task['date_time'] . '">Delete</a></div>';
    }

echo '<p>To add a task complete the form below, along with which type of update it is.</p><p>To delete a task once it has been completed hover over the task and press delete.</p><form action="/adminpanel?tab=process" method="post">
<fieldset>
<input type="hidden" name="newtask" />
<textarea name="message"></textarea>
<select name="type">
    <option value="Community">Community</option>
    <option value="Security">Security</option>
    <option value="Gameplay">Gameplay</option>
    <option value="Account">Account</option>
    <option value="Miscellaneous">Misc.</option>
</select>
<input id="submit" type="submit" value="Insert New Task" />
</fieldset>
</form>';
}

// ########## PROCESS ADMIN CHANGES ##########

elseif (isset($_GET['tab']) && $_GET['tab'] === 'process') {

// ########## GAME UPDATE SUCCESS ##########

if (isset($_POST['newupdate'])) {

    if ($_POST['message'] === '') {
        header('Location:/adminpanel?tab=gameupdates');
        exit();
            }

    $q = $dbc -> prepare("INSERT INTO updates (message, type, date_time) VALUES (?, ?, ?)");
    $q -> execute(array($_POST['message'], $_POST['type'], date("y/m/d : H:i:s", time())));
    $_SESSION['success'] = '<p><strong>Successfully added a new game update.</strong></p>';
    header('Location:/adminpanel?tab=gameupdates');
    exit();
}

// ######### TO DO LIST SUCCESS ##########

elseif (isset($_POST['newtask'])) {

    if ($_POST['message'] === '') {
        header('Location:/adminpanel?tab=tasklist');
        exit();
    }

    $q = $dbc -> prepare("INSERT INTO tasks (message, type, date_time) VALUES (?, ?, ?)");
    $q -> execute(array($_POST['message'], $_POST['type'], date("y/m/d : H:i:s", time())));
    $_SESSION['success'] = '<p><strong>Successfully added a new task.</strong></p>';
    header('Location:/adminpanel?tab=tasklist');
    exit();
}

// ########## GAME UPDATE DELETE ##########

elseif ($delete = 'update') {

    $q = $dbc -> prepare("DELETE FROM updates WHERE date_time = ?");
    $q -> execute(array($_GET['update']));
    $_SESSION['success'] = '<p><strong>Successfully deleted the selected update.</strong></p>';
    header('Location:/adminpanel?tab=gameupdates');
    exit();
}

// ########## TO DO LIST DELETE ##########

elseif ($delete = 'task') {

    $q = $dbc -> prepare("DELETE FROM tasks WHERE date_time = ?");
    $q -> execute(array($_GET['task']));
    $_SESSION['success'] = '<p><strong>Successfully deleted the selected task.</strong></p>';
    header('Location:/adminpanel?tab=tasklist');
    exit();
}

else {

    header('Location:/adminpanel');
    exit();
}
}

// ########## ADMIN CONTROL PANEL ##########

else {
echo '<p>This is the admin panel. Click on an option below to begin.</p><p><a href="/adminpanel?tab=gameupdates">Add/Remove A Game Update</a></p><p><a href="/adminpanel?tab=tasklist">Add/Remove A Task From List</a></p>';
}
?>

我知道一个很长的脚本,但是您需要全部看完才能理解,除了一个小错误之外,该脚本运行良好。

你会注意到第一个 if 语句有一个变量

$delete = 'update';

这意味着如果$_GET['tab'] = gameupdates$delete = 'update'

第二个 elseif 不一样...

if $_GET['tab'] = tasklist, $delete = 'task'

在前两个 if 和 else if 语句中,有包含类似链接的 while 循环

<a href="adminpanel?tab=process&update OR task=' . some variable . '">Delete</a>

现在,如果链接在 $_GET['gameupdates'] 中,则更新链接,如果在 $_GET['tasklist'] 中,则更新任务。

$_GET['tab'] = process 的第三个 elseif 语句中,前两个语句用于表单提交,但最后两个 elseif 语句是我解释的链接的用途。

现在,如果有人在$_GET['tab'] = updates$delete = update 中单击“删除”链接。所以 elseif $_GET['tab'] = process 中的第二个 elseif 语句会触发,它确实会触发。

问题是,如果有人在$_GET['tab'] = tasklist 中单击链接,然后是$delete = task。所以 elseif 语句 $_GET['tab'] = process 中的第三个 elseif 语句应该作为 $delete = task 触发。

相反,更新总是触发,并且查询失败,您会注意到在查询之后,标头会重定向到前一页,其中包含确认操作的会话。当我单击$_GET['tab'] = tasklist 中“删除”中的链接时,它会触发更新的链接,而它应该触发$_GET['tab'] = tasklist 的链接。

我希望这是有道理的

感谢您的耐心等待(非常感谢!!!)

【问题讨论】:

    标签: php mysql variables get pdo


    【解决方案1】:

    您的代码引用了 $_SESSION 变量,但从未启动会话 (session_start())。您需要在脚本顶部使用 session_start() 才能访问会话数据。

    此外,'=' 是一个赋值运算符。您需要一个相等运算符 ('==')。

    在以下行中,您将 'update' 的值分配给 $delete

    elseif ($delete = 'update') {
    

    相反,您应该检查$delete 是否包含值“更新”:

    elseif ($delete == 'update') {
    

    您需要在多个地方进行此更改。

    【讨论】:

    • 我试过你的答案,但没有用,然后我决定使用会话,以及你的答案,它有效,可以解释一下,以供将来参考。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多