我正在尝试使用带有PDO连接的AJAX将数据提交到数据库,但有时候数据不会提交。我的AJAX响应是空的。
有时候我会--rpath=...
为空,如果我刷新页面并再次点击提交然后数据提交
另外,我收到一些错误。你能帮帮我吗?
$mailbody=$_POST['mailbody'];
PDO连接
<br />
<b>Fatal error</b>: Uncaught Error: Call to undefined method PDOStatement::bind_param() in C:\xampp\htdocs\fillter\process.php:23
Stack trace:
#0 C:\xampp\htdocs\demo\process.php(7): aus(Object(PDO))
#1 {main}
thrown in <b>C:\xampp\htdocs\demo\process.php</b> on line <b>23</b><br />
Process.php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbname";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
的Ajax
date_default_timezone_set('Asia/Kolkata');
$date_of_added= date('d-m-Y H:i:s');
switch($_GET['key']) {
case 'aus':aus($conn);break;
default : redirect('index.php');
}
function aus($conn){
$emailtemplate=$_POST['emailtemplate'];
$subject=$_POST['subject'];
$mailbody=$_POST['mailbody'];
$country="AUS";
$email_status=0;
global $date_of_added;
$sql ="INSERT INTO request(bulkemails, subjects, mailbody, country, email_status, date_of_send) VALUES (?,?,?,?,?,?)";
//echo $sql;
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssssis", $emailtemplate, $subject, $mailbody,$country, $email_status, $date_of_added);
$stmt->execute();
$stmt->close();
$conn->close();
}
HTML
$(function() {
$("form[name='bulkmailsend']").validate({
// Specify the validation rules
rules: {
emailtemplate:{
required: true,
},
subject: {
required: true}
},
submitHandler: function(form) {
//form.submit();
var emailtemplate = $('#emailtemplate').val();
var subject = $('#subject').val();
var mailbody = $('#editor11').val();
//alert(mailbody);
$.ajax(
{
url:'process.php?key=aus',
type:'POST',
data:{
'emailtemplate':emailtemplate,
'subject':subject,
'mailbody':mailbody
},
success:function(data)
{
alert(data);
},
});
}
});
});
答案 0 :(得分:0)
您使用MySQLi语法来绑定参数,但使用PDO语法连接到数据库。您收到错误Call to undefined method PDOStatement::bind_param()
的原因是因为该方法在PDO中不存在,而在MySQLi中存在(bind_param
)。使用PDO时,请检查绑定参数bindParam
的正确语法。
另外,请尝试相应地更改脚本。一种方法是使用命名占位符。
$sql ="INSERT INTO request(bulkemails, subjects, mailbody, country, email_status, date_of_send) VALUES (:mailTemplate,:subject,:mailbody,:country,:email_status,:date_of_added)";
//echo $sql;
$stmt = $conn->prepare($sql);
$stmt->bindParam(':mailTemplate', $emailtemplate, PDO::PARAM_STR);
$stmt->bindParam(':subject', $subject, PDO::PARAM_STR);
$stmt->bindParam(':mailbody', $mailbody, PDO::PARAM_STR);
$stmt->bindParam(':country', $country, PDO::PARAM_STR);
$stmt->bindParam(':email_status', $email_status, PDO::PARAM_INT);
$stmt->bindParam(':date_of_added', $date_of_added, PDO::PARAM_STR);
$stmt->execute();
// exiting
$stmt = null;
$conn = null;
另一种方法是使用问号占位符。
$sql ="INSERT INTO request(bulkemails, subjects, mailbody, country, email_status, date_of_send) VALUES (?,?,?,?,?,?)";
//echo $sql;
$stmt = $conn->prepare($sql);
$stmt->bindParam(1, $emailtemplate, PDO::PARAM_STR);
$stmt->bindParam(2, $subject, PDO::PARAM_STR);
$stmt->bindParam(3, $mailbody, PDO::PARAM_STR);
$stmt->bindParam(4, $country, PDO::PARAM_STR);
$stmt->bindParam(5, $email_status, PDO::PARAM_INT);
$stmt->bindParam(6, $date_of_added, PDO::PARAM_STR);
$stmt->execute();
// exiting
$stmt = null;
$conn = null;
答案 1 :(得分:0)
您可以尝试使用以下函数来绑定参数。
<?php
$sql = "INSERT INTO request(bulkemails, subjects, mailbody, country, email_status, date_of_send) VALUES (?,?,?,?,?,?)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(1, $emailtemplate, PDO::PARAM_STR);
$stmt->bindParam(2, $subject, PDO::PARAM_STR);
$stmt->bindParam(3, $mailbody, PDO::PARAM_STR);
$stmt->bindParam(4, $country, PDO::PARAM_STR);
$stmt->bindParam(5, $email_status, PDO::PARAM_STR);
$stmt->bindParam(6, $date_of_added, PDO::PARAM_STR);
?>