我不明白为什么我的代码不工作。我只是从表单中取值,我想用JSON和AJAX将它们插入到数据库中。有什么我做错了吗?发送JSON到位于服务器上的PHP文件
$(document).ready(function() {
$("#insert").click(function() {
var email = $("#email").val();
var password = $("#password").val();
var name = $("#name").val();
var bio = $("#bio").val();
var postData = {"email":email,"password":password,"name":name,"bio":bio};
$.ajax({
type: "POST",
dataType: "json",
url: "http://**************/php-code/insert.php",
data: {myData:postData},
crossDomain: true,
cache: false,
beforeSend: function() {
$("#insert").val('Connecting...');
},
success: function(data) {
if (data == "success") {
alert("inserted");
$("#insert").val('submit');
} else if (data == "error") {
alert("error");
}
}
});
return false;
});
});
和PHP文件,该文件在服务器上坐:
include "db.php";
if(isset($_POST['myData'])) {
$email=$_POST['email'];
$password=$_POST['password'];
$name=$_POST['name'];
$bio=$_POST['bio'];
$q=mysqli_query($con,"INSERT INTO 'user' ('email','password','name', 'bio') VALUES ('$email','$password','$name','$bio')");
if($q)
echo "success";
else
echo "error";
}
你有包括jquery吗? –
[Little Bobby](http://bobby-tables.com/)说*** [你的脚本存在SQL注入攻击风险。](http://stackoverflow.com/questions/60174/how-can- ***)了解[MySQLi](http://php.net/manual)[准备](http://en.wikipedia.org/wiki/Prepared_statement)声明/en/mysqli.quickstart.prepared-statements.php)。即使[转义字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! [不要相信它?](http://stackoverflow.com/q/38297105/1011527) –
**不要存储纯文本密码!**请使用PHP的[内置函数](http:// jayblanchard。 net/proper_password_hashing_with_PHP.html)来处理密码安全性。如果您使用的PHP版本低于5.5,则可以使用'password_hash()'[兼容包](https://github.com/ircmaxell/password_compat)。确保你*** [不要越狱密码](http://stackoverflow.com/q/36628418/1011527)***或在哈希之前使用其他任何清理机制。这样做*更改密码并导致不必要的附加编码。 –