So basically I have multiple-step form, which I handle through jQuery and AJAX request. Request goes through fine, but it seems like my PHP code doesn't work (it doesn't insert data into database table).
Here's my code jQuery AJAX code:
$(".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;
})
});
And here's my 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;
?>
It really seems like everything's alright and when I refresh the page (phpMyAdmin) there's 0 data in my table regardless I get message that everything went fine and from this point I don't know what I am doing wrong. I used clean_data function like this $godina = isset($_POST['godina']) ? clean_data($_POST['godina']) : null;
because I thought there's problem with it, but even after removing it my data is still not inserted.
CodePudding user response:
I will suggest you use a prepared statement and bind the values using execute. Change your query to something like this
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();
}