我正在尝试通过ajax将表单中的数据传递到后端php脚本以与数据库进行交互。运行verifyUN函数有效,它会检查是否存在没有类似用户并继续执行下一个函数create。不幸的是,create函数没有从它调用的php脚本中传回任何内容。运行两个不同的$ .post函数不起作用吗?我有什么简单的东西吗?
function verifyUN(username,password) {
$.post('verify.php', { login: username}, function(result) {
console.log("passed from ajax to verify, returned: "+ result);
if (result==0) {
create(username,password);
}
else if(result == 1) {
alert("Username already exists!"); //until a better error
}
else {
alert("Error:" +result);
}
});
}
此功能创建,不打印任何返回的数据。即使我粘贴了vertifyUN的内部,使它与verifyUN完全相同。然而,它仍然成功地触及数据库,因为它传递的数据,甚至将数据插入数据库。没有任何东西可以返回显示它是成功的......
function create(username,password){
console.log("This is inside create function");
$.post('create.php', { login: username, password: password}, function(returned) {
console.log("passed from create.php to verify, returned: "+ returned);
});
}
verify.php代码,通过verifyUN成功运行。
//needs cleaned
require '../database/info.php';
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error) {
die("connection not created: ". $conn->connect_error);
}
$username = htmlspecialchars($_POST['login']);
if($query = $conn->prepare("SELECT username FROM User WHERE username = ?") ) {
$query->bind_param("s", $username);
$query->execute();
$query->bind_result($un);
$query->fetch();
$query->close();
if ( $username != $un ) { //double checking
$result = 0; //doesn't exist, sends to ajax
}
else {
$result =1; //username exists, sends to ajax
}
echo json_encode($result);
}
else {
echo "query failed\n";
}
$conn->close();
?>
如果需要,create.php
require '../database/info.php';
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error) {
die("connection not created: ". $conn->connect_error);
}
// if (!isset($_POST['login'])) {
// $_POST['login'] = "testing";
// }
// if (!isset($_POST['password'])) {
// $_POST['password'] = "password";
// }
$username = htmlspecialchars($_POST['login']);
$password = htmlspecialchars($_POST['password']);
$result = 0;
//echo "first result: $result\n";
$q1 = "INSERT INTO User (username) VALUES (?)";
$q2 = "INSERT INTO Security (username, shpassword) VALUES (?, ?)";
if( $stmt = $conn->prepare($q1) ) {
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->close();
$result++;
}
else {
$result = 0;
echo json_encode("Username failed\n");
}
if ( $stmt = $conn->prepare($q2) ) {
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$stmt->close();
$result++;
}
else {
echo json_encode("Failed to add user and password to database");
}
echo json_encode($result);
$conn->close();
?>
提前致谢。
答案 0 :(得分:0)
我认为问题在于php脚本create.php
,而且很可能在于未声明变量$un
和$hash
两个sql语句是相关的,所以使用prepare
方法的返回值来继续处理或完全放弃 - 真正的代码应该/将比下面显示的更干净地执行它但你应该得到想法
<?php
$result = false;
$username = !empty( $_POST['login'] ) ? $_POST['login'] : false;
$password = !empty( $_POST['password'] ) ? $_POST['password'] : false;
if( !$username or !$password )exit('bad foo');
require '../database/info.php';
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error) die("connection not created: ". $conn->connect_error);
$q1 = "insert into `user` ( `username` ) values (?)";
$q2 = "insert into `security` ( `username`, `shpassword` ) values (?, ?)";
$stmt=$conn->prepare( $q1 );
if( $stmt ) {
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->close();
$stmt = $conn->prepare( $q2 );
if( $stmt ){
/* assumed algorithm here is default */
$hash=password_hash( $password, PASSWORD_DEFAULT );
$stmt->bind_param("ss", $username, $hash );
$stmt->execute();
$stmt->close();
} else {
exit('all is lost-abandon ship');
}
$conn->close();
} else {
exit('prepare failed');
}
?>