php - 无法在 MySQL、PHP、AngularJS 中插入多行

标签 php mysql angularjs

我有一个“请求”表单,允许通过 AngularJS 插入许多产品(将其视为一张票 -> 1 个请求数,但包含文章的许多“行”+请求的金额)。

问题是,如果我请求 5 篇文章,有时它会在数据库中插入其中 2 篇,有时 3 篇,有时甚至 1 篇。

pedidosClienteNuevo.html

 <div id="cabeza">
    <h3>Nuevo Pedido</h3>
</div>

<div id="cuerpo_alta" ng-init="iniciarNuevoPedido()">
    <form name="formPedidoClienteNuevo" role="form" novalidate>

        <table class="tabla">

            <tr>
                <td>
                    <label>Numero de Pedido: </label>
                </td>
                <td>
                    <input type="text" name= "numeroPedido" placeholder="Numero de Pedido" value="{{nuevoNumeroPedido}}" />
                </td>   
            </tr>
            <tr>
                <td>
                    <label>Codigo de Cliente: </label>
                </td>
                <td>
                    <input type="text" name= "idCliente" placeholder="Codigo de Cliente" ng-model="idCliente" required ng-class="{ 'ingreso-tiene-error' : formPedidoClienteNuevo.idCliente.$invalid && !formPedidoClienteNuevo.idCliente.$pristine, 'ingreso-es-valido' : !(formPedidoClienteNuevo.idCliente.$invalid && !formPedidoClienteNuevo.idCliente.$pristine) }"/>
                </td>
                <td>
                    <a ng-click="buscarCliente(idCliente)">Buscar</a>
                </td>   
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <p ng-show="formPedidoClienteNuevo.idCliente.$invalid && !formPedidoClienteNuevo.idCliente.$pristine" class="help-block">Codigo de cliente requerida.</p>
                    <p ng-model="resultadoCliente">
                        {{resultadoCliente}}
                    </p>
                </td>
            </tr>     
            <tr>
                <td>
                    <label>Codigo de Articulo: </label>
                </td>
                <td>
                    <input type="text" name= "idArticulo" placeholder="Codigo de Articulo" ng-model="idArticulo" required ng-class="{ 'ingreso-tiene-error' : formPedidoClienteNuevo.idArticulo.$invalid && !formPedidoClienteNuevo.idArticulo.$pristine,'ingreso-es-valido' : !(formPedidoClienteNuevo.idArticulo.$invalid && !formPedidoClienteNuevo.idArticulo.$pristine) }"/>
                </td>
                <td>
                    <a ng-click="buscarArticulo(idArticulo)">Buscar</a>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <p ng-show="formPedidoClienteNuevo.idArticulo.$invalid && !formPedidoClienteNuevo.idArticulo.$pristine" class="help-block">Codigo de articulo requerido.</p>
                    <p ng-model="resultadoArticulo">
                        {{resultadoArticulo}}
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <label>Cantidad: </label>
                </td>
                <td>
                    <input type="text" name= "cantidad" placeholder="Cantidad" ng-model="cantidad" ng-class="{ 'ingreso-tiene-error' : formPedidoClienteNuevo.cantidad.$invalid && !formPedidoClienteNuevo.cantidad.$pristine, 'ingreso-es-valido' : !(formPedidoClienteNuevo.cantidad.$invalid && !formPedidoClienteNuevo.cantidad.$pristine) }"/>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <p ng-show="formPedidoClienteNuevo.cantidad.$invalid && !formPedidoClienteNuevo.cantidad.$pristine" class="help-block">Cantidad requerida.</p>
                </td>
            </tr>

            <tr>
                <td>
                    <label>Precio Minorista: </label>
                </td>
                <td>
                    <input type="text" name= "precioMinorista" placeholder="Precio Minorista" value= "{{precioMinoristaBuscado}}" disabled/>
                </td>
            </tr>

            <tr>
                <td>
                    <label>Fecha de Pedido: </label>
                </td>
                <td>
                    <input type="text" name= "fechaPedido" placeholder="Fecha de Pedido" value="{{datePedido | date:'dd/MM/yyyy'}}" disabled/>
                </td>
            </tr>

        </table>

        <label class="volverAtras">
            <a href="pedidos_cliente.html#/lista">Atras</a>
        </label>
        <!-- Agrega el articulo actual a los detalles y permite la carga de un nuevo articulo-->
        <button class="botonera" ng-click="confirmarArticulo(numeroPedido, idCliente, idArticulo, cantidad, precioMinoristaBuscado, fechaPedido)">Confirmar
        </button>
        <!-- Deshabilita los demas botones, excepto Guardar. Calcula el total del pedido-->
        <button class="guardarBoton" ng-click="pedidosClienteInsert()" type="submit">GUARDAR
        </button>
        <table class="listadoDetalles tabla">
            <caption class="tituloTablaDetalles">Detalles del pedido</caption>
            <tr>
                <th>Codigo Articulo
                </th>
                <th>Cantidad
                </th>
                <th>Precio
                </th>
                <th>Precio Final
                </th>
                <th>Borrar
                </th>
            </tr>
            <tr ng-repeat="item in listadoActual">
                <td>{{item.idArticulo}}
                </td>
                <td>{{item.cantidad}}
                </td>
                <td>{{item.precioMinorista}}
                </td>
                <td>{{item.precioFinalDeFila}}
                </td>
                <td>
                    <button ng-click="quitarElemento(item)">X
                    </button>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                </td>
                <td>TOTAL
                </td>
                <td>{{totalPedido}}
                </td>
            </tr> 
        </table>
    </form> <!-- formPedidoClienteNuevo-->
</div>

pedidosClienteControlador.js( Controller ,仅将数据发送到 PHP 的函数部分。

$scope.pedidosClienteInsert = function() {
//descomponer el array en tantas lineas como posea y mandar una solicitud a PHP por cada linea

  angular.forEach($scope.listadoActual, function(valor,indice){
    alert("Codigo del articulo:" + valor.idArticulo);
    alert("Cantidad: " + valor.cantidad);
    idCliente = valor.idCliente;
    idArticulo = valor.idArticulo;
    cantidad = valor.cantidad;
    precioMinorista = valor.precioMinorista;
    numeroPedido = $scope.nuevoNumeroPedido;
    fechaPedido = $scope.fechaPedido;
    $http.post("servidor/pedidosCliente/pedidosClienteInsert.php?numeroPedido="+numeroPedido+"&idCliente="+idCliente+"&idArticulo="+idArticulo+"&cantidad="+cantidad+"&precioMinorista="+precioMinorista+"&fechaPedido="+fechaPedido).success(function(data){
      $scope.listadoActual = [];
      $scope.totalPedido= 0.00;
      alert("Exito");
    });
  });

pedidosClienteInsert.php(连接到 MySQL 并尝试下载的那个)

    <?php 
`require_once '../conectar2.php';
    $numeroPedido = $_GET['numeroPedido'];
    $idCliente = $_GET['idCliente'];
    $idArticulo = $_GET['idArticulo'];
    $cantidad = $_GET['cantidad'];
    $precioMinorista = $_GET['precioMinorista'];

    // convertir fechas al formato que tiene en la BD
    $fecha1=$_GET['fechaPedido'];
    list($dia, $mes, $anio)=explode("/", $fecha1);
    $fechaPedido=$anio."-".$mes."-".$dia;

    // Tomar el ID mas alto y sumarle 1
    $result= $mysqli->query("SELECT MAX(idPedidoCliente) FROM pedidoCliente");
    $resultFila = $result->fetch_row();
    $idPedidoCliente=($resultFila[0]) + 1;

    $query="INSERT INTO pedidoCliente VALUES ('$idPedidoCliente', '$numeroPedido', '$idCliente', '$idArticulo', '$cantidad', '$precioMinorista', '$fechaPedido')";
    $result = $mysqli->query($query) or die($mysqli->error.__LINE__);
    $result = $mysqli->affected_rows;
    echo $json_response = json_encode($result);
?>

有什么想法吗?抱歉,代码很糟糕。

最佳答案

您的 XmlHttpRequest 类型是 post

  $http.post 

但是你正在尝试从 GET var 获取请求参数。在你的 php 代码中尝试这个

  $rowData = json_decode(file_get_contents("php://input"));

在你的 Angular 代码更改请求中,像这样。

  $http({
      url : "url",
      data : {data Object}
  }).success(function(response){
     // handle response here
  })

关于php - 无法在 MySQL、PHP、AngularJS 中插入多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32277746/

相关文章:

angularjs - Angular ui-router 不在嵌套路由中调用子 Controller

angularjs - 如何等待 Protractor 端的http请求响应

javascript - AngularJS - 从 1.2.9 迁移到 1.4.9 时出错

php - 如何构建多站点管理器应用程序?

php - gmail 没有正确显示 html 文本

php - 是否可以在不删除数据库的情况下加载学说固定装置

php - 如何计算并显示二进制上的所有左子和右子

mysql - 程序限制

php - 尝试为现有表创建列时出现错误用户已存在

javascript - 通知: Undefined variable picture,性别