douzhushen_9776 2016-04-30 19:20
浏览 75
已采纳

从HTML表单发布时未定义的索引错误

I am using the following html form to get a login:

<!DOCTYPE html>   
<!-- This is a html file for logging in to the virtual adviser -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
    <head>
        <meta charset="UTF-8">
        <title> Pet Tracker Login</title>
        <link rel="stylesheet" type="text/css" media='screen' href="css/login.screen.css">
    </head>
    <body>
        <!-- The div that displays the login form-->
        <div class="login">
            <h1><strong>Welcome to the Pet Tracker System</strong></h1>
            <form action="validateLogin.php" method="POST">
            <fieldset>
            <label for="user">Username</label>
            <p><input type="text" required id = "user" name = "Username" value=""></p>
            <label for="pass">Password</label>
            <p><input type="password" required id = "pass" name = "Password" value=""></p>
            <!-- <p><a href="#">Forgot Password?</a></p> -->
            <p><input type="submit" value="Login"></p>
            </fieldset>
            </form>
            <!-- include the  logo with green background -->
            <img src="images/threeDeePawPrint.png" alt="paw Logo" style="width:194px;height:97px" class = "center">
        </div>
    </body>
</html>

but apparently this is not posting because I get an undefined index error on the validateLogin.php page:

<?php 
// start the session for this page and create the array to hold error messages
session_start();
$errmsg_arr = array();
$errflag = false;

$username = 'root';
$password = '';
$url = 'localhost';
$database = 'pet_tracker';

/*  Note that above variables are using single quote for string. When they
    get replaced in the connection statement below, single quotes within
    single quotes will fail, therefore, the string argument in $conn= statement
    must be double quotes
*/

try
{
    $conn = new PDO("mysql:host=$url; dbname=$database",$username,$password);       //create PDO object (PHP Data Objects = PDO)
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);                 /*set the attribute that controls the error mode once the database
                                                                                    has been connected to, so that it throws exceptions (PDO switches to
                                                                                    "silent failure" mode after establishing a successful connection) */
    $conn->exec('SET NAMES "utf8"');                                                    /* PDO has a method exec that runs SQL scripts. Configure the character
                                                                                    encoding to UTF-8 for special characters like smart quotes */

}   
catch (PDOException $e)
{
    echo $e;
    $output = 'Unable to connect to the database server.'.                          //the '.' is the concatenation operator for a string
    $e->getMessage();                                                               //the '->' is the equivalent of the dot operator in Java
    include 'error.html.php';
    exit();
}

//get the username and password posted from the login page (index.php)
if(isset($_POST['user'])) echo 'index user has value'.$_POST['user'];
if(isset($_POST['pass'])) echo 'index user has value'.$_POST['pass'];

$user = $_POST['user'];
$pass = $_POST['pass'];


//query the database for the posted data from form
    $result = $conn->prepare("SELECT * FROM client WHERE username= :un AND password= :pw");
    $result->bindParam(':un', $user);
    $result->bindParam(':pw', $pass);
    $result->execute();
    $rows = $result->fetch(PDO::FETCH_NUM);
    if($rows > 0) 
    {
    $result = $conn->prepare("SELECT * FROM client WHERE username = :un");  //PDO can only handle a row of data at a time?? Cannot select first_name from students, etc.??
    $result->bindParam(':un',$user);
    $result->execute();
    $name = $result->fetchColumn(1);    
    $_SESSION['name'] = $name;
    $_SESSION['user'] = $user;          //the next page employee.php will need the username to get information from the database
    header("location: employee.php");
    }
else{
    $errmsg_arr[] = 'Username and Password are not found';
    $errflag = true;
    }

if($errflag) {
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    session_write_close();
    header("location: index.php");
    exit();
}
?>

The errors I get are in the lines where I set the $user and $pass variables. The errors I get are:

Notice: Undefined index: user in C:\xampp\htdocs\PetTracker\validateLogin.php on line 41

Notice: Undefined index: pass in C:\xampp\htdocs\PetTracker\validateLogin.php on line 42

  • 写回答

3条回答 默认 最新

  • dongying6896 2016-04-30 19:26
    关注

    Index of $_POST variable should be exactly same as the name attribute of the HTML Input. Name of your input field is Username while you are trying to access it as $_POST['user'] which is entirely wrong. Either change the name to user or the $_POST index to Username

    Also consider reviewing your code, there are bunch of other mistakes as well. Just for instance, you need to capitalize the first letter of location as Location in the header function. Also consider checking the post-back by writing the following condition on the first line of validateLogin.php page

    if($_SERVER['REQUEST_METHOD']=='POST'&&isset($_POST['submit'])){
    //Your code
    }else{
    //redirect to somewhere else...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探