无法添加用户

时间:2014-02-21 10:30:00

标签: php mysqli

我的记录未插入数据库?有人可以帮我这个吗?我真的很感谢谢谢你。

<?php
# user_add.php 
$connection = mysqli_connect('localhost', 'root', 'root', 'databasename');


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

    $firstname = $_POST['first_name'];
    $lastname = $_POST['last_name'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    mysqli_query($connection, "INSERT INTO users (first_name, last_name, email, password) VALUES ('$_POST[first_name]', 
         '$_POST[last_name]', '$_POST[email]', '$_POST[password]'");

    echo 'User has been added';
    mysqli_close($connection);

}

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <title></title>
    </head>
    <body>
        <div style="width: 500px; padding: 5px; margin: 0 auto;">
            <fieldset>
                <legend>Add User to the database</legend>
                    <form method="POST" action="user_add.php">
                First name: <br/> <input type="text" name="first_name"> <br/>
                Last name: <br/> <input type="text" name="last_name"> <br/>
                Email:  <br/> <input type="text" name="email"> <br/>
                Password:   <br/> <input type="text" name="password"> <br/>
                <input type="submit" value="Register" name="user_addition">
                    </form>
            </fieldset>
        </div>
    </body>
</html>

这是我的文件。再次感谢有人可以提供帮助。

2 个答案:

答案 0 :(得分:1)

您读取了$ _POST值,然后将这些值分配给变量,但您没有使用这些变量。你应该改变它。

$firstname = $_POST['first_name'];
$lastname = $_POST['last_name'];
$email = $_POST['email'];
$password = $_POST['password'];

mysqli_query($connection, "INSERT INTO users (first_name, last_name, email, password)   
VALUES ('$firstname', '$lastname', '$email', '$password')");

答案 1 :(得分:0)

您的查询中有错误。你错过了)。还要经常检查是否执行查询

mysqli_query($connection, "INSERT INTO users (first_name, last_name, email, password) VALUES ('$_POST[first_name]', 
         '$_POST[last_name]', '$_POST[email]', '$_POST[password]')") or die(mysqli_error($connection));