我在控制台中获得了正确的日志,但它没有进入数据库我看不出我的PHP有什么问题。任何人都可以看到可能出现的问题吗?
JQuery代码:
$( "#dialog-form" ).dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
buttons: {
"Create an account": function() {
var fname = $( "#guestfname" ).val();
var lname = $( "#guestlname" ).val();
var gender = $( "#guestgender" ).val();
var address = $( "#guestaddress" ).val();
var city = $( "#guestcity" ).val();
var state = $( "#gueststate" ).val();
var zip = $( "#guestzip" ).val();
var phone = $( "#guestphone" ).val();
var email = $( "#guestemail" ).val();
var dob = $( "#guestdob" ).val();
var dataString ={fname:fname, lname:lname, gender:gender, address:address,
city:city, state:state, zip:zip, phone:phone, email:email,
dob:dob};
console.log(dataString);
$.ajax({
type: "POST",
url: "classes/add_guest.php",
data: dataString,
cache: false,
success: function(html)
{
$('.guestinfo').html(html);
}
});
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
}
});
$( "#create-user" )
.button()
.click(function() {
$( "#dialog-form" ).dialog( "open" );
});
});
PHP mySQL查询代码:
<?php
//open connection
require_once('../config/db.php');
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
//get date
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$gender = $_POST['gender'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$dob = $_POST['dob'];
mysqli_query($con, 'INSERT INTO guests(fname, lname, gender, address, city, state, zip, phone, email, dob)
VALUES("'.$fname.'","'.$lname.'","'.$gender.'","'.$address.'","'.$city.'","'.$state.'",'.
$zip.','.$phone.',"'.$email.'",'.$dob.'');
mysqli_close($con);
?>
答案 0 :(得分:1)
有一个小问题,字符串在传递给MySQL服务器之前必须单引号:
mysqli_query($con, 'INSERT INTO guests(fname, lname, gender, address, city, state, zip, phone, email, dob)
VALUES("'.$fname.'","'.$lname.'","'.$gender.'","'.$address.'","'.$city.'","'.$state.'",'.
$zip.','.$phone.',"'.$email.'",'.$dob.'');
=&GT;语法很好:
mysqli_query($con, "INSERT INTO guests(fname, lname, gender, address, city,
state, zip, phone, email, dob)
VALUES('$fname', '$lname', '$gender', '$address', '$city',
'$state', '$zip', '$phone', '$email', '$dob'");
答案 1 :(得分:1)
首先,您的查询字符串不需要一起添加,PHP可以选择哪些变量。所以试试这个
mysqli_query($con, "INSERT INTO guests (fname, lname, gender, address, city, state, zip, phone, email, dob) VALUES ('$fname','$lname','$gender','$address','$city','$state','zip','$phone','$email','$dob')";
如果不起作用,请将error_reporting(E_ALL);
添加到页面顶部,尝试将所有$_POST
更改为$_REQUEST
。然后转到包含URL中的变量的页面(GET方法)。这应该告诉你错误。例如website.com/add_guest.php?lname=Dave
答案 2 :(得分:1)
首先,你没有逃避任何事情。或使用准备好的陈述。您好,Bobby Tables(如果您愿意,可以使用SQL注入)。
其次,您没有在引号中将您的值包装在语句中。例如,$phone
没有被包裹。我不认为MySQL会非常友善地
INSERT INTO table(phone) VALUES(123-867-5309)
所以我们需要引用你所有的字符串
INSERT INTO table(phone) VALUES("123-867-5309")
第三,你的AJAX发生了什么?您是否使用类似Firebug的东西来确保您的AJAX正确发送?你听说过serialize吗?发布表单数据的更简单方法。
答案 3 :(得分:0)
感谢大家的帮助。不过我们都错过了。 mysqli_query($con, "INSERT INTO guests(fname, lname, gender, address, city,
state, zip, phone, email, dob)
VALUES('$fname', '$lname', '$gender', '$address', '$city',
'$state', '$zip', '$phone', '$email', '$dob')");
忘记)
关闭VALUES
。