Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用jQuery POST插入数据库_Php_Jquery_Json - Fatal编程技术网

Php 使用jQuery POST插入数据库

Php 使用jQuery POST插入数据库,php,jquery,json,Php,Jquery,Json,我在使用jQuery POST插入数据库时遇到问题 在js中 在php中 $data = json_decode($_POST['user'], true); $conn = new mysqli("localhost", "root", "", "dbname"); $conn->query("INSERT INTO users (uid, name, email, number) VALUES (\'".$data['uid']."\', \'".$data['displayName'

我在使用jQuery POST插入数据库时遇到问题

在js中

在php中

$data = json_decode($_POST['user'], true);
$conn = new mysqli("localhost", "root", "", "dbname");
$conn->query("INSERT INTO users (uid, name, email, number) VALUES (\'".$data['uid']."\', \'".$data['displayName']."\', \'".$data['email']."\', \'".$data['phoneNumber']."\')");
它没有插入数据库,上面的一个不工作


请给我用jQuery$插入数据库的代码。POST

jQuery代码:

$(document).ready(function(){
    /* rather then JSON.stringify i had used the simple varibale  */
    $("button").click(function(){
        $.post("demo_test_post.php",{name: "Donald Duck",city: "Duckburg"},
        function(data,status){
            alert("Data: " + data + "\nStatus: " + status);
        });
    });
});
AJAX请求的PHP代码:

<?php 
print_r($_POST);
$name = $_POST['name'];
$city = $_POST['city'];
/* you can now pass this variables in the query and insert your data easily */
?>


您应该考虑将错误添加到问题中。是否有错误消息?请注意,您的代码易于SQL注入,您应该注意这一点。将查询放入变量并转储它。请给我代码,以便使用jQuery$插入db。POST@Sameera已经给了你一个暗示。您也可以通过
var\u dump($data)
查看它包含哪些字段。提示:无需在双引号中转义单引号
\'
。启用错误报告。使用
<?php 
print_r($_POST);
$name = $_POST['name'];
$city = $_POST['city'];
/* you can now pass this variables in the query and insert your data easily */
?>
$("#btnSubmit").click(function () {
                $("#unit").val('');
                    jQuery.ajax({
                        type: 'post',
                        url: $("#hdnService").val() + "InsertUomData",
                        data: "{'Uom':'" + $("#unit").val() + "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {

});
//you have to pass parameter in data: section. same parameter fetch in service class. you have to define the same name of the parameter in service.cs and aspx file