weixin_33726313 2018-04-30 06:22 采纳率: 0%
浏览 32

Ajax-在PHP中使用Json时出错

I'm trying to insert JSON data into MySQL using Ajax. But I don't get anything, The query isn't working.

I tried everything, and I don't know if the problem is in Ajax, PHP or in the JSON.

Where should I start looking?

JS CODE

$("#btnprueba").click(function () {
            var array1 = [];
            $("#tabla .DataRow").each(function () {

                var firstTableData = {};
                var Aidi = $(this).find('td').eq(0).text().trim();
                    firstTableData.ID_Articulo = $(this).find('td').eq(0).text().trim();
                    firstTableData.Descripcion = $(this).find('td').eq(1).text().trim();
                    firstTableData.Valor_Venta = $(this).find('td').eq(2).text().trim();
                    firstTableData.Cantidad = $(this).find('td').eq(3).text().trim();
                    firstTableData.Subtotal = $(this).find('td').eq(4).text().trim();
                    array1.push(firstTableData);

            });

            var JsonValues = JSON.stringify(array1);
                alert(JsonValues);
            $.ajax({
                type: "POST",
                url: "GuardarDatosFactura2.php",
                data: "J="+JsonValues,
                success: function(response){
                    alert(response);
                },
                error: function(e){
                    console.log(e.message);
                },

            });

        });

Json in the variable JsonValues

[{"ID_Articulo":"001","Descripcion":"Caramelos","Valor_Venta":"6500","Cantidad":"2","Subtotal":"13000"}]

PHP CODE

<?php 
header("Content-Type: application/json; charset=UTF-8");

$CON = mysqli_connect("localhost","root","","BDfactura") or die ("error");

$data = json_decode($_POST['J'],false);

    $ID=$data->ID_Articulo;
    $Canti=$data->Cantidad;   
    $Vlr=$data->Valor_Venta;

    $cadena2 = "INSERT INTO ItemXVenta (IdArticulo, Cantidad, ValorVenta) VALUES ('$ID','$Canti','$Vlr')";
    $create2 = mysqli_query($CON,$cadena2);

    if($cadena2){
        $MSG= "Se guardaron los datos";
    }
    else{
        $MSG= "No se guardaron los datos";
    }
    echo($MSG);



?>
  • 写回答

3条回答 默认 最新

  • weixin_33725272 2018-04-30 06:31
    关注

    You have to get the array values from json decoded data like this :

    <?php 
    header("Content-Type: application/json; charset=UTF-8");
    
    $CON = mysqli_connect("localhost","root","","BDfactura") or die ("error");
    
    $data = json_decode($_POST['J'],false);
    
        $ID=$data[0]->ID_Articulo;
        $Canti=$data[0]->Cantidad;   
        $Vlr=$data[0]->Valor_Venta;
    
        $cadena2 = "INSERT INTO ItemXVenta (IdArticulo, Cantidad, ValorVenta) VALUES ('$ID','$Canti','$Vlr')";
        $create2 = mysqli_query($CON,$cadena2);
    
        if($cadena2){
            $MSG= "Se guardaron los datos";
        }
        else{
            $MSG= "No se guardaron los datos";
        }
        echo($MSG);
    
    
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏