[英]I can not hold a session variable passing by get
下午好,我希望您能为我的代码提供帮助,并且我花了四个小时进行搜索,但找不到错误,我正在进行交换,变量存储在会话中,问题是$discount
正在清除每次添加,删除,删除或更新产品时,我都想推迟此变量,使其像其他变量一样保持活动状态。
这是我的代码:
<!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>
提前非常感谢您为我提供的帮助。。。很抱歉在代码中使用西班牙语单词,但我来自墨西哥。
在显示为另一种产品的视频中,折扣被删除。 那就是我要避免的事情... http://youtu.be/QfjA-cUVL3Y
您应该对代码进行更好的调试,并且$discounto
变量可能不会出现问题。
我认为您的折扣计算公式不正确。
试试这个代码,看看是否有所作为。 如果有效,我会解释。
<!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';
$total = 1000;
//CALCULAMOS DESCUENTO
$descuento = floatval($_GET['descuento']);
//RECUPERAMOS VALORES DE LA URL
$product_id = intval($_GET['codigo']); //ID DE PRODUCTO
$accion = trim($_GET['accion']); //ACCION
$porcentaje = $total - (( $total * $descuento ) /100 );
//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
$currentVal = 0;
if(isset($_SESSION['venta'][$product_id])){
$currentVal = intval($_SESSION['venta'][$product_id]);
}
switch($accion) {
case "agregar":
$_SESSION['venta'][$product_id] = $currentVal + 1; //SUMAMOS UNO
break;
case "restar":
$_SESSION['venta'][$product_id] = $currentVal - 1; //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(isset($_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>
只需移动:
//INICIAMOS SESION
session_start();
在文档顶部为:
<?php
//INICIAMOS SESION
session_start();
?>
因此您的文档应以以下内容开头:
<?php
//INICIAMOS SESION
session_start();
?>
<!DOCTYPE html>
<html>
<head>
希望有帮助! :-D
这就像将session_start()移动到文档顶部一样简单。 应该在页面上发生任何其他事件,任何输出或标题之前调用它。
您是否在错误日志中看到关于这一点的警告?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.