-3

This is my code: this is my file named signup.php where is the form to write

<html>
<head>
<title>Sign Up</title>
</head>
<body>
<form name="signup" metod="post" action="signup-connection.php">
Username: <input type="text" name="username" /> <br /><br />
Password: <input type="password" name="password" /> <br /><br />
<input type="submit" value="Sign Up"/>
</form>
</body>
</html>

Now this is my other file named signup-connection.php:

<html>
<head><title>Sign Up</title></head>
<body>

<?php

define('DB_NAME', 'crowdfunding');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
    die('Cant connect: ' .mysql_error());
}

$db_selected = mysql_select_db (DB_NAME, $link);

if (!$db_selected){
    die('Cant use ' . DB_NAME . ': ' . mysql_error());
}

if ( isset( $_POST["username"] ) ) {
    $username=$_POST["username"];
}
if ( isset( $_POST["password"] ) ) {
    $password=$_POST["password"];
}

$count = 0;

$query = mysql_query ("SELECT * FROM user WHERE username = '$username' "); 
if (mysql_num_rows($query) > 0){ 
    echo 'Sorry, the username \'' .$_POST['username'] . '\' is already taken!';
    $count += 1;
} 

if (strlen ($_POST['password']) < 6) { 
    echo 'Your password must be at least 6 characters';
    $count += 1;
}

if (empty($_POST) === false) {
    $required_fields = array ('username', 'password' );
    foreach ($_POST as $key => $value) { 
        if (empty($value) && in_array ($key, $required_fields) === true) { 
            echo 'Fields are required';
            $count += 1;
            break 1;
        }
    }
}

if ($count === 0) { 
    $sql = "INSERT INTO user(username, password) VALUES ('$username', '$password')"; 

    echo 'Everything OK!';
    //echo "<script> window.location.replace('login.php') </script>";   
}
/*else {
    echo "Try again!";
    echo "<script> window.location.replace('signup.php') </script>"; 
}*/
?>

</body>
</html>

I try to log in but there's 2 errors:

Notice: Undefined variable: username in C:\xampp\htdocs\projeto\BD\signup-connection.php on line 33

Notice: Undefined index: password in C:\xampp\htdocs\projeto\BD\signup-connection.php on line 39 Your password must be at least 6 characters

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Nov 30 '15 at 13:53
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Nov 30 '15 at 13:53
  • You really should use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). – Jay Blanchard Nov 30 '15 at 13:54
  • [Don't limit passwords](http://jayblanchard.net/security_fail_passwords.html). – Jay Blanchard Nov 30 '15 at 13:56
  • I think this issue was already posted here in stackoveflow. Check http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index – Suman Thapa Nov 30 '15 at 13:58
  • Please refer to http://stackoverflow.com/questions/2179520/whats-the-best-way-to-do-user-authentication-in-php – Timofey Nov 30 '15 at 14:01

1 Answers1

2

You are missing an 'h' on your method="post"

Change

<form name="signup" metod="post" action="signup-connection.php">

to

<form name="signup" method="post" action="signup-connection.php">
William Madede
  • 727
  • 4
  • 8
  • Thanks a lot mate ;) this is the first time i use stackoverflow, i always see other posts but never posted here. Yeah, problem solved!! Spend hours on this and it was just a letter missing – João Campos Nov 30 '15 at 14:03
  • No problem at all...please accept myAnswer...thanks man..am glad you sorted...done – William Madede Nov 30 '15 at 14:04