第一次使用预准备语句但查询未激活

时间:2017-01-23 22:40:51

标签: javascript php html mysql

我刚开始使用预备语句,因为它引起了另一位用户的注意。但是,在我开始实现它之后,它似乎没有正确地将新记录添加到数据库中。当我点击提交时,它只显示我已设置的注册成功div。

login.php(表单所在的位置)

<form id="signupform" class="form-horizontal" role="form" method="post">
    <div class="form-group">
        <label for="username" class="col-md-3 control-label">Username</label>
        <div class="col-md-9">
            <input type="text" class="form-control" name="username" placeholder="Username">
        </div>
    </div>

    <div class="form-group">
        <label for="email" class="col-md-3 control-label">Email</label>
        <div class="col-md-9">
            <input type="text" class="form-control" name="email" placeholder="Email Address">
        </div>
    </div>
    <div class="form-group">
        <label for="password" class="col-md-3 control-label">Password</label>
        <div class="col-md-9">
            <input type="password" class="form-control" name="password" placeholder="Password">
        </div>
    </div>

    <div class="form-group">
        <label for="firstname" class="col-md-3 control-label">First Name</label>
        <div class="col-md-9">
            <input type="text" class="form-control" name="firstname" placeholder="First Name">
        </div>
    </div>
    <div class="form-group">
        <label for="lastname" class="col-md-3 control-label">Last Name</label>
        <div class="col-md-9">
            <input type="text" class="form-control" name="lastname" placeholder="Last Name">
        </div>
    </div>

    <div class="form-group">
        <label for="age" class="col-md-3 control-label">Age</label>
        <div class="col-md-9">
            <input type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' maxlength="3" class="form-control" name="age" placeholder="Age">
        </div>
    </div>

    <div class="form-group">
        <label for="region" class="col-md-3 control-label">Region</label>
        <div class="col-md-9">
            <input type="text" class="form-control" name="region" placeholder="Region">
        </div>
    </div>

    <div class="form-group">
        <label for="address" class="col-md-3 control-label">Address</label>
        <div class="col-md-9">
            <input type="text" class="form-control" name="address" placeholder="Address">
        </div>
    </div>

    <div class="form-group">
        <label for="Postcode" class="col-md-3 control-label">Postcode</label>
        <div class="col-md-9">
            <input type="text" class="form-control" name="postcode" placeholder="Postcode">
        </div>
    </div>

    <div class="form-group">
        <label for="language" class="col-md-3 control-label">Language</label>
        <div class="col-md-9">
            <input type="text" class="form-control" name="language" placeholder="Language">
        </div>
    </div>

    <div class="form-group">
    <!-- Button -->                                        
        <div class="col-md-offset-3 col-md-9">
        <button id="btn-signup" type="submit" class="btn btn-info"><i class="icon-hand-right"></i>Submit</button>
         </div>
    </div>
</form>

JavaScript的:

        <script type="text/javascript">
        $(function() {
            $("#signupform").bind('submit',function() {
                var username = $('#username').val();
                var email = $('#email').val();
                var password = $('#password').val();
                var firstname = $('#firstname').val();
                var lastname = $('#lastname').val();
                var age = $('#age').val();
                var region = $('#region').val();
                var address = $('#address').val();
                var postcode = $('#postcode').val();
                var language = $('#language').val();
                $.post('scripts/register.php',{username:username, email:email, password:password, firstname:firstname, lastname:lastname, age:age, region:region, address:address, postcode:postcode, language:language}, function(data){
                    $('#signupsuccess').show();
                }).fail(function(){{
                    $('#signupalert').show();
                }});
                return false;
            });
        });
    </script>

register.php

<?php
include 'connection.php';
if($conn->connect_error){
    die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->prepare("INSERT INTO `users` (`ID`, `Username`, `Password`, `Email`, `First_Name`, `Last_Name`, `Age`, `Region`, `Address`, `Postcode`, `Language`) VALUES ('NULL', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?')");
$stmt->bind_param(NULL, $username, $password, $email, $firstName, $lastName, $age, $region, $address, $postcode, $language);

$username = $_POST['username'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$email = $_POST['email'];
$firstName = $_POST['firstname'];
$lastName = $_POST['lastname'];
$age = $_POST['age'];
$region = $_POST['region'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$language = $_POST['language'];
$stmt->execute();
$stmt->close();
?>

1 个答案:

答案 0 :(得分:1)

您从您认为有ID的元素(例如#username)中获取值,但您的输入都没有id="username"。您可以将ID添加到输入元素中,也可以更改选择器,例如$('[name="username"]')