我有这个使用模态插入数据的ajax函数,但是我在插入查询中传递数组值时遇到了问题。如何转换它以将多个信息传递给我的查询?
我在HTML中的输入文本框
<input type="text" class="form-control text-center" id="author_lname[]" name="author_lname[]" placeholder="Last Name" required>
<input type="text" class="form-control text-center" placeholder="First Name" id="author_fname[]" name="author_fname[]" required>
<input type="text" class="form-control text-center" id="author_mname[]" name="author_mname[]" placeholder="Middle Name / Initial" required>
Ajax功能
var getauthor_lname = $("#author_lname").val();
var getauthor_fname = $("#author_fname").val();
var getauthor_mname = $("#author_mname").val();
var whatprocess = "ADDBOOK";
$.ajax({
url: "adminfunctions.php",
method: "POST",
data: {getauthor_lname:getauthor_lname,
getauthor_fname:getauthor_fname,
getauthor_mname:getauthor_mname ,
whatprocess : whatprocess
},
success: function(data) {
var getdata = data.trim();
if (getdata == "SUCCESS") {
swal({
title: 'Success!',
text: '',
type: 'success',
confirmButtonClass: "btn btn-success",
buttonsStyling: false
}).then(function() {
$("#datatables").load(window.location + " #datatables");
});
}
else {
swal({
title: 'Sorry for the inconvenience!',
text: "There's a problem. Please contact the technical support for any concerns and questions.!",
type: 'error',
confirmButtonClass: "btn btn-danger",
buttonsStyling: false
}).catch(swal.noop)
}
},
error: function(jqXHR, exception) {
console.log(jqXHR);
}
});
PHP for INSERTING AUTHORS
$getauthor_lname = $_POST["getauthor_lname"];
$getauthor_fname = $_POST["getauthor_fname"];
$getauthor_mname = $_POST["getauthor_mname"];
for ($i = 0; $i < count($getauthor_fname); $i++) {
if ($getauthor_fname[$i] != "" && $getauthor_mname[$i] != "" && $getauthor_lname[$i] != "") {
$query = "INSERT INTO tbl_author (book_isbn, author_firstname, author_middlename, author_lastname) VALUES (? , ? , ? , ?)";
$stmt = $mysqlconnection->prepare($query);
$getauthor_lname[$i] = htmlspecialchars(strip_tags($getauthor_lname[$i]));
$getauthor_fname[$i] = htmlspecialchars(strip_tags($getauthor_fname[$i]));
$getauthor_mname[$i] = htmlspecialchars(strip_tags($getauthor_mname[$i]));
$stmt->bind_param("ssss", $getbook_isbn, $getauthor_fname[$i], $getauthor_mname[$i], $getauthor_lname[$i]);
$stmt->execute();
}else{
echo "ERRORauthor";
}
}
答案 0 :(得分:0)
我会将所有变量打包成一个数组并使用数据从AJAX调用中发送它,并在PHP层解包数组,我可以直接将值传递给INSERT语句并执行查询。我认为你已经在javascript层验证了你的数据,以避免数据损坏。希望如果您有更多问题,这会有所帮助或回复。
快速提问:为什么你的id和名字被设置为数组。我不认为这些是单个输入字段所必需的
var author_lname = $('#author_lname').val()
var author_fname = $('#author_fname').val()
var author_mname = $('#author_mname').val()
var whatprocess = 'ADDBOOK'
var entries = {'author_lname': author_lname, 'author_mname': author_mname, 'author_fname': author_fname, 'whatprocess': whatprocess}
var json = JSON.stringify(entries)
$.ajax({
url: 'functioncall.php',
method: 'POST',
data: { entry: entries },
success: function (data) {
console.log(data)
// var getdata = data.trim();
},
error: function (jqXHR, exception) {
console.log(jqXHR)
}
})
$author_fname = $author_info['author_fname'];
$author_mname = $author_info['author_mname'];
$author_lname = $author_info['author_lname'];
if ( !(empty($author_fname)) && !(empty($author_mname)) && !(empty($author_lname)) ) {
$query = "INSERT INTO tbl_author (book_isbn, author_firstname, author_middlename, author_lastname) VALUES (? , ? , ? , ?)";
$stmt = $mysqlconnection->prepare($query);
$stmt->bind_param("ssss", $getbook_isbn, $getauthor_fname[$i], $getauthor_mname[$i], htmlspecialchars(strip_tags($author_lname)));
$stmt->execute();
}
else{
echo "ERRORauthor";
}