【问题标题】:Warning: Attempt to read property "total" on array警告:尝试读取数组上的属性“总计”
【发布时间】:2022-11-26 12:23:46
【问题描述】:

我正在尝试加载销售、打折库存,但出现此错误

警告:尝试在第 28 行的 C:\xampp\htdocs\veterinaria\terminarVenta.php 中的数组中读取属性“total”

警告:尝试在第 29 行的 C:\xampp\htdocs\veterinaria\terminarVenta.php 中的数组中读取属性“id”

警告:尝试在第 29 行的 C:\xampp\htdocs\veterinaria\terminarVenta.php 中的数组中读取属性“cantidad”

`

<?php
if(!isset($_POST["total"])) exit;


session_start();


$total = $_POST["total"];
include_once "conexion.php";


$ahora = date("Y-m-d H:i:s");


$sentencia = $base_de_datos->prepare("INSERT INTO ventas(fecha, total) VALUES (?, ?);");
$sentencia->execute([$ahora, $total]);

$sentencia = $base_de_datos->prepare("SELECT id FROM ventas ORDER BY id DESC LIMIT 1;");
$sentencia->execute();
$resultado = $sentencia->fetch(PDO::FETCH_OBJ);

$idVenta = $resultado === false ? 1 : $resultado->id;
var_dump($_SESSION["carrito"]);

$base_de_datos->beginTransaction();
$sentencia = $base_de_datos->prepare("INSERT INTO productos_vendidos(id_producto, id_venta, cantidad) VALUES (?, ?, ?);");
$sentenciaExistencia = $base_de_datos->prepare("UPDATE productos SET existencia = existencia - ? WHERE id = ?;");
foreach ($_SESSION["carrito"] as $producto) {
    $total += $producto->total;
    $sentencia->execute([$producto->id, $idVenta, $producto->cantidad]);
    $sentenciaExistencia->execute([$producto->cantidad, $producto->id]);
}
$base_de_datos->commit();
unset($_SESSION["carrito"]);
$_SESSION["carrito"] = [];
header("Location: ./vender.php?status=1");
?>

`

我不知道这个错误试图告诉我什么,因为这是我第一次发生这个错误 var_dump($_SESSION["carrito"]) 是 array(1) { [0]=> array(7) { ["id"]=> string(1) "1" ["codigo"]=> string(1) "1" ["descripcion"]=> string(7) “bozales” [“precioVenta”]=> string(6) “150.00” [“existencia”]=> string(6) “200.00” [“cantidad”]=> int(1) [“total” ]=> 字符串(6) "150.00" } }

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    您正在尝试访问数组而不是对象上的属性,它应该是这样的:

    foreach ($_SESSION["carrito"] as $producto) {
        $total += $producto['total'];
        $sentencia->execute([$producto['id'], $idVenta, $producto['cantidad']]);
        $sentenciaExistencia->execute([$producto['cantidad'], $producto['id']]);
    }
    

    你像这样访问数组键$arr['key'],对象属性像这样$obj-&gt;property,它是一个数组

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-14
      • 2021-11-05
      • 2022-01-08
      • 2021-12-07
      • 2021-05-10
      • 2021-12-30
      • 2022-10-12
      • 2022-12-12
      相关资源
      最近更新 更多