使用if和else语句进行PHP UPDATE

时间:2013-08-23 18:20:00

标签: php javascript mysql

我需要对我的脚本提供一点帮助,我正在制作一个图书馆登录/退出和页面,我让它工作,以便在他们登录和退出时发布表单,但我想将其转到哪里,如果他们正试图退出,他们实际上从未登录过它会弹出一个js警告告诉他们,我无法得到它。

以下是代码:

<?php
    session_start();
    include_once("connect.php");
    date_default_timezone_set("America/Winnipeg");
    $date = ("m-d-Y");
    $timeout = date("g:i:s a");

    //search for existing entries
    if ("SELECT EXISTS(SELECT * FROM signin_out WHERE
    lname='".$_POST['lastname']."' AND fname='".$_POST['firstname']."' 
    AND date='".$date."')") {

        //if they exist run this
        mysql_query("UPDATE signin_out SET timeout='" . $timeout . "' 
            WHERE lname='" . $_POST['lastname'] . "' 
            AND fname='" . $_POST['firstname'] . "' AND timeout='' ");
        header("Location: ../index.html");

        //if they don't exist run this
    } else {
        header("Location: ../index.html");
        echo "<script type='text/javascipt'>\n";
        echo "alert('You did not sign in!');\n";
        echo "</script>";
   }
?>

1 个答案:

答案 0 :(得分:0)

HTTP Location header早在JavaScript ../index.html被删除之前就已经将它们发送到echo。您需要有一个logout.php页面,其中包含该JavaScript或您重定向到的页面的$_GET变量,并在那里触发警报框。

例如:

...
} else {
    header('Location: ../index.php?notloggedin');
}

index.php中的某个地方:

<?php
if(isset($_GET['notloggedin'])) {
    echo '<script>alert("You did not sign in!");</script>';
}
?>

虽然我发现alert框是相当干扰的,但我宁愿这样做:

<?php
if(isset($_GET['notloggedin'])) {
    echo '<p><strong>You did not sign in!</strong></p>';
}
?>