ben
ben

Reputation: 11

what is this notice referring to

Notice: Undefined index: name in C:\wamp\www\espn.com\registration.php on line 12

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

Notice: Undefined index: password in C:\wamp\www\espn.com\registration.php on line 14 I'm sorry but the username you specified has already been taken. Please pick another one.

coding

<html>
<body>
<?php
$dbhost = "localhost";
$dbname = "users";
$dbuser = "root";
$dbpass = "*******";

mysql_connect ($dbhost,$dbuser,$dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

$name = $_POST['name'];
$username = $_POST['username'];
$password = md5($_POST['password']);

$checkuser = mysql_query("SELECT username FROM users WHERE username='$username'"); 

$username_exist = mysql_num_rows($checkuser);

if($username_exist > 1){
    echo "I'm sorry but the username you specified has already been taken.  Please pick another one.";
    unset($username);
    include 'registration.html';
    exit();
}

$query = "INSERT INTO users (name,username, password)
VALUES('$name','$username', '$password')";
mysql_query($query) or die(mysql_error());
mysql_close();

echo "You have successfully registered";
?>
</body>
</html>

Upvotes: 1

Views: 146

Answers (2)

Shoe
Shoe

Reputation: 76280

Basically the global variables $_POST['name'], $_POST['username'] and $_POST['password'] do not exist. I'd suggest you to check that each input of the form is submitted as a name that can be declared as follows:

<input name="username"/>

So that you can access the value of that input by $_POST['username']. After that the string that the script output (I'm sorry but the username you specified has already been taken. Please pick another one.) is echoed because your sql query looks like that:

SELECT username FROM users WHERE username=''

which will return every user with empty ('') username. Also as already suggested please fix the script as soon as possible.

Upvotes: 0

Vincent Savard
Vincent Savard

Reputation: 35927

It means $_POST['name'], $_POST['username'] and $_POST['password'] do not exist. Your form was most likely not submitted when you accessed registration.php (or the field names are wrong).

In PHP, you have to make sure every field you will use is set before you use it. You can use !empty or isset function.

Also, your script is vulnerable to SQL injections, take a look at mysql_real_escape_string function. And your condition is wrong : if you want to know if the username already exists, you should check if it is greater than 0, not 1. You can also use a COUNT(*) query instead of selecting a row.

Upvotes: 7

Related Questions