PHP中的$ _POST不起作用

时间:2017-12-11 23:35:17

标签: php html mysql

我正在创建一个寄存器,数据库正在使用phpMyadmin,我已经查找了为什么POST不起作用,但我读到的所有内容都不适合我。我没有错误,我使用REQUEST而不是POST但仍然没有运气

require_once('config1.php');

if (isset($_POST) & !empty($_POST)) {
    $username = $_POST['username']; // you should really do some more logic to see if it's set first
    $email = $_POST['email'];
    $password = $_POST['password'];

    $sql = "INSERT INTO 'zz_login' (username, email, password) VALUES 
    ('$username', '$email', '$password')";
    $result = mysqli_query($conn, $sql);
    if ($result) {
        echo "user registation successful";
    } else {
        echo "user registeration failed";
    }
};

我已经创建了一个简单的表单

<form class="form-signin" action="" method="POST">

<input type="text" id="username" class="form-control" placeholder="Username" required autofocus>

 <span id="reauth-email" class="reauth-email"></span>
 <input type="email" id="email" class="form-control" placeholder="Email address" required autofocus>
 <input type="password" id="password" class="form-control" placeholder="Password" required>

 <button class="btn btn-lg btn-primary btn-block btn-signin" type="submit">Register</button>
 <button class="btn btn-lg btn-primary btn-block btn-signin" type="submit">Sign in</button>
 </form><!-- /form -->

2 个答案:

答案 0 :(得分:0)

您需要将name元素添加到所有字段。这就是$_POST['this']中的“this”:

<form class="form-signin" action="" method="POST">
    <input type="text" id="username" name="username" class="form-control" placeholder="Username" required autofocus>
    <span id="reauth-email" class="reauth-email"></span>
    <input type="email" name="email" id="email" class="form-control" placeholder="Email address" required autofocus>
    <input type="password" name="password" id="password" class="form-control" placeholder="Password" required>
    <button class="btn btn-lg btn-primary btn-block btn-signin" type="submit">Register</button>
    <button class="btn btn-lg btn-primary btn-block btn-signin" type="submit">Sign in</button>
</form>

另外请转义您的变量以防止SQL注入并进行某种脚本验证(javascript只会让您进行验证)。我也不确定你打算如何使用前面提到的php逻辑登录和注册选项。

PHP:

if ($_POST) {
    $username = isset($_POST['username']) ? $_POST['username'] : null;
    $email = isset($_POST['email']) ? $_POST['email'] : null;
    $password = isset($_POST['password']) ? $_POST['password'] : null;
    if (empty($username) || empty($email) || empty($password)) {
        echo "user registration failed - not all fields filled in!";
    } else {
        $sql = "INSERT INTO 'zz_login' (username, email, password) VALUES ('$username', '$email', '$password')";
        $result = mysqli_query($conn, $sql);
        if ($result) {
            echo "user registration successful";
        } else {
            echo "user registration failed";
        }
    }
};

答案 1 :(得分:0)

你应该在输入feild中尝试'name ='属性。 _POST变量仅适用于'name'属性而非'id'。