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