gpt4 book ai didi

javascript - 将结果从 pdo 发送到 ajax 时遇到问题

转载 作者:行者123 更新时间:2023-11-30 00:08:09 26 4
gpt4 key购买 nike

我想要做的是,如果用户成功注册,pdo 将提供信息并将其发送到 ajax,如果用户注册与否,ajax 将发送消息。在我将这个条件放入我的 pdo 中后,它工作正常,现在它不会再插入,并且 ajax 告诉“注册用户时出错!”一直以来。

脚本:

<script type="text/javascript">
$(document).ready(function() {

$('#submit').click(function (e) {
e.preventDefault();

var data = {};
data.name = $('#name').val();
data.age = $('#age').val();
data.gender = $('#gender').val();
data.address = $('#address').val();
data.image = $('#imgInp').val();


$.ajax({
type: "POST",
url: "user.php",
data: data,
cache: false,
success: function (response) {
if (Number(response) == 1)
{
alert("User successfully registered");
}
else
{
alert("Error registering user!");
}
}
});
return false;
});

});
</script>

用户.php:

<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "test";

$dbc = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$name = @$_POST['name'];
$age = @$_POST['age'];
$address = @$_POST['address'];
$gender = @$_POST['gender'];
$imageName = @$_FILES['image']['name'];


$q = "INSERT INTO students(name, age, address, gender, imageName ) VALUES(:name, :age, :address, :gender, :image)";

$query = $dbc->prepare($q);
$query->bindParam(':name', $name);
$query->bindParam(':age', $age);
$query->bindParam(':address', $address);
$query->bindParam(':gender', $gender);
$query->bindParam(':image', $imageName);


$results = $query->execute();
$results ? echo "1"; : echo "2"; ;
?>

最佳答案

您似乎有错误:

$results ? echo "1"; : echo "2"; ;

yours demo

尝试这样:

echo  $results ? "1" : "2";

working demo

你可以看到here教程。

关于javascript - 将结果从 pdo 发送到 ajax 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24333277/

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