所以我试图使用PHP执行一个Insert语句,但是当我调用应该执行它的代码部分时,没有任何事情发生......
已经检查过,看看出了什么问题,但无法找到答案。
这是调用PHP代码的JavaScript函数。
function comparaSenhas(){
var pass = document.getElementById("pwd1").value;
var pass2 = document.getElementById("pwd2").value;
if(pass !== pass2){
return false;
}else{
return true;
}
}
function postData(){
var hr = new XMLHttpRequest();
var url = "../mysql.php";
var fstnm = document.getElementById("fn").value;
var lstnm = document.getElementById("ln").value;
var dtnasc = document.getElementById("dn").value;
var email = document.getElementById("em").value;
var senha = document.getElementById("pwd1").value;
var vars = "fname="+fstnm+"&lname="+lstnm+"&dt_nasc="+dtnasc+"&email="+email+"&senha="+senha;
if(comparaSenhas()){
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
alert(return_data);
}
}
hr.send(vars);
}
}
这是我正在使用的PHP代码。
<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "godienski";
$dbname = "web";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->prepare("insert into usuarios(first_name, last_name, data_nascimento, email, senha) values (?,?,?,?,?);");
$stmt->bind_param("sssss",$firstname,$lastname,$dtnascimento,$mail,$password);
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$dtnascimento = $_POST['dt_nasc'];
$mail = $_POST['email'];
$password = $_POST['senha'];
$stmt->execute();
$stmt->close();
$conn->close();
?>
</body>
</html>
有人可以帮忙吗?