【问题标题】:Undefined index error when posting from HTML form从 HTML 表单发布时出现未定义的索引错误
【发布时间】:2016-08-25 20:17:31
【问题描述】:

我正在使用以下 html 表单进行登录:

<!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>

但显然这没有发布,因为我在 validateLogin.php 页面上收到未定义的索引错误:

<?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();
}
?>

我得到的错误出现在我设置 $user 和 $pass 变量的行中。我得到的错误是:

注意:未定义索引:第 41 行 C:\xampp\htdocs\PetTracker\validateLogin.php 中的用户

注意:未定义索引:在第 42 行传入 C:\xampp\htdocs\PetTracker\validateLogin.php

【问题讨论】:

标签: php html


【解决方案1】:

$_POST 变量的索引应该与 HTML 输入的name 属性完全相同。您输入字段的名称是Username,而您尝试以$_POST['user'] 访问它,这是完全错误的。 将名称更改为 user 或将 $_POST 索引更改为 Username

还要考虑检查您的代码,还有很多其他错误。例如,您需要在header 函数中将location 的第一个字母大写为Location。还可以考虑通过在 validateLogin.php 页面的第一行写入以下条件来检查回发

if($_SERVER['REQUEST_METHOD']=='POST'&&isset($_POST['submit'])){
//Your code
}else{
//redirect to somewhere else...
}

【讨论】:

  • 谢谢。您的回答是最完整的,并解释了为什么我应该更改索引。
  • 不客气。我也想知道为什么你在这个页面上创建一个新的连接不应该在其他地方创建它可以在整个项目中重复使用,而不是为所有需要数据库连接的页面一次又一次地创建它?
【解决方案2】:

Html 从 name 属性创建输入键。

解决方案 1:更改 HTML

<input type="text" required id="user" name="user" value="">

或解决方案2:更改PHP

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

【讨论】:

    【解决方案3】:

    PHP Notice of Undefined Index 表示在数组中找不到数组Key。如果您想查看数组的所有元素,可以使用 print_r($_POST);

    要解决您的问题,只需将 $_POST['user'] 更改为 $_POST['U​​sername'] 并将 $_POST['pass'] 更改为 $_POST['Password']; p>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      • 2011-03-16
      • 2015-07-05
      • 2018-09-11
      • 2014-06-01
      相关资源
      最近更新 更多