php表单未提交到MySQL数据库

时间:2015-12-17 16:21:15

标签: php html mysql forms

我正在制作一个注册表格。以下是我的代码。我的数据库名称是"用户"并具有以下字段:id,name,email&密码。当我提交表格回复时,你已经注册了!"但没有任何东西插入数据库。我是php和SQL的新手。如果有人可以帮我解释为什么我的表单没有向数据库提交数据。

<?php

if (isset($_POST['submit'])) {

    $error = "";

    if (!$_POST['userName'])
        $error .= "<br/>Please enter your name";

    if (!$_POST['userEmail'])
        $error .= "<br/>Please enter your email";
    else if (!filter_var($_POST['userEmail'], FILTER_VALIDATE_EMAIL))
        $error .= "<br/>Please enter a valid email address";

    if (!$_POST['userPassword'])
        $error .= "<br/>Please enter your password";
    else {
        if (strlen($_POST['userPassword']) < 8)
            $error .= "<br/>Please enter a password with minimum 8 characters";
        if (!preg_match('`[A-Z]`', $_POST['userPassword']))
            $error .= "<br/>Please include a capital letter in your password";
    }

    if ($error)
        echo "There were error(s) in your signup details: " . $error;
    else {
        $link = mysqli_connect("localhost", "username", "password", "database");

        if (!$link) {
            echo "Failed.";
        } else {
            $query = "SELECT * FROM `users` WHERE email='$_POST[userEmail])'";
            $result = mysqli_query($link, $query);
            $results = mysqli_num_rows($result);

            if ($results)
                echo "That email address already exists. Do you want to log in? ";
            else {
                $query = "INSERT INTO users (name, email, passsword) VALUES(
                    '$_POST[userName]', '$_POST[userEmail]', '$_POST[userPassword]')";
                mysqli_query($link, $query);
                echo "You've been signed up!";
            }
        }
    }
}
?>
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Sign Up</title>
    </head>
    <body>
    <h1>Sign Up</h1>
    <form method="post">
        <label for="userName">Name</label>
        <input type="text" id="userName" name="userName" placeholder="Name"/>
        <label for="userEmail">Email address</label>
        <input type="email" id="userEmail" name="userEmail" placeholder="Email"/>
        <label for="userPassword">Password</label>
        <input type="password" id="userPassword"
            name="userPassword" placeholder="Password"/>
        <input type="submit" id="submit" name="submit" placeholder="Sign Up"/>
    </form>
    </body>
    </html>

3 个答案:

答案 0 :(得分:0)

替换你的其他人
else {
     $uname = $_POST["userName"];
     $email = $_POST["userEmail"];
     $password = $_POST["userPassword"];
     $query = "INSERT INTO users (name, email, passsword) VALUES('$uname', '$email', '$password')";
     mysqli_query($link, $query) or die(mysqli_error($link));
     echo "You've been signed up!";     
}

答案 1 :(得分:0)

您的INSERT查询应如下所示:

$query="INSERT INTO users (name, email, passsword) VALUES('$userName', '$userEmail', '$userPassword')";

答案 2 :(得分:0)

我检查了你的代码。

Error : 1.) It was in select Query : placed extra ')' right parenthesis. 
2.) It was in Insert Query : spelling of password was passsword.

更新:只需更改以下查询。

$query = "SELECT * FROM `student` WHERE email='$_POST[userEmail]'";

$query = "INSERT INTO student (name, email, password) VALUES(
                    '$_POST[userName]', '$_POST[userEmail]', '$_POST[userPassword]')";