Foros del Web » Programando para Internet » PHP »

Consulta php y mysql. Repite datos.

Estas en el tema de Consulta php y mysql. Repite datos. en el foro de PHP en Foros del Web. Hola, nuevamente recurro a vosotros porque tengo un código PHP el cual se conecta a la bd para el inicio de sesion. En caso que ...
  #1 (permalink)  
Antiguo 24/01/2015, 06:46
 
Fecha de Ingreso: junio-2009
Mensajes: 16
Antigüedad: 14 años, 10 meses
Puntos: 0
Información Consulta php y mysql. Repite datos.

Hola, nuevamente recurro a vosotros porque tengo un código PHP el cual se conecta a la bd para el inicio de sesion. En caso que el usuario se loguea de forma correcta le aparece una tabla con los respectivos botones.
Pero resulta que me aparece la tabla repetida con todos los botones.
No se donde le estoy errando, y estoy bastante olvidado con PHP.
Código PHP:
Ver original
  1. <?php
  2. ?>
  3. <html> 
  4.     <head>
  5.             <title>Página privada alumnos y profesores.</title>
  6. </head>
  7. <body class="fondo">
  8.     <div id="menu">
  9.       <asp:Repeater ID="MenuItems" runat="server">
  10.           <ItemTemplate>
  11.          
  12. <table align="center">
  13.     <tr>
  14.         <td>
  15.        
  16.              <?php
  17. if (isset($_SESSION['k_username'])) {
  18.    
  19. $link = @mysql_connect ("localhost", "root","root");
  20. $conex=@mysql_select_db("examen",$link);
  21. $usuario = email;
  22. $sql = "SELECT *
  23.        FROM persona
  24.        WHERE email=$usuario";
  25.        
  26. $result = mysql_query($sql,$link);
  27.  
  28.  while ($reg = mysql_fetch_array($result)) {
  29.    
  30.  echo "<table border='1px' text-align='center'>";
  31.  
  32.    
  33.     echo "<tr>";
  34.         echo "<td>";echo 'Bienvenido, '.$_SESSION['ncompleto'];
  35.         echo "</td>";
  36.        
  37.         echo "<td>";
  38.             echo '<a href="logout.php" >(Salir)</a> ';
  39.         echo "</td>";
  40.     echo "</tr>";
  41.        
  42.     echo "<tr>";
  43.         echo "<td colspan='2' align='center'>Examenes</td>";
  44.     echo "</tr>";
  45.    
  46.     echo "<tr>";
  47.    
  48.         echo "<td>";
  49.             echo '<input type="button" value="Ver examenes." onclick="window.location.href=\'examenes/verexamenes.php\'"/>';
  50.         echo "</td>";
  51.        
  52.         echo "<td>";
  53.             echo '<input type="button" value="Agregar examenes." onclick="window.location.href=\'examenes/altas/formAltasDeExamenes.php\'"/>';
  54.         echo "</td>";
  55.     echo "</tr>";
  56.    
  57.     echo "<tr>";
  58.         echo "<td colspan='2' align='center'>Personas</td>";
  59.     echo "</tr>";
  60.     echo "<tr>";
  61.    
  62.         echo "<td>";
  63.             echo '<input type="button" value="Ver personas." disabled="true"/>';
  64.         echo "</td>";
  65.        
  66.         echo "<td>";
  67.             echo '<input type="button" value="Agregar personas." onclick="window.location.href=\'persona/altaspersona/altapersona.htm\'"/>';
  68.         echo "</td>";
  69.     echo "</tr>";
  70.    
  71.         echo "<tr>";
  72.         echo "<td colspan='2' align='center'>Profesores</td>";
  73.     echo "</tr>";
  74.     echo "<tr>";
  75.    
  76.         echo "<td>";
  77.             echo '<input type="button" value="Ver profesores." disabled="true"/>';
  78.         echo "</td>";
  79.        
  80.         echo "<td>";
  81.             echo '<input type="button" value="Agregar profesores." disabled=tue/>';
  82.         echo "</td>";
  83.     echo "</tr>";
  84.    
  85.    
  86.     echo "<tr>";
  87.         echo "<td colspan='2' align='center'>Alumnos</td>";
  88.     echo "</tr>";
  89.    
  90.     echo "<tr>";
  91.         echo "<td>";
  92.             echo '<input type="button" value="Ver alumnos." disabled="true"/>';
  93.         echo "</td>";
  94.        
  95.         echo "<td>";
  96.             echo '<input type="button" value="Agregar alumnos." disabled=tue/>';
  97.         echo "</td>";
  98.     echo "</tr>";
  99.    
  100.  
  101.     echo "<tr>";
  102.         echo "<td colspan='2' align='center'>Grupo</td>";
  103.     echo "</tr>";
  104.    
  105.     echo "<tr>";
  106.         echo "<td>";
  107.             echo '<input type="button" value="Ver grupos." disabled="true"/>';
  108.         echo "</td>";
  109.        
  110.         echo "<td>";
  111.             echo '<input type="button" value="Agregar grupos." disabled=tue/>';
  112.         echo "</td>";
  113.     echo "</tr>";
  114.    
  115.    
  116.         echo "<tr>";
  117.         echo "<td colspan='2' align='center'>Instituto.</td>";
  118.     echo "</tr>";
  119.    
  120.     echo "<tr>";
  121.         echo "<td>";
  122.             echo '<input type="button" value="Ver instituto." disabled="true"/>';
  123.         echo "</td>";
  124.        
  125.         echo "<td>";
  126.             echo '<input type="button" value="Agregar instituto." disabled=tue/>';
  127.         echo "</td>";
  128.     echo "</tr>";
  129.  echo "</table>";
  130.  
  131.              }
  132.  
  133. echo "<div class='fecha'>";
  134.             echo "Hoy es ";
  135.             echo date("l ");
  136.             echo date("d ");
  137.             echo "de ";
  138.             echo date("F ");
  139.             echo "de ";
  140.             echo date("Y");
  141.             echo "</div>";
  142.                
  143. @mysql_close ($link);
  144. }else{
  145.     echo '<img src=materiales/error.jpg width=50px height=50px></br>Usted no se ha logueado correctamente. Por favor hagalo clickeando <a href="login.php">aqui.</a></p>';
  146. }
  147.  
  148.     ?>
  149.         </td>
  150.         <td align="rigth">
  151.        
  152.         </td>
  153.     </tr>  
  154. </table>
  155.  
  156.          </ItemTemplate>
  157.     </asp:Repeater>
  158.       </div>
  159. </body>
  160. </html>
Gracias a quien me pueda ayudar.
  #2 (permalink)  
Antiguo 24/01/2015, 06:52
 
Fecha de Ingreso: julio-2008
Mensajes: 543
Antigüedad: 15 años, 9 meses
Puntos: 56
Respuesta: Consulta php y mysql. Repite datos.

y has probado a ejecutar las consulta en el phpmyadmin, a ver si es que te devuelve más de un resultado (o si te los devuelve correctamente)?

De todos modos, hay una cosa que no entiendo. Para que haces el select? luego o utilizas ningun dato de ahi en tu tabla...
__________________
Un saludo! :-)
Quiero ser el primero
  #3 (permalink)  
Antiguo 24/01/2015, 06:55
 
Fecha de Ingreso: junio-2009
Mensajes: 16
Antigüedad: 14 años, 10 meses
Puntos: 0
Respuesta: Consulta php y mysql. Repite datos.

Para hacer el login, y traigo el email y el nombre del usuario.
Lo que se me repite es la tabla con los botones.

Última edición por guillegodino; 24/01/2015 a las 07:00
  #4 (permalink)  
Antiguo 24/01/2015, 07:03
 
Fecha de Ingreso: julio-2008
Mensajes: 543
Antigüedad: 15 años, 9 meses
Puntos: 56
Respuesta: Consulta php y mysql. Repite datos.

pero... si compruebas si esta logueado ANTES de hacer la consulta , la cual haces solo si lo está:
Código:
if (isset($_SESSION['k_username'])) {
    
$link = @mysql_connect ("localhost", "root","root");
$conex=@mysql_select_db("examen",$link);
$usuario = email;
$sql = "SELECT * 
        FROM persona 
        WHERE email=$usuario";
       
$result = mysql_query($sql,$link);
 
 while ($reg = mysql_fetch_array($result)) {
y bueno, debo de estar quedandome aun mas cegata, pero no veo donde utilzas el email...

de todos modos, ve visto otra cosa que puede que sea la fuente de tus duplicados,

Código:
<asp:Repeater ID="MenuItems" runat="server">
Segun la web de w3school, The Repeater control is used to display a repeated list of items that are bound to the control. . No podria ser eso? Lo has puesto por encima y te lo hace 2 veces (que la verdad es que no se como funciona porque no lo he usado nunca, ademas veo que eso es de ASP)

Por cierto, has probado que la query es correcta y te da solamente un resultado?
__________________
Un saludo! :-)
Quiero ser el primero
  #5 (permalink)  
Antiguo 24/01/2015, 07:05
 
Fecha de Ingreso: julio-2008
Mensajes: 543
Antigüedad: 15 años, 9 meses
Puntos: 56
Respuesta: Consulta php y mysql. Repite datos.

otra cosa, en algunos botones has puesto
Código:
disabled=tue
en lugar de
Código:
disabled="true"
__________________
Un saludo! :-)
Quiero ser el primero
  #6 (permalink)  
Antiguo 24/01/2015, 07:13
 
Fecha de Ingreso: junio-2009
Mensajes: 16
Antigüedad: 14 años, 10 meses
Puntos: 0
Respuesta: Consulta php y mysql. Repite datos.

Los botones estan disabled porque no estan en funcionamiento aún.
Lo que hice fue eliminar
Código HTML:
Ver original
  1. <asp:Repeater ID="MenuItems" runat="server">
y sigue repitiendome los botones.
Es como si estuviera mal el
Código HTML:
Ver original
  1. while
.
Igual gracias por la ayuda. Seguiré tocando código haber si encuentro el detalle, porque estoy seguro que es un detalle por menor.
  #7 (permalink)  
Antiguo 24/01/2015, 12:22
 
Fecha de Ingreso: julio-2008
Mensajes: 543
Antigüedad: 15 años, 9 meses
Puntos: 56
Respuesta: Consulta php y mysql. Repite datos.

Cita:
Iniciado por guillegodino Ver Mensaje
Los botones estan disabled porque no estan en funcionamiento aún.
no me has entendido. que lo has escrito mal, no has puesto true si no tue, sin la R (y sin las comillas)
Cita:
Iniciado por guillegodino Ver Mensaje
Lo que hice fue eliminar
Código HTML:
Ver original
  1. <asp:Repeater ID="MenuItems" runat="server">
y sigue repitiendome los botones.
Pero sabes por qué o para qué tenias ese trozo de codigo ahi? Porque no va solo (supongo que esres consciente de ello), y segun contestas, parece que has quitado un trozo "a ver que pasa" sin más...
__________________
Un saludo! :-)
Quiero ser el primero
  #8 (permalink)  
Antiguo 25/01/2015, 23:54
 
Fecha de Ingreso: julio-2008
Mensajes: 543
Antigüedad: 15 años, 9 meses
Puntos: 56
Respuesta: Consulta php y mysql. Repite datos.

estaria bien saber si lo has podido solucionar, si te sirvio algo de lo que dije, si probaste algo.... algo!
__________________
Un saludo! :-)
Quiero ser el primero
  #9 (permalink)  
Antiguo 26/01/2015, 03:05
Avatar de Eleazan  
Fecha de Ingreso: abril-2008
Ubicación: Ibiza
Mensajes: 1.879
Antigüedad: 16 años
Puntos: 326
Respuesta: Consulta php y mysql. Repite datos.

Antes del while, haz:
Código PHP:
 echo mysql_num_rows(); 
__________________
>> Eleazan's Source
>> @Eleazan
  #10 (permalink)  
Antiguo 30/01/2015, 06:53
 
Fecha de Ingreso: junio-2009
Mensajes: 16
Antigüedad: 14 años, 10 meses
Puntos: 0
Respuesta: Consulta php y mysql. Repite datos.

Hola a todos, no lo he podido solucionar. De todas formas agradezco a todos los que han dedicado un poquito de su tiempo en tratar de ayudarme.
  #11 (permalink)  
Antiguo 30/01/2015, 07:21
Avatar de fede5426  
Fecha de Ingreso: diciembre-2014
Ubicación: Córdoba
Mensajes: 446
Antigüedad: 9 años, 4 meses
Puntos: 208
Respuesta: Consulta php y mysql. Repite datos.

La tabla esta dentro de un while, while es un bucle, quizas por eso se repiten los botones. Intenta sacando el while..

Almacena el array de la misma forma:

$reg = mysql_fetch_array($result)

y abajo todo el contenido que pusiste en el while.

Código PHP:
Ver original
  1. $reg = mysql_fetch_array($result);
  2.  
  3. echo "<table border='1px' text-align='center'>";
  4.  
  5.    
  6.     echo "<tr>";
  7.         echo "<td>";echo 'Bienvenido, '.$_SESSION['ncompleto'];
  8.         echo "</td>";
  9.        
  10.         echo "<td>";
  11.             echo '<a href="logout.php" >(Salir)</a> ';
  12.         echo "</td>";
  13.     echo "</tr>";
  14.        
  15.     echo "<tr>";
  16.         echo "<td colspan='2' align='center'>Examenes</td>";
  17.     echo "</tr>";
  18.    
  19.     echo "<tr>";
  20.    
  21.         echo "<td>";
  22.             echo '<input type="button" value="Ver examenes." onclick="window.location.href=\'examenes/verexamenes.php\'"/>';
  23.         echo "</td>";
  24.        
  25.         echo "<td>";
  26.             echo '<input type="button" value="Agregar examenes." onclick="window.location.href=\'examenes/altas/formAltasDeExamenes.php\'"/>';
  27.         echo "</td>";
  28.     echo "</tr>";
  29.    
  30.     echo "<tr>";
  31.         echo "<td colspan='2' align='center'>Personas</td>";
  32.     echo "</tr>";
  33.     echo "<tr>";
  34.    
  35.         echo "<td>";
  36.             echo '<input type="button" value="Ver personas." disabled="true"/>';
  37.         echo "</td>";
  38.        
  39.         echo "<td>";
  40.             echo '<input type="button" value="Agregar personas." onclick="window.location.href=\'persona/altaspersona/altapersona.htm\'"/>';
  41.         echo "</td>";
  42.     echo "</tr>";
  43.    
  44.         echo "<tr>";
  45.         echo "<td colspan='2' align='center'>Profesores</td>";
  46.     echo "</tr>";
  47.     echo "<tr>";
  48.    
  49.         echo "<td>";
  50.             echo '<input type="button" value="Ver profesores." disabled="true"/>';
  51.         echo "</td>";
  52.        
  53.         echo "<td>";
  54.             echo '<input type="button" value="Agregar profesores." disabled=tue/>';
  55.         echo "</td>";
  56.     echo "</tr>";
  57.    
  58.    
  59.     echo "<tr>";
  60.         echo "<td colspan='2' align='center'>Alumnos</td>";
  61.     echo "</tr>";
  62.    
  63.     echo "<tr>";
  64.         echo "<td>";
  65.             echo '<input type="button" value="Ver alumnos." disabled="true"/>';
  66.         echo "</td>";
  67.        
  68.         echo "<td>";
  69.             echo '<input type="button" value="Agregar alumnos." disabled=tue/>';
  70.         echo "</td>";
  71.     echo "</tr>";
  72.    
  73.  
  74.     echo "<tr>";
  75.         echo "<td colspan='2' align='center'>Grupo</td>";
  76.     echo "</tr>";
  77.    
  78.     echo "<tr>";
  79.         echo "<td>";
  80.             echo '<input type="button" value="Ver grupos." disabled="true"/>';
  81.         echo "</td>";
  82.        
  83.         echo "<td>";
  84.             echo '<input type="button" value="Agregar grupos." disabled=tue/>';
  85.         echo "</td>";
  86.     echo "</tr>";
  87.    
  88.    
  89.         echo "<tr>";
  90.         echo "<td colspan='2' align='center'>Instituto.</td>";
  91.     echo "</tr>";
  92.    
  93.     echo "<tr>";
  94.         echo "<td>";
  95.             echo '<input type="button" value="Ver instituto." disabled="true"/>';
  96.         echo "</td>";
  97.        
  98.         echo "<td>";
  99.             echo '<input type="button" value="Agregar instituto." disabled=tue/>';
  100.         echo "</td>";
  101.     echo "</tr>";
  102.  echo "</table>";

Probalo asi, saludos!
  #12 (permalink)  
Antiguo 30/01/2015, 08:00
Avatar de Carlos1975  
Fecha de Ingreso: mayo-2005
Mensajes: 29
Antigüedad: 18 años, 11 meses
Puntos: 0
Respuesta: Consulta php y mysql. Repite datos.

prueba esto:


Código PHP:
Ver original
  1. <?php session_start(); ?>
  2.  
  3. <html>
  4.  
  5. <head><title>Página privada alumnos y profesores.</title></head>
  6.  
  7. <body class="fondo">
  8.     <div id="menu">
  9.       <asp:Repeater ID="MenuItems" runat="server">
  10.           <ItemTemplate>
  11.              
  12. <?php
  13.  
  14. if ((isset($_SESSION['k_username'])) && ($_SESSION['k_username'] != "")) {
  15.    
  16.     $link = mysql_connect ("localhost", "root","root");
  17.     $conex = mysql_select_db("examen",$link);
  18.     $usuario = "email";
  19.     $sql = "SELECT * FROM persona WHERE email = ".$usuario."";
  20.            
  21.     $result = mysql_query($sql,$link);
  22.     $reg = mysql_fetch_array($result);
  23.    
  24.     while ($reg = mysql_fetch_array($result)) {
  25.    
  26.     // lo que tengas que repetir en forma de bucle
  27.    
  28.     }
  29.        
  30.     echo "<table border='1' text-align='center'>";
  31.     echo "<tr>";
  32.     echo "<td>Bienvenido, ".$_SESSION['ncompleto']."</td>";
  33.     echo "<td><a href='logout.php'>(Salir)</a></td>";
  34.     echo "</tr>";
  35.     echo "<tr><td colspan='2' align='center'>Examenes</td></tr>";
  36.     echo "<tr>";
  37.     echo "<td><input type='button' value='Ver examenes.' onclick='window.location.href='examenes/verexamenes.php''/></td>";
  38.     echo "<td><input type='button' value='Agregar examenes.' onclick='window.location.href='examenes/altas/formAltasDeExamenes.php''/></td>";
  39.     echo "</tr>";
  40.     echo "<tr><td colspan='2' align='center'>Personas</td></tr>";
  41.     echo "<tr>";
  42.     echo "<td><input type='button' value='Ver personas.' disabled /></td>";
  43.     echo "<td><input type='button' value='Agregar personas.' onclick='window.location.href='persona/altaspersona/altapersona.htm''/></td>";
  44.     echo "</tr>";
  45.     echo "<tr><td colspan='2' align='center'>Profesores</td></tr>";
  46.     echo "<tr>";
  47.     echo "<td><input type='button' value='Ver profesores.' disabled /></td>";
  48.     echo "<td><input type='button' value='Agregar profesores.' disabled /></td>";
  49.     echo "</tr>";
  50.     echo "<tr><td colspan='2' align='center'>Alumnos</td></tr>";
  51.     echo "<tr>";
  52.     echo "<td><input type='button' value='Ver alumnos.' disabled /></td>";
  53.     echo "<td><input type='button' value='Agregar alumnos.' disabled /></td>";
  54.     echo "</tr>";
  55.     echo "<tr><td colspan='2' align='center'>Grupo</td></tr>";
  56.     echo "<tr>";
  57.     echo "<td><input type='button' value='Ver grupos.' disabled /></td>";
  58.     echo "<td><input type='button' value='Agregar grupos.' disabled /></td>";
  59.     echo "</tr>";
  60.     echo "<tr><td colspan='2' align='center'>Instituto.</td></tr>";
  61.     echo "<tr>";
  62.     echo "<td><input type='button' value='Ver instituto.' disabled /></td>";
  63.     echo "<td><input type='button' value='Agregar instituto.' disabled /></td>";
  64.     echo "</tr>";
  65.     echo "</table>";
  66.          
  67.     echo "<div class='fecha'>";
  68.         echo "Hoy es ";
  69.         echo date("l ");
  70.         echo date("d ");
  71.         echo "de ";
  72.         echo date("F ");
  73.         echo "de ";
  74.         echo date("Y");
  75.     echo "</div>";
  76.                        
  77.     mysql_close ($link);
  78.    
  79. } else {
  80.        
  81.     $updateGoTo = "login.php";
  82.     if (isset($_SESSION['k_username'])) {
  83.         $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
  84.         $updateGoTo .= $_SESSION['k_username'];
  85.     }
  86.     header(sprintf("Location: %s", $updateGoTo));
  87. }
  88.        
  89. ?>
  90.      
  91.              </ItemTemplate>
  92.         </asp:Repeater>
  93.     </div>
  94. </body>
  95.  
  96. </html>
  #13 (permalink)  
Antiguo 30/01/2015, 08:17
Avatar de fede5426  
Fecha de Ingreso: diciembre-2014
Ubicación: Córdoba
Mensajes: 446
Antigüedad: 9 años, 4 meses
Puntos: 208
Respuesta: Consulta php y mysql. Repite datos.

Cita:
Iniciado por Carlos1975 Ver Mensaje
prueba esto:

Es basicamente lo mismo que le dije yo, quitar la tabla del while

Etiquetas: php+base+de+datos
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 22:22.