【问题标题】:How to add json data in mysql database如何在mysql数据库中添加json数据
【发布时间】:2014-09-03 02:13:32
【问题描述】:
function addToCart(product_id, quantity, main) {
    quantity = typeof(quantity) != 'undefined' ? quantity : 1;
    main = typeof(main) != 'undefined' ? main : 0;
    view1 = $("#view1").val();
    cv_page = $("#cv_page").val();
    order_id = $("#order_id").val();

    $.ajax({
        url: 'index.php?option=com_payment&task=cart',
        type: 'post',
        data: 'product_id=' + product_id + '&quantity=' + quantity + '&main=' + main + '&view1=' + view1 + '&cv_page=' + cv_page + '&order_id=' + order_id,
        dataType: 'json',
        success: function(json) {
            $('.success, .warning, .attention, .information, .error, .fail').remove();
            if (json['success']) {
                $('#cart-total').html(json['total']);

                if (main == "1") {                  
                    window.location =$('#order-url').val();
                } else {
                    window.location =$('#form-url').val();
                }

            } else {
                $('#notification').html(json['message']);
            }
        }           
    });     
} 

如何在mysql数据库中添加这个文件以及如何定义这个

url: 'index.php?option=com_payment&task=cart',

以及如何调用它

<input type="hidden" id="order-url" value ="order.html?cv_page=1#order-title" />  
<input type="hidden" id="form-url" value ="form.html?cv_page=1#form_mark" />

谁能帮帮我,我只知道运行这个站点需要 index.php 文件。

【问题讨论】:

    标签: php mysql ajax json


    【解决方案1】:

    如果我了解您正在研究如何构建 index.php(从您的 AJax 调用的 php 文件)。

    首先,关于您发送的数据,我可以向您推荐以下格式吗?

    data: {product_id.product_id , quantity: quantity [...]},
    

    在您的 php 文件 (index.php) 中:

    1- 检查每个参数(product_id 示例):

    $product_id = null;
    if(isset($_POST['product_id']) && !empty($_POST['product_id'])){
        product_id = $_POST['product_id'];
    }
    

    2-检查是否缺少参数:

    if($product_id === null || [...] ){
        echo json_encode(array(
            'error' => array(
                'msg' => "There is a missing parameter"
            ),
        ));
        exit();
    }
    

    3- 使用您喜欢的方式在数据库中输入数据(以 PDO 为例): $requete = " INSERT INTO ".$this->NomTable_product." (product_id, ...) 价值观 (:product_id, :view_name, :id_page_index)";

        $requete = $this->customerPdo->prepare($requete);
    
        $requete->bindValue(':product_id', $product_id , PDO::PARAM_INT);
    
        $returnOperation = $requete->execute();
        $requete->closeCursor();
        return$returnOperation;
    

    另外,我会建议您在数据验证方面非常小心。在将其放入数据库之前,您应该检查每个参数。我不知道您目前在做什么来连接数据库并输入数据,但是您可以使用 PDO 来改进这一点。

    问候,

    【讨论】:

      猜你喜欢
      • 2020-09-06
      • 2017-07-14
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多