Ver Mensaje Individual
  #1 (permalink)  
Antiguo 09/04/2016, 16:06
Avatar de aviweb2015
aviweb2015
 
Fecha de Ingreso: abril-2016
Ubicación: venezuela
Mensajes: 215
Antigüedad: 8 años, 1 mes
Puntos: 1
Mensaje duplicidad de datos

hola amigos tengo un problemita resulta que cuanto intento guardar los datos en mi bd me crea duplicidad de datos cuando intento registrar dos productos en adelantes mis tablas son factura y detalles factura en la primera si me gustada bien pero en la segunado que seria detalles factura me duplica los datos cuando intento registrar dos productos en adelante.





código del carrito e campos para completar el registro personal

Código PHP:
Ver original
  1. <?php
  2. if (isset($_GET['id']))
  3. $id = $_GET['id'];
  4. else
  5. $id = 1;
  6. if (isset($_GET['action']))
  7. $action = $_GET['action'];
  8. else
  9. $action = "empty";
  10.  
  11. switch($action){
  12.      
  13. case "add":
  14. if(isset($_SESSION['carro'][$id]))
  15. $_SESSION['carro'][$id]++;
  16. else
  17. $_SESSION['carro'][$id]=1;
  18. break;
  19.        
  20. case "remove":
  21. if(isset($_SESSION['carro'][$id]))
  22.           {
  23.             $_SESSION['carro'][$id]--;
  24.             if($_SESSION['carro'][$id]==0)
  25.               unset($_SESSION['carro'][$id]);
  26.           }
  27.          
  28.         break;
  29.         case "removeProd":
  30.           if(isset($_SESSION['carro'][$id])){
  31.             unset($_SESSION['carro'][$id]);
  32.           }
  33.         break;
  34.        
  35.         case "mostrar":
  36.           if(isset($_SESSION['carro'][$id])){
  37.             continue;
  38.           }
  39.         break;
  40.        
  41.         case "empty":
  42.           unset($_SESSION['carro']);
  43.        
  44.         break;
  45.            
  46.        
  47.       }
  48.  
  49.      
  50. if(isset($_SESSION['carro'])){
  51. echo "<form action='carro.php' method='POST'>";
  52. echo "<table class=''>";
  53. $totalcoste = 0;//Inicializamos el contador de productos seleccionados.
  54. $xTotal = 0;
  55.        
  56. echo "<tr>";
  57. echo "<td>Producto</td>";
  58. echo "<td>Cantidad</td>";
  59. echo "<td>Acciones</td>";
  60. echo "<td colspan=2 align=right >&nbsp;&nbsp;&nbsp;&nbsp;SubTotal</td>";
  61. echo "</tr>";
  62. echo "<tr><td colspan=5><hr></td></tr>";
  63.  
  64. foreach($_SESSION['carro'] as $id => $x){
  65. $resultado = mysql_query("SELECT id,descripcion,precio FROM servicios WHERE id=$id");
  66. $mifila = mysql_fetch_array($resultado);
  67. $id = $mifila['id'];
  68. $descripcion = $mifila['descripcion'];
  69. //acortamos el nombre del producto a 40 caracteres
  70. $descripcion = substr($descripcion,0,40);
  71. $precio = $mifila['precio'];
  72. //Coste por artículo según la cantidad elegida
  73. $coste = $precio * $x;
  74. //Coste total del carro
  75. $totalcoste = $totalcoste + $coste;
  76. //Contador del total de productos añadidos al carro
  77. $xTotal = $xTotal + $x;
  78.          
  79. echo "<input type='hidden' name=descripcion value='$descripcion'>";
  80.  
  81.  
  82. echo "<tr>";
  83. echo "<td align='left'>$descripcion</td>";
  84. echo "<td align='center'>$x<input type='hidden' name=chk_group3[] value='$x'></td>";
  85.          
  86. echo "<td align='left'>";
  87. echo "<a href='carro.php?id=". $id ."&action=add'><img src='img/aumentar.png' style='padding:0 0px 0 5px;' alt='Aumentar cantidad' /></a>";
  88.           //Controlamos el display para cuando se vaya a eliminar el producto del carro o bien
  89.           //se vaya a reducir la cantidad.
  90.           //if ($x > 1)
  91. echo "<a href='carro.php?id=". $id ."&action=remove'><img src='img/restar.png' alt='Reducir cantidad' /></a>";
  92.           //else
  93. echo "<a href='carro.php?id=". $id ."&action=removeProd'><img src='img/eliminar.png' alt='Reducir cantidad' /></a></td>";
  94.          
  95. echo "<td align='right'>  </td>";
  96. echo "<td align='right' style='margin-left:10px'>$coste Bsf
  97. <input type='hidden' name=chk_group2[] value='$coste'>";
  98. echo "</tr>";
  99.  
  100. }
  101.         echo "<tr><td colspan='5'><hr></td></tr>";
  102.         echo "<tr>";
  103.         echo "</table>";
  104.  
  105. echo "<table>";
  106. echo "<td align='right'
  107. colspan='4'><b>
  108. <h4 class='blue-text text-darken-2
  109. right CONDENSED LIGHT5'>Total&nbsp;=&nbsp;$totalcoste&nbsp;Bsf</h4></b></td>
  110. <input type='hidden' name=costetotal value='$totalcoste'>";
  111. echo "</tr>";
  112. echo "</table>";?>
  113. <!-- final de la tabla-->
  114. </div><!-- fin colunna izquiereda -->
  115.  
  116.  
  117.  
  118. <div class="col s12 m8 l5"> <!-- colunna derecha -->
  119. <!-- fecha y numero de factura-->
  120. <div class='input-field col s12 m4'><p class='blue-text text-darken-2'>Fecha:</p>
  121. <input type="text" name="fecha" value="<?php echo date("d/m/Y"); ?>"
  122. readonly="true"/>
  123. </div>
  124. <div class='input-field col s12 m4'><p class='blue-text text-darken-2'>N° de Fact.</p>
  125. <input type="text" name='idfactura' readonly='readonly' value="
  126. <?php
  127. include("conexion.php");
  128. $res=mysql_query("select * from factura");
  129. if($campo=mysql_fetch_array($res))
  130. {
  131. $codigo=mysql_num_rows($res)+1;
  132. echo $codigo=$codigo;
  133. }
  134. if(mysql_num_rows($res)==0)
  135. echo $codigo='1' ;
  136. ?>" >
  137. </div>
  138. <div class='input-field col s12 m4'><p class='blue-text text-darken-2'>Personas</p>
  139. <input id="icon_prefix" type="text" name="personas" autocomplete="off"
  140. title="Se Necesita el numero de Personas Asistir" >
  141. </div>
  142. <div class='input-field col s12 m12'><p class='blue-text text-darken-2'>Tipo de Organización.</p>
  143. <select select name="evento" title="Disculpa, seleccione el Tipo de Organizacion!" required/>
  144. <option value=""  disabled selected>Seleccione</option>
  145. <?php
  146. include('conexion.php');
  147. $result = mysql_query("SELECT * FROM evento");
  148. while ($row=mysql_fetch_array($result)) {
  149. ?>
  150. <option value="<?php echo $row['descripcion'];?>">
  151. <?php echo $row['descripcion'];?> </option>
  152. <?php
  153. }
  154. mysql_close($link);
  155. ?>
  156. </select>
  157. </div>
  158.  
  159.  
  160. <div class='input-field col s12 m12'>
  161. <p class='blue-text text-darken-2'>Cliente</p>
  162. <input type=text placeholder='Busqueda de clientes'
  163. autocomplete="off"  id="inputString" name='idcedula'>
  164.  
  165.  
  166.  
  167. </div>
  168.  
  169. <div class='center-align'>
  170. <div class='center-btn'><!-- botones -->
  171.  
  172. <div class='row'>
  173. <button class='btn waves-effect red accent-4
  174. btn-medium tooltipped' data-position='right'
  175. data-tooltip='Generar Contrato' type='submit' name='guardar'>
  176. <i class="material-icons">description</i></i>
  177. </button></form>
  178. </div>
  179.  
  180.  
  181.  
  182.  
  183. <div class='row'>
  184. <a href="./contrato.php">
  185. <button class='btn waves-effect cyan deep-purple accent-4
  186. btn-medium tooltipped' data-position='right'
  187. data-tooltip='Volver a la lista de productos'
  188. id="submit" type="submit" >
  189. <i class='material-icons'>reply_all</i>
  190. </button></a>
  191. </div>
  192. <div class='row'>
  193. <a href='carro.php?id=$_SESSION[id]'>
  194. <button class='btn waves-effect cyan darken-4  
  195. btn-medium tooltipped' data-position='right'
  196. data-tooltip='Vaciar Carrito' type='reset' >
  197. <i class='tiny mdi-image-adjust'></i>
  198. </button></a>
  199.  
  200.  
  201.  
  202.  
  203. </div>
  204. </div><!-- fin botones -->
  205.  
  206.  
  207.  
  208. </div><!-- fin div colunna izquierda y colunna derecha -->
  209.  
  210. <br>
  211.  
  212. <br>
  213.  
  214. <br>
  215.  
  216.  
  217. <?php    
  218.     }
  219.       else
  220.  
  221. echo "<br>
  222. <br>
  223. <br>
  224. <br>
  225. <br>
  226. <div class='center-align'>
  227. <h5 class='red-text text-darken-4 center-align'>Ops, El carro
  228. está vacío, en breve sera redireccionado a la lista de productos</h4>
  229. </div>";
  230. // Redireccionar a contrato.php después de 5 segundos
  231. //header( "refresh:10; url=contrato.php" );
  232.  
  233.  
  234.  
  235.     ?>




código para el proceso de registro en la bd:

Código PHP:
Ver original
  1. <?php
  2. if (isset ($_POST['guardar'])){
  3. $idfactura=$_REQUEST['idfactura'];
  4. $idcedula=$_REQUEST['idcedula'];
  5. $fecha=$_REQUEST['fecha'];
  6. $iddetalle_factura=$_REQUEST['iddetalle_factura'];
  7. $descripcion=$_REQUEST['descripcion'];
  8. $cantprod=$_REQUEST['chk_group3'];
  9. $coste=$_REQUEST['chk_group2'];
  10. $costetotal=$_REQUEST['costetotal'];
  11. $personas=$_REQUEST['personas'];
  12. $evento=$_REQUEST['evento'];
  13.  
  14. include("conexion.php");
  15. $sql="SELECT * FROM factura WHERE id='$id'";
  16.    
  17.   $res=mysql_query($sql,$link);
  18.   $nrows=mysql_num_rows($res);
  19.   if($nrows==0){
  20.  
  21.  
  22.         for ($j = 0; $j < count($cantprod); $j++) {
  23.                 for ($k = 0; $k < count($coste); $k++) {
  24.  
  25.  
  26. if (mysql_query("INSERT INTO factura (idfactura,idcedula,fecha,costetotal) VALUES
  27. ('$idfactura','$idcedula','$fecha','$costetotal')",$link));
  28.  
  29. if (mysql_query("INSERT INTO detalles_factura (iddetalle_factura,idfactura,descripcion,cantprod,coste,personas,evento) VALUES
  30. ('$iddetalle_factura','$idfactura','$descripcion','$cantprod[$j]','$coste[$k]','$personas','$evento')",$link));
  31.  
  32. }
  33.         }
  34.  
  35. echo "<script>alert ('Se ha generado el contrato!');</script>
  36. <META HTTP-EQUIV='REFRESH' CONTENT=0;URL=http:presupuesto.php>";
  37.           }
  38.  
  39. else echo "<script>alert ('Disculpe este presupuesto ya existe!');</script>
  40. <META HTTP-EQUIV='REFRESH' CONTENT=0;URL=http:presupuesto.php>";
  41.           }
  42.  
  43.  
  44. ?>



gracias de ante mano
__________________
yoclens avilan