如何将JSON代码获取到MYSQL数据库中? [英] How to get JSON code into MYSQL database?

查看:709
本文介绍了如何将JSON代码获取到MYSQL数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用jQuery自动完成功能创建了此表单.从自动完成列表中选择的品牌需要使用$ .ajax函数发送到PHP文件.我的代码似乎不起作用,但是我找不到错误.我不知道为什么数据没有插入到MYSQL数据库中.这是我的代码:

I've created this form with a jQuery autocomplete function. The selected brand from the autocomplete list needs to get sent to a PHP file using $.ajax function. My code doesn't seem to work but i can't find the error. I don't know why the data isn't getting inserted into MYSQL database. Here is my code:

JQUERY:

           $(document).ready(function() {

    $("#autocomplete").autocomplete({
        minLength: 2
    });

    $("#autocomplete").autocomplete({

        source: ["Adidas", "Airforce", "Alpha Industries", "Asics", "Bikkemberg", "Birkenstock", "Bjorn Borg", "Brunotti", "Calvin Klein", "Cars Jeans", "Chanel", "Chasin", "Diesel", "Dior", "DKNY", "Dolce &  Gabbana"]

    });

    $("#add-brand").click(function() {

        var merk = $("#autocomplete").val();

        $("#selected-brands").append(" <a class=\"deletemerk\" href=\"#\">" + merk + "</a>");

                //Add your parameters here        
                var param = JSON.stringify({
                    Brand: merk
                });

                $.ajax({
                    type: "POST",
                    async: true,
                    url: "scripttohandlejson.php",
                    contentType: "application/json",
                    data: param,
                    dataType: "json",
                    success: function (good){
                       //handle success

                       alert(good)
                    },
                    failure: function (bad){
                       //handle any errors

                       alert(bad)

                    }
                });


        return false;

    });

});

PHP文件: scripttohandlejson.php

PHP FILE: scripttohandlejson.php

  <?PHP

     $getcontent = json_decode($json, true);

     $getcontent->{'Brand'};

     $vraag = "INSERT INTO kijken (merk) VALUES ('$data_s')";

     $voerin = mysql_query($vraag) or die("couldnt put into db");

  <?

推荐答案

$arr_content = json_decode($json, true);

$brand = $arr_content["Brand"];

$vraag = "INSERT INTO kijken (merk) VALUES ('" . $brand . "')";

问题:在您的示例中,您的json_decode返回一个关联数组,因为在函数调用中您具有参数'true'.但是在下一行中,您尝试通过尝试访问'它的'BRAND'属性来像对象一样使用结果.

THE PROBLEM: In your example, your json_decode returns an associative array because you have the parameter 'true' in the function call. But in the next line you are attempting to use the result like an object by trying to access 'it's 'BRAND' property.

您要做的就是从json_decode函数调用中删除true,或者使用上面的代码.

All you have to do is to remove true from the json_decode function call, or alternatively use my code above.

注意:也编辑了SQL语句.

NOTE: Edited SQL statement too.

这篇关于如何将JSON代码获取到MYSQL数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆