gpt4 book ai didi

php - AJAX 调用 PHP 文件的问题,mysql 不执行 - 有错误

转载 作者:行者123 更新时间:2023-11-30 22:10:41 24 4
gpt4 key购买 nike

真的不知道这里有什么问题。可能是一些简单的东西,但也可以在这里发布。我有一个在简单网站上注册用户的表格,我使用 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

最佳答案

已解决:

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

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

应该是这样的:

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

加上

$conn 在

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

失踪了。

关于php - AJAX 调用 PHP 文件的问题,mysql 不执行 - 有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40203765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com