无法在php中创建注册表单

时间:2015-11-30 13:51:14

标签: php

这是我的代码:     这是我的文件名为signup.php,其中是要写的表单

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

现在这是我的另一个名为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>

我尝试登录但有2个错误:

  

注意:未定义的变量:第33行的C:\ xampp \ htdocs \ projeto \ BD \ signup-connection.php中的用户名

     

注意:未定义的索引:第39行的C:\ xampp \ htdocs \ projeto \ BD \ signup-connection.php中的密码   您的密码必须至少为6个字符

1 个答案:

答案 0 :(得分:2)

你错过了一个&#39; h&#39;在你的方法=&#34;发布&#34;

更改

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

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