所以基本上我有多步表單,我通過 jQuery 和 AJAX 請求處理。請求順利通過,但似乎我的 PHP 代碼不起作用(它沒有將資料插入資料庫表)。
這是我的代碼 jQuery AJAX 代碼:
$(".submit").click(function(){
var request;
//moj kod
event.preventDefault();
// Abort any pending request
if (request) {
request.abort();
}
// setup some local variables
var $form = $(this);
// Let's select and cache all the fields
var $inputs = $form.find("input, select, button, textarea");
// Serialize the data in the form
var serializedData = $form.serialize();
// Let's disable the inputs for the duration of the Ajax request.
// Note: we disable elements AFTER the form data has been serialized.
// Disabled form elements will not be serialized.
$inputs.prop("disabled", true);
// Fire off the request to createticket.php
request = $.ajax({
url: "createticket.php",
type: "post",
data: serializedData
});
// Callback handler that will be called on success
request.done(function (response, textStatus, jqXHR){
// Log a message to the console
console.log("Request uspje?no poslat!");
});
// Callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
// Log the error to the console
console.log(JSON.stringify(errorThrown));
console.error(
"Desio se sljedeci error: "
textStatus, errorThrown
);
});
// Callback handler that will be called regardless
// if the request failed or succeeded
request.always(function () {
// Reenable the inputs
$inputs.prop("disabled", false);
});
//moj kod ->end
return false;
})
});
這是我的 createticket.php :
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
function clean_data($data)
{
/* trim whitespace */
$data = trim($data);
$data = htmlspecialchars($data);
return $data;
}
$make = isset($_POST['make']) ? $_POST['make'] : null;
$model = isset($_POST['model']) ? $_POST['model'] : null;
$godina = isset($_POST['godina']) ? $_POST['godina'] : null;
$engine = isset($_POST['engine']) ? $_POST['engine'] : null;
$termin = isset($_POST['termin']) ? $_POST['termin'] : null;
$usluga = isset($_POST['usluga']) ? $_POST['usluga'] : null;
$user_id = isset($_POST['user_id']) ? $_POST['user_id'] : null;
$request = "U obradi";
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);
$sql = "INSERT INTO appointments (vl_id, make, model, godina, engine, opis, vrijeme, request) VALUES (?,?,?,?,?,?,?,?)";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
}
catch (PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
看起來一切都很好,當我重繪 頁面(phpMyAdmin)時,我的表中有 0 個資料,無論我收到訊息說一切都很好,從這一點開始我不知道我做錯了什么。我使用了這樣的 clean_data 函式,$godina = isset($_POST['godina']) ? clean_data($_POST['godina']) : null;因為我認為它有問題,但即使洗掉它,我的資料仍然沒有插入。
uj5u.com熱心網友回復:
我建議您使用準備好的陳述句并使用執行系結值。將您的查詢更改為這樣的內容
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$sql = "INSERT INTO appointments (make, model, godina, engine, opis, vrijeme, request) VALUES (?,?,?,?,?,?,?)";
$stmt = $conn->prepare($sql);
$stmt->execute([$make, $model, $godina, $engine, $opis, $vrijeme, $request]);
echo "New record created successfully";
}
catch (PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/478984.html
上一篇:如何在跨度和輸入之間同步輸入?
