Ver Mensaje Individual
  #7 (permalink)  
Antiguo 06/01/2011, 16:42
Avatar de Masterphp
Masterphp
 
Fecha de Ingreso: septiembre-2009
Ubicación: /home/php/
Mensajes: 94
Antigüedad: 14 años, 7 meses
Puntos: 3
Respuesta: Suma de pagos en php

ahiva: antes debes de tener creado tabla datos con compos id, a, b, suma
esto es una guia numas analiza y modifica atu gusto.

Código PHP:
Ver original
  1. <?php
  2. $uri = "http://".$_SERVER["SERVER_NAME"];
  3. define( 'DB_SERVER', 'localhost' );
  4. define( 'DB_NAME', 'nombre_bd');
  5. define( 'DB_USER', 'root');
  6. define( 'DB_PASS', 'contraseña');
  7.  function conectar () {
  8.    
  9.     $db_con = mysql_pconnect (DB_SERVER,DB_USER,DB_PASS);
  10.     if (!$db_con) return false;
  11.     if (!mysql_select_db (DB_NAME, $db_con)) return false;
  12.     return $db_con;
  13.  
  14. }
  15. $dbConn = conectar();
  16. if ( !empty($_POST['sumar']) ) {
  17.     if ( !empty($_POST['a']) )  $a = $_POST['a'];
  18.     if ( !empty($_POST['b']) )  $b = $_POST['a'];
  19.     if ( empty($a) )    $error['a']         = 'falta numero';
  20. if ( empty($b) )    $error['b']         = 'falta numero 2';
  21. if ( empty($error) ) {
  22.         // inserto los datos de registro en la db
  23. $suma = $a + $b;
  24.         $query  = "INSERT INTO `datos` (a,b,suma) VALUES ('$a','$b','$suma')";
  25.         $result = mysql_query($query, $dbConn);
  26. echo ' <script>
  27. alert("Datos enviados correctamente.");
  28. </script>
  29. <SCRIPT LANGUAGE="javascript">
  30. location.href ="'.$uri.'"
  31. </SCRIPT>
  32. ';
  33. exit();
  34.     }
  35. }
  36. ?>
Código HTML:
Ver original
  1. <html>
  2. <head>
  3. <title>Suma</title>
  4. </head>
  5. <body>
  6. <? if (!empty($error)) { ?>
  7. ERROR!<BR>
  8.                 <ul>
  9.                 <? foreach ($error as $mensaje) { ?>
  10.                     <li><?=$mensaje ?></li>
  11.                 <? } ?>
  12.                 </ul>
  13.             <? } ?>
  14. <form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
  15. <label>Abono 1</label>
  16. <!-- br -->
  17. <input type="text" name="a" />
  18. <!-- br -->
  19. <label>Abono 2</label>
  20. <!-- br -->
  21. <input type="text" name="b" />
  22. <!-- br -->
  23. <input type="submit" name="sumar" value="Sumar" />
  24. </form>
  25. <!-- br -->
  26. <?php
  27. $a = $_POST['a'];
  28. $b = $_POST['b'];
  29. $suma = $a + $b;
  30. echo "La suma de los digitos es <b>" . $suma;
  31. ?>
  32. </body>
  33. </html>

ahora imprimir.php
Código PHP:
Ver original
  1. <?php
  2. $query = mysql_query("SELECT * FROM `datos` ",$dbConn) or die("Problemas en el select:".mysql_error());
  3. while ($reg=mysql_fetch_array($query))
  4. {
  5. echo "dato 1 ".$reg['a']."<br>";
  6. echo "dato 2 ".$reg['b']."<br><hr>";
  7. echo "suma es ".$reg['suma']."<br>";
  8. }
  9. ?>