javascript - I can not hold a session variable passing by get -


good afternoon, hope can me code, , have 4 hours searching , can not find error, i'm doing exchange, variables stored in session, problem $discount being cleared every time added, delete, delete or update product, put off variable remains active others.

this 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 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 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 * productos 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'] $product_id => $quantity) {                     $sql = sprintf("select codigo, descripcion, venta productos 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 in advance can give me ... big apology using spanish words in code i'm mexico.

in video shown product put discount deleted. that's want avoid ... http://youtu.be/qfja-cuvl3y

you should debug code little better , issue may not coming form $discounto variable.

i think formula calculating discount incorrect.

try code , see if make different. if works explain.

    <!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 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 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 * productos 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'] $product_id => $quantity) {                     $sql = sprintf("select codigo, descripcion, venta productos 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> 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -