简体   繁体   English

PHP INSERT INTO 不会将数据插入数据库

[英]PHP INSERT INTO doesn't insert data into database

So basically I have multiple-step form, which I handle through jQuery and AJAX request.所以基本上我有多步表单,我通过 jQuery 和 AJAX 请求处理。 Request goes through fine, but it seems like my PHP code doesn't work (it doesn't insert data into database table).请求顺利通过,但似乎我的 PHP 代码不起作用(它没有将数据插入数据库表)。

Here's my code jQuery AJAX code:这是我的代码 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;
})

});

And here's my createticket.php :这是我的 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.看起来一切都很好,当我刷新页面(phpMyAdmin)时,我的表中有 0 个数据,不管我收到消息说一切都很好,从这一点开始我不知道我做错了什么。 I used clean_data function like this $godina = isset($_POST['godina']) ? clean_data($_POST['godina']) : null;我使用了这样的 clean_data 函数$godina = isset($_POST['godina']) ? clean_data($_POST['godina']) : null; $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.因为我认为它有问题,但即使删除它,我的数据仍然没有插入。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM