如何将$ _POST变量发布?

时间:2011-12-01 03:41:50

标签: php html post

这是我的第一页,也就是我的登录页面。用户输入用户名,点击登录,然后broswer应该将他们带到login.php Index.html

<html>
<head>
<title>Login</title>
</head>
<body >

<br><br><br><br><h2 align=center>Quiz Taker</h2>
<br><br><br>
<p align=center></p>

<br>
    <form method="post" action="login.php">
        <table class="login" cellpadding=10 cellspacing=0 align="center" bgcolor="#cccccc" border="3">
        <tr>
            <td>
            <input type="radio" name="userType" value="Student"/> Student
            <input type="radio" name="userType" value="Teacher"/> Teacher
             <td>
             Username:</td><td><input type="text" name="username" size=10>
             </td>
        </tr>
        <tr>
             <td colspan=2 align="center">
             <input type="submit" name="submit" value="  Log In  ">
             </td>
        </tr>
    </table>
    </form>


</body>
</html>

当用户点击提交时,它将转到login.php,如下所示:

<?php

include_once("DatabaseConnection.php");

$stmt=$DBH->prepare("SELECT * FROM QuizUser where Name=?")
$stmt->bindValue(1, $_POST['username']);
$stmt->execute();
if ($row=$stmt->fetch())
    {
        $ID=$row['ID'];
    }
    else
    {

        $stmt=$DBH->prepare("INSERT INTO QuizUser(Name) VALUES(?)")
        $stmt->bindValue(1, $_POST['username']);
        $stmt->execute();
        $ID=$DBH->lastInsertId();
    }
    $_SESSION['userID']= $ID;
    $_SESSION['userName']= $_POST['username'];
    $_SESSION['userType']=$_POST['userType'];

    if($_POST['userType'] == "Student")
    {
        header($string["Location:student.php"]);
    }
    else 
    {
        header($string["Location:teacher.php"]);    
    }

    ?>

哪个不起作用,我无法弄清楚原因。它只是弹出一个解析错误:语法错误,.... myfile中的意外T_VARIABLE ....在第一次尝试使用$ _POST变量时。顺便说一下,我在login.php顶部导入的DataBaseConnection.php中开始我的会话。

3 个答案:

答案 0 :(得分:2)

你错过了

中的分号
$stmt=$DBH->prepare("SELECT * FROM QuizUser where Name=?")

$stmt=$DBH->prepare("INSERT INTO QuizUser(Name) VALUES(?)")

答案 1 :(得分:1)

您的header函数写错了。

header($string["Location:student.php"]);

应该是

header("Location: student.php");

同样地,

header($string["Location:teacher.php"]);

应该是

header("Location: teacher.php");

答案 2 :(得分:1)

这里

include_once("DatabaseConnection.php");

你最好用

require_once("DatabaseConnection.php");