У меня есть сценарий ajax, который собирает значения из входных данных, а затем вставляет в базу данных MySQL, используя MYSQLI
,
Когда я нажимаю кнопку, уникальный идентификатор возвращается успешно, но ни одно из значений записи не вставляется в базу данных. Также при тестировании значений записей по отдельности они также не отображаются при достижении опубликованного сценария php.
После выполнения этого уникальный идентификатор находится в базе данных, но никакая другая информация не сохраняется.
Q. How come the variables not reaching the PHP, thus preventing me from a successful insertion to the database?
Аякс
$('#postData').click(function() {
smallVan_getForm_values();
$.ajax({
type: "POST",
dataType: "json",
url: "//xxxxx",
data: JSON.stringify({
book_email:book_email,
book_clientTitle : book_clientTitle,
book_firstname : book_firstname,
book_lastname :book_lastname,
book_dateofbirth : book_dateofbirth,
book_primarycontactnumber : book_primarycontactnumber,
book_secondarycontactnumber : book_secondarycontactnumber,
departing_first_lineaddress : departing_first_lineaddress,
departing_second_lineaddress : departing_second_lineaddress,
departing_postcode : departing_postcode,
destination_first_lineaddress : destination_first_lineaddress,
destination_second_lineaddress : destination_second_lineaddress,
destination_postcode : destination_postcode,
amount : amount,
book_consignmentdate : book_consignmentdate,
book_timeofbooking : book_timeofbooking,
book_additionalnotes : book_additionalnotes
}),
contentType: "application/json; charset=utf-8",
success: function (result) {
// alert(result);
if (result.status == "success"){
uniqueID = result.uniqueID;
alert(uniqueID);
}
}
});
});
});
//AJAX END
PHP
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{ //if request made from localserver then proceed here
insertData();
}function insertData() {
$uniqueID = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYXZ", 10)), 0, 10);$conn=mysqli_connect("xxxx","xxx","xxxxx^","xxxxx");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
mysqli_query($conn,"INSERT INTO bookings_table ( booking_uniqueID , book_email , book_clientTitle , book_firstname , book_lastname , book_dateofbirth , book_primarycontactnumber , book_secondarycontactnumber , departing_first_lineaddress , departing_second_lineaddress , departing_postcode , destination_first_lineaddress , destination_second_lineaddress , destination_postcode , amount , book_consignmentdate , book_timeofbooking , book_additionalnotes )
VALUES ( '$uniqueID' , '$_POST[book_email]' , '$_POST[book_clientTitle]' , '$_POST[book_firstname]' , '$_POST[book_lastname]' , '$_POST[book_dateofbirth]' , '$_POST[book_primarycontactnumber]' , '$_POST[book_secondarycontactnumber]' , '$_POST[departing_first_lineaddress]' , '$_POST[departing_second_lineaddress]' , '$_POST[departing_postcode]' , '$_POST[destination_first_lineaddress]' , '$_POST[destination_second_lineaddress]' , '$_POST[destination_postcode]' , '$_POST[amount]' , '$_POST[book_consignmentdate]' , '$_POST[book_timeofbooking]' , '$_POST[book_additionalnotes]' )");
mysqli_close($conn);
echo json_encode(array('status' => 'success', 'uniqueID' => $uniqueID));
}
просто удалите свой JSON.stringify ().
data: {
book_email:book_email,
book_clientTitle : book_clientTitle,
book_firstname : book_firstname,
book_lastname :book_lastname,
book_dateofbirth : book_dateofbirth,
book_primarycontactnumber : book_primarycontactnumber,
book_secondarycontactnumber : book_secondarycontactnumber,
departing_first_lineaddress : departing_first_lineaddress,
departing_second_lineaddress : departing_second_lineaddress,
departing_postcode : departing_postcode,
destination_first_lineaddress : destination_first_lineaddress,
destination_second_lineaddress : destination_second_lineaddress,
destination_postcode : destination_postcode,
amount : amount,
book_consignmentdate : book_consignmentdate,
book_timeofbooking : book_timeofbooking,
book_additionalnotes : book_additionalnotes
},
Других решений пока нет …