开发者

$_Post problems [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

what is this notice referring to

can i get an answer in plain English please i am new at this .. the better it is explained the fewer times i will need to repost

Notice: Undefined index: username in C:\wamp\www\espn.com\login.php on line 16

Notice: Undefined index: password in C:\wamp\www\espn.com\login.php on line 17

1<?php
2
3//Database Information
4
5 $dbhost = "localhost";
6 $dbname = "users";
7 $dbuser = "root";
8 $dbpass = "*****";
9
10 //Connect to database
11
12 mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
13 mysql_select_db($dbname) or die(mysql_error());
14
15 session_start();
****16 $username = $_POST['username'];
17 $password = md5($_POST['password']);****
18
19 $query = sprintf('SELECT * FROM users WHERE username="%s" A开发者_StackOverflowND password="%s"',
20    ($username), ($password));
21
22 $result = mysql_query($query);
23
if (mysql_num_rows($result) != 0) {
$error = "Bad Login";
    include "login.html";

} else {
    $_SESSION['username'] = "$username" ;
    include "memberspage.php";
}


Sure. The form that is submitted does not have any name tag. Please fix your html part as follow:

<input **name="username"**/>

So that you can easily use $_POST['username'] to recover the value from the form. Actually every time you see "undefined index" it means that a key of an array is missing. It's like:

$a = array('zero', 'one', 'two');

and you calling:

$a[3];

In this case $_POST is a global array which is missing the key 'username' and 'password'.


When you submit a page, the data (user input / form data) is sent as an array which you can access via $_POST.

Now, when you access $_POST[somename], the PHP engine tries to retrieve the value for the key "somename" in the array $_POST.

In your program (specifically lines 16 & 17), the key for the array does not exist.

So, it is better you do a check like this:

$username = isset($_POST['username'])?$_POST['username']:null;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜