使用AJAX调用PHP文件时,mysql无法执行 - 错误

时间:2016-10-23 14:02:57

标签: php jquery mysql ajax

不知道这里的问题是什么。可能是简单的东西,但也可能在这里得到它。我有一个表单在一个简单的网站上注册用户,我用AJAX将信息发送到register.php,所有值都正确发送。表单和AJAX如下所示:

        <form method="GET">
        <h4>Användarnamn:</h4>
        <input type="text" class="username" placeholder="Användarnamn" name="reg-user">
        <h4>Välj lösenord:</h4>
        <input type="text" class="password" placeholder="Lösenord" name="reg-pass">
        <h4>Förnamn</h4>
        <input type="text" class="firstname" placeholder="Förnamn" name="reg-fn">
        <h4>Efternamn</h4>
        <input type="text" class="lastname" placeholder="Efternamn" name="reg-en"><br><br>
        <div class="btn btn-success reg-btn">Registrera</div>
    </form>
</div>

<script type="text/javascript">
    $(".reg-btn").click(function(){
        var username  = $(".username").val();
        var password  = $(".password").val();
        var firstname = $(".firstname").val();
        var lastname  = $(".lastname").val();

    //Ajax call
    $.ajax({
        method:"GET",
        url: "php/register.php",
        data:{ username: username, password: password, firstname: firstname, lastname: lastname },
        success: function(){
        $(".username").val("");
        $(".password").val("");
        $(".firstname").val("");
        $(".lastname").val("");
        $("#slide-register").prepend("<p>Användare registrerad.</p>");
}});
})

当此信息发送到php时,它会尝试注册用户,现在没有出现任何错误..:

<?php
   mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
   include('connect.php');

   if(isset($_GET['username']) && isset($_GET['password'])){
    $uname  = mysqli_real_escape_string($conn, $_GET['username']);
    $pword  = password_hash(mysqli_real_escape_string($conn, $_GET['password']), PASSWORD_DEFAULT);
    $fname = mysqli_real_escape_string($conn,$_GET['firstname']);
    $lname  = mysqli_real_escape_string($conn,$_GET['lastname']);

    $username  = trim($uname);
    $password  = trim($pword);
    $firstname = trim($fname);
    $lastname  = trim($lname);
    $admin = "0";

        $query  = "SELECT username FROM users WHERE username ='$username'";
        $result = mysqli_query($conn, $query);

        $count  = mysqli_num_rows($result);

        if($count == 0){
            $stmt = mysqli_prepare($conn, "INSERT INTO users (username, password, firstname, lastname, admin) VALUES (?, ?, ?, ?, ?)");
            mysqli_stmt_bind_param($stmt, "sss", $username, $password, $firstname, $lastname, $admin);
            mysqli_stmt_execute($stmt);
        }
        else {
            $msg  = "Användarnamn upptaget.. ";
        }
   }
        mysqli_close($conn);
?>

好吧,当执行ajax调用时,没有任何事情发生,无法看到任何语法错误,但也许你可以。先谢谢。

我收到这些错误:

Warning: mysqli_stmt_bind_param(): Number of elements in type definition string doesn't match number of bind variables in /customers/5/0/5/xxxxxx/php/register.php on line 24

Fatal error: Uncaught mysqli_sql_exception: No data supplied for parameters in prepared statement in /customers/5/0/5/xxxxx/php/register.php:25
Stack trace:
#0 /customers/5/0/5/xxxxx/php/register.php(25):    mysqli_stmt_execute(Object(mysqli_stmt))
#1 {main}
thrown in /customers/5/0/5/xxxxxxx/php/register.php on line 25

1 个答案:

答案 0 :(得分:0)

解决了:

此行不正确,因为我没有使用足够的字符来绑定值。

mysqli_stmt_bind_param($stmt, **"sss"**, $username, $password, $firstname, $lastname, $admin);

应该像:

mysqli_stmt_bind_param($stmt, **"sssss"**, $username, $password, $firstname, $lastname, $admin);

PLUS

中的$ conn

 mysqli_real_escape_string(-->$conn<--, $_GET['password']);

失踪了。