我使用PHP和Jquery将数据保存到MySQL数据库的形式很简单。它适用于我的本地开发环境,但在推送生产时,它会向db表添加字段,但它不会保存任何输入数据(名称,电子邮件,电话)。在本地一切都很好,它保存所有信息。哪里可能有问题?这也是保存数据的安全方法吗?我错过了一些额外的输入字段检查吗?
这是jQuery部分:
$('.js-form').submit(function() {
$.ajax({
type: "POST",
data: $(this).serialize(),
url: "process.php",
success: function (response) {
alert('form success');
},
error: function (response) {
alert('form error');
}
});
doSomethingAfterForm();
return false;
});
and process.php
<?php
include_once('connection.php');
if (mysqli_connect_errno()) :
echo "Failed to connect to database";
endif;
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
if (!empty($_SERVER['HTTP_CLIENT_IP'])) :
$ip = $_SERVER['HTTP_CLIENT_IP'];
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) :
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
else :
$ip = $_SERVER['REMOTE_ADDR'];
endif;
$name = mysql_real_escape_string($name);
$email = mysql_real_escape_string($email);
$phone = mysql_real_escape_string($phone);
$ip = mysql_real_escape_string($ip);
$sql = "INSERT INTO entries (name, email, phone, ip) VALUES ('$name', '$email', '$phone', '$ip')";
if (!mysqli_query($con,$sql)) :
die('Error');
endif;
mysqli_close($con);
?>
Connection.php
<?php
$db_host = 'localhost';
$db_username = '******';
$db_pass = '*******';
$con = mysqli_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysqli_select_db($con, "my") or die ("no database");
?>
PHP错误日志显示:
PHP警告:mysql_real_escape_string():指向服务器的链接 没有在/
中建立
答案 0 :(得分:0)
<form>
<input type="text" id="name" name="name">
<input type="text" id="email" name="email">
<input type="text" id="phn" name="phone">
<input type="submit" value="submit" id="submit">
</form>
<script>
$(document).ready(function(){
$("#submit").click(fucntion(e){
$.ajax({
type: "POST",
data: { name : $("#name").val(),email: $("#email").val()
, phone: $("#phn").val()},
url: "process.php",
success: function (response) {
alert('form success');
},
error: function (response) {
alert('form error');
}
});
doSomethingAfterForm();
e.preventDefault();
});
});
</script>
这是您的解决方案。使用它会起作用。