如何在mysql数据库中添加json数据

时间:2014-09-03 02:13:32

标签: php mysql ajax json

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文件需要哪个来运行这个网站。

1 个答案:

答案 0 :(得分:0)

如果我理解你正在研究如何构建你的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 =&#34; INSERT INTO&#34;。$ this-&gt; NomTable_product。&#34;                             (product_id,...)                         VALUES                             (:product_id,:view_name,:id_page_index)&#34 ;;

    $requete = $this->customerPdo->prepare($requete);

    $requete->bindValue(':product_id', $product_id , PDO::PARAM_INT);

    $returnOperation = $requete->execute();
    $requete->closeCursor();
    return$returnOperation;

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

此致