管理面板脚本无法正常运行

时间:2011-08-29 18:39:06

标签: php mysql variables get pdo

脚本如下......

<?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>';
}
?>

我知道一个很长的脚本,但你需要看到它才有意义,除了一个小bug之外,脚本工作正常。

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

$delete = 'update';

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

第二个是另一个......

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的那个时。

我希望这是有道理的,我被困在做什么,我认为我的所有规则都是正确的,我看不出我的错误......

感谢您的耐心(以及很多!!!)

1 个答案:

答案 0 :(得分:1)

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

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

在以下行中,您将“更新”的值指定给$delete

elseif ($delete = 'update') {

相反,您应该检查$delete是否包含值'update':

elseif ($delete == 'update') {

您需要在多个位置进行此更改。