Author Topic: Re-send data after updating a DB  (Read 265 times)

0 Members and 1 Guest are viewing this topic.

Offline ferpadroTopic starter

  • Irregular
  • Posts: 11
    • View Profile
Re-send data after updating a DB
« on: September 16, 2007, 01:41:25 PM »
I have this function in a script called user_functions.php:

<div class="code">
<?
function Alquilar($autoalquilar,$fechai,$fechaf)
{
   $sql="INSERT INTO alquiler VALUES ('','$autoalquilar','$_SESSION[id]','$fechai','$fechaf')";
   mysql_query($sql) or die(mysql_error());
   $sql2="UPDATE auto SET alquilado = 'si' WHERE idauto = '$autoalquilar'";
   mysql_query($sql2) or die(mysql_error());
   echo "Informacion cargada con exito";
}   
?>
</div>

This function is included in user.php and used whenever the user clicks on the "Alquilar" button:

<div class="code">
<?php

   include('config.inc.php');
   include('user_functions.php');

   require('session.php');

   $val = check_in($_SESSION['usuario'], $_SESSION['contrasena'], $tab);

   if (strcmp($val,"usuario") != 0) {

      cambiar("index.php");

      return;

   }   

   else
   {
?>
   <html>
   <head>
   <form method="post" action="">
   <div style="text-align: center;"><big style="font-style: italic;"><big><big><big>-.USUARIO.-</big></big></big></big>
   &nbsp; &nbsp;<br><br>
   
   1) Realizar Alquiler</br></br>
   Auto:
   <select name=autoaalquilar>
<?   
     $sql= "SELECT * FROM auto WHERE alquilado = 'no'";

     $consulta=mysql_query($sql);

     while($array = mysql_fetch_array($consulta))

     {

        echo "<option value=$array[0]>$array[1]</option>";

     }   

?>
   </select></br>
<?
   echo "$_POST[autoaalquilar]";
?>
   Fecha Inicio: Dia:<select name="diai" class="input" id="diai">

              <?

              for ($i = 1; $i < 32; $i++)

            {

               if ($i < 10)

                  echo '<option value="0'.$i.'">0'.$i.'</option>';

               else

                  echo '<option value="'.$i.'">'.$i.'</option>';

            }      

           ?>  </select>
   Mes: <select name="mesi" class="input" id="mesi">

              <?

              for ($i = 1; $i < 13; $i++)

            {

               if ($i < 10)

                  echo '<option value="0'.$i.'">0'.$i.'</option>';

               else

                  echo '<option value="'.$i.'">'.$i.'</option>';

            }      

           ?> </select>
   Anio:  <select name="anioi" class="input" id="anioi">

              <?

              for ($i = 2007; $i < 2100; $i++)

            {

               if ($i < 10)

                  echo '<option value="0'.$i.'">0'.$i.'</option>';

               else

                  echo '<option value="'.$i.'">'.$i.'</option>';

            }      

           ?> </select>
</select></br>
   Fecha Fin: Dia:<select name="diaf" class="input" id="diaf">

              <?

              for ($i = 1; $i < 32; $i++)

            {

               if ($i < 10)

                  echo '<option value="0'.$i.'">0'.$i.'</option>';

               else

                  echo '<option value="'.$i.'">'.$i.'</option>';

            }      

           ?>  </select>
   Mes: <select name="mesf" class="input" id="mesf">

              <?

              for ($i = 1; $i < 13; $i++)

            {

               if ($i < 10)

                  echo '<option value="0'.$i.'">0'.$i.'</option>';

               else

                  echo '<option value="'.$i.'">'.$i.'</option>';

            }      

           ?> </select>
   Anio:  <select name="aniof" class="input" id="aniof">

              <?

              for ($i = 2007; $i < 2100; $i++)

            {

               if ($i < 10)

                  echo '<option value="0'.$i.'">0'.$i.'</option>';

               else

                  echo '<option value="'.$i.'">'.$i.'</option>';

            }      

           ?> </select></br>

   <input name="opcion" type="submit" value="Alquilar"></br></br>
   
   Cancelar un alquiler</br>
   Alquiler: <select name="autoalquilado">
<?   
     $sql= "SELECT descripcion FROM alquiler JOIN usuario ON alquiler.idusuario = usuario.idusuario JOIN auto ON alquiler.idauto = auto.idauto WHERE (usuario.nombre = '$_SESSION[usuario]')";

     $consulta=mysql_query($sql);

     while($array = mysql_fetch_array($consulta))

     {

        echo "<option value=$array[0]>$array[0]</option>";

     }   

?>
   </select>
   <input name="opcion" value="Cancelar" type="submit"></br></br>
   
   
   Consultar todos los alquileres realizados:
   <input name="opcion" type="submit" value="Consultar">
   </select></br></br>
   Informar la cantidad de veces que se alquilo cada auto
   <input type="submit" name="opcion" value="Informar"></br></br>

<?
   $autoalquilar = $_POST['autoaalquilar'];
   $fechai = $_POST['anioi']."-".$_POST['mesi']."-".$_POST['diai'];
   $fechaf = $_POST['aniof']."-".$_POST['mesf']."-".$_POST['diaf'];   
   $autoacancelar = $_POST['autoalquilado'];
   
   
   $opcion=$_POST['opcion'];
   switch($opcion)

         {

            case "Alquilar":

            {
               Alquilar($autoalquilar,$fechai,$fechaf);
               break;

            }
            case "Cancelar":
            {
               Cancelar($autoacancelar);
               break;
            }
            case "Consultar":
            {
               Consultar();
               break;
            }
            case "Informar":
            {
               Informar();
               break;
            }
         }

   include('pie.php');

   }   
?>
</div>

The problem is that when the user clicks on "Alquilar" button, the echo is properly showed on screen but the values are the same the database had before updating it. How can i update the page that shows the information at the same time the user clicks the button?

Thanks in advance
« Last Edit: September 16, 2007, 01:46:31 PM by ferpadro »

Offline Dragen

  • Devotee
  • Posts: 1,153
  • Gender: Male
  • Life's Peachy...
    • View Profile
    • Gimp Productions
Re: Re-send data after updating a DB
« Reply #1 on: September 16, 2007, 01:46:50 PM »
where are you echoing the data? it needs to be colected and echoed after it's been updated.
Handmade crafts - gimp crafts
Web design - gimp production

Offline rarebit

  • Devotee
  • Posts: 996
    • View Profile
Re: Re-send data after updating a DB
« Reply #2 on: September 16, 2007, 01:55:07 PM »
Dragen is correct, you get through the whole script before you call the 'Alquilar()' function, which then inserts and updates.