Good afternoon, I hope you can help me with my code, and I have four hours searching and can not find the error, I'm doing an exchange, the variables are stored in a session, the problem is that $discount
is being cleared every time I added, delete, delete or update the product, I would like to put off this variable remains active as the others.
This is my code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vender</title>
<style>
body {
width: 80%;
margin: 0 auto;
padding: 20px;
}
table {
width:100%;
margin: 0 auto;
border: 1px solid black;
border-collapse: collapse;
padding: 3px;
}
td {
border: 1px solid black;
border-collapse: collapse;
}
form {
display: inline-block;
margin-right: 10px;
margin-bottom: 10px;
}
table {
margin-bottom: 10px;
}
</style>
</head>
<body>
<?php
//INICIAMOS SESION
session_start();
//CONECTAMOS A LA DB
require ("config/conectar.php");
//CALCULAMOS DESCUENTO
$descuento = $_GET[descuento];
$des = $total-(($total*$descuento)/100);
$porcentaje = $total - $des;
//RECUPERAMOS VALORES DE LA URL
$product_id = $_GET[codigo]; //ID DE PRODUCTO
$accion = $_GET[accion]; //ACCION
//SI EL PRODUCTO NO EXISTE MOSTRAMOS UNA ALERTA
if($product_id && !productExists($product_id)) {
die('<script type="text/javascript">alert("El producto no existe!");window.location.href="index-sin-estilos.php";</script>');
}
//DECIDIMOS QUE HAREMOS
switch($accion) {
case "agregar":
$_SESSION['venta'][$product_id]++; //SUMAMOS UNO
break;
case "restar":
$_SESSION['venta'][$product_id]--; //RESTAMOS UNO
if($_SESSION['venta'][$product_id] == 0) unset($_SESSION['venta'][$product_id]); //SI LA CANTIDAD LLEGA A CERO BORRAMOS PRODUCTO
break;
case "vaciar":
unset($_SESSION['venta']); //DESTRUIMOS LA SESION Y BORRAMOS TODO
break;
case "eliminar":
unset($_SESSION['venta'][$product_id]); //BORRAMOS EL PRODUCTO SELECCIONADO
break;
case "idescuento":
$_SESSION['venta'][$descuento]; //BORRAMOS EL PRODUCTO SELECCIONADO
break;
}
//USAMOS SPRINTF PARA ASEGURARSE DE QUE $PRODUCT_ID SE INSERTA EN LA CONSULTA COMO UN NÚMERO - PARA EVITAR LA INYECCIÓN SQL
function productExists($product_id) {
$sql = sprintf("SELECT * FROM productos WHERE codigo = %d;",
$product_id);
return mysql_num_rows(mysql_query($sql)) > 0;
}
?>
<form action="index-sin-estilos.php" method="GET">
<input type="hidden" name="accion" value="agregar">
<input style="width:150px;" type="text" name="codigo" placeholder="Codigo del producto" autocomplete="off" autofocus required>
<input type="submit" value="ENTER">
</form>
<form action="index-sin-estilos.php" method="GET">
<input type="hidden" name="accion" value="idescuento">
<input style="width:150px;" type="text" name="descuento" min="0" max="100" placeholder="0" value="<?php echo "$descuento"; ?>" autocomplete="off" required>
<input type="submit" value="ENTER">
</form>
<a style="float: right;" href="index-sin-estilos.php?accion=vaciar" onclick="return confirm('Estas seguro?');">Borrar todo</a>
<br>
<?php
if($_SESSION['venta']) {
echo "<table>";
echo '
<tr>
<td><b><center>Codigo</center></b></td>
<td><b><center>Descripcion</center></b></td>
<td><b><center>Precio</center></b></td>
<td><b><center>Cantidad</center></b></td>
<td><b><center>Importe</center></b></td>
</tr>
';
foreach($_SESSION['venta'] as $product_id => $quantity) {
$sql = sprintf("SELECT codigo, descripcion, venta FROM productos WHERE codigo = %d;",$product_id);
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
list($codigo, $descripcion, $venta) = mysql_fetch_row($result);
//CALCULAMOS EL IMPORTE
$line_cost = $venta * $quantity;
//CALCULAMOS EL TOTAL
$total = $total + $line_cost;
//CALCULAMOS DESCUENTO
$descuento = $_GET[descuento];
$des = $total-(($total*$descuento)/100);
$porcentaje = $total - $des;
echo "<tr>";
//MOSTRAMOS LOS DATOS EN LA TABLA
echo "<td>$codigo</td>";
echo "<td>$descripcion</td>";
echo "<td align=right>$venta</td>";
echo "<td align=center>$quantity ( <a href=$_SERVER[PHP_SELF]?accion=restar&codigo=$product_id>-</a> / <a href=$_SERVER[PHP_SELF]?accion=agregar&codigo=$product_id>+</a> / <a href=$_SERVER[PHP_SELF]?accion=eliminar&codigo=$product_id>X</a>)</td>";
echo "<td align=right>$line_cost</td>";
echo "</tr>";
}
}
echo "</table>";
//MOSTRAMOS EL TOTAL
echo "<table>";
echo "<tr>";
echo "<td colspan=1 align=right><b>Sub-Total</b></td>";
echo "<td style='text-align: right;'><b>$total</b></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan=1 align=right><b>Descuento ($descuento%)</b></td>";
echo "<td style='text-align: right;'><b>$porcentaje</b></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan=1 align=right><b>Total</b></td>";
echo "<td style='text-align: right;'><b>$des</b></td>";
echo "</tr>";
echo "</table>";
}else{
//SI LA SESION ESTA VACIA MOSTRAMOS LO SIGUIENTE
echo "AGREGA UN CODIGO PARA INICIAR";
}
?>
</body>
</html>
Thank you very much in advance for the help you can give me ... A big apology for using Spanish words in the code but I'm from Mexico.
In the video shown as another product to put the discount is deleted. That's what I want to avoid ... http://youtu.be/QfjA-cUVL3Y
This could be as simple as moving session_start() to the top of your document. It should be called before anything else happens on your page, any output or headers set.
Are you seeing warnings on this point in your error log?