This is the file addAlCarrito.php:
<?php
if(!isset($_POST["codigo"])&&!isset($_POST["codigo1"])) return;
$codigo = $_POST["codigo"];
if(!isset($_POST["codigo"])&&!isset($_POST["codigo1"])) return;
$codigo1 = $_POST["codigo1"];
include_once "base_de_datos.php";
$sentencia = $base_de_datos->prepare("SELECT * FROM productos WHERE codigo = ? LIMIT 1;");
$sentencia->execute([$codigo]);
$producto = $sentencia->fetch(PDO::FETCH_OBJ);
$precioVenta=0;
if($producto){
if($codigo1=="BOLIVIANO"){
if($producto->existencia < 1){
header("Location: ./vender.php?status=5");
exit;
}
session_start();
$indice = false;
for ($i=0; $i < count($_SESSION["carrito"]); $i++) {
if($_SESSION["carrito"][$i]->codigo === $codigo){
$indice = $i;
break;
}
}
if($indice === FALSE){
$producto->cantidad = 1;
$producto->total = $producto->precioVentaB;
array_push($_SESSION["carrito"], $producto);
}else{
$_SESSION["carrito"][$indice]->cantidad++;
$_SESSION["carrito"][$indice]->total = $_SESSION["carrito"][$indice]->cantidad * $_SESSION["carrito"][$indice]->precioVentaB;
}
header("Location: ./vender.php");
}
}else header("Location: ./vender.php?status=4");
?>
The error is displayed as follows: Notice: Undefined index: code1 in C: \ xampp \ htdocs \ addAlCarrito.php on line 5
This is the file vender.php:
<?php
include_once "encabezado.php";
session_start();
if(!isset($_SESSION["carrito"])) $_SESSION["carrito"] = [];
$granTotal = 0;
$mon=0;
?>
<div class="col-xs-12">
<h1>Vender</h1>
<?php
if(isset($_GET["status"])){
if($_GET["status"] === "1"){
?>
<div class="alert alert-success">
<strong>¡Correcto!</strong> Venta realizada correctamente
</div>
<?php
}else if($_GET["status"] === "2"){
?>
<div class="alert alert-info">
<strong>Venta cancelada</strong>
</div>
<?php
}else if($_GET["status"] === "3"){
?>
<div class="alert alert-info">
<strong>Ok</strong> Producto quitado de la lista
</div>
<?php
}else if($_GET["status"] === "4"){
?>
<div class="alert alert-warning">
<strong>Error:</strong> El producto que buscas no existe
</div>
<?php
}else if($_GET["status"] === "5"){
?>
<div class="alert alert-danger">
<strong>Error: </strong>El producto está agotado
</div>
<?php
}else{
?>
<div class="alert alert-danger">
<strong>Error:</strong> Algo salió mal mientras se realizaba la venta
</div>
<?php
}
}
?>
<br>
<form method="post" action="agregarAlCarrito.php">
<label for="codigo">Código de barras:</label>
<input autocomplete="off" autofocus class="form-control" name="codigo" required type="text" id="codigo" placeholder="Escribe el código">
</form>
<br><br>
<table class="table table-bordered">
<form method="post" action="agregarAlCarrito.php">
<label for="codigo1">Tipo de moneda:</label>
<input autocomplete="off" autofocus class="form-control" name="codigo1" required type="text" id="codigo1" placeholder="BOLIVIANO,DOLAR,PESO">
</form>
<br><br>
<thead>
<tr>
<th>ID</th>
<th>Código</th>
<th>Descripción</th>
<th>Tipo de moneda</th>
<th>Precio de venta</th>
<th>Cantidad</th>
<th>Total</th>
<th>Quitar</th>
</tr>
</thead>
<tbody>
<?php foreach($_SESSION["carrito"] as $indice => $producto){
$granTotal += $producto->total;
?>
<tr>
<td><?php echo $producto->id ?></td>
<td><?php echo $producto->codigo ?></td>
<td><?php echo $producto->descripcion ?></td>
<td><?php echo $moneda ?></td>
<td><?php echo $producto->precioVentaB ?></td>
<td><?php echo $producto->cantidad ?></td>
<td><?php echo $producto->total ?></td>
<td><a class="btn btn-danger" href="<?php echo "quitarDelCarrito.php?indice=" . $indice?>"><i class="fa fa-trash"></i></a></td>
</tr>
<?php } ?>
</tbody>
</table>
<h3>Total: <?php echo $granTotal; ?></h3>
<form action="./terminarVenta.php" method="POST">
<input name="total" type="hidden" value="<?php echo $granTotal;?>">
<button type="submit" class="btn btn-success">Terminar venta</button>
<a href="./cancelarVenta.php" class="btn btn-danger">Cancelar venta</a>
</form>
</div>