PHP INSERT INTO 没有将数据插入数据库
PHP INSERT INTO doesn't insert data into database
所以基本上我有多步表单,我通过 jQuery 和 AJAX 请求处理。
请求顺利进行,但我的 PHP 代码似乎不起作用(它没有将数据插入数据库 table)。
这是我的代码 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) 时,我的 table 中的数据为 0,尽管我收到消息说一切正常,从那时起我不知道自己是什么做错了。我使用了 clean_data 这样的函数 $godina = isset($_POST['godina']) ? clean_data($_POST['godina']) : null;
因为我认为它有问题,但即使在删除它之后我的数据仍然没有插入。
我建议您使用准备好的语句并使用 execute 绑定值。将您的查询更改为类似这样的内容
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();
}
所以基本上我有多步表单,我通过 jQuery 和 AJAX 请求处理。 请求顺利进行,但我的 PHP 代码似乎不起作用(它没有将数据插入数据库 table)。
这是我的代码 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) 时,我的 table 中的数据为 0,尽管我收到消息说一切正常,从那时起我不知道自己是什么做错了。我使用了 clean_data 这样的函数 $godina = isset($_POST['godina']) ? clean_data($_POST['godina']) : null;
因为我认为它有问题,但即使在删除它之后我的数据仍然没有插入。
我建议您使用准备好的语句并使用 execute 绑定值。将您的查询更改为类似这样的内容
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();
}