Help with my mysql_query in PHP.

Hi ! Its me again, it turns out that im having a problem with one query.
Here`s the code :

<!--  COMENTARIOS


- Este es el modulo que le permite al usuario inscribirse a los nuevos eventos.
-Cuando este modulo se corre se supone que ya se haya logueado un usuario
-->


<head>
	<title>	Inscribirse a un nuevo evento </title>
</head>
<body>

	<h1> Seleccione el evento al que se quiere inscribir  </h1>

	<?php
		include ("..\..\connect.php") ; // Este include contiene las instrucciones necesarias para que el programa se conecte a la base de datos.
		
		$userlogueado = $_COOKIE['userlogueado'] ;
		echo "El user logueado es :".$userlogueado ; 

		$query  =
		
		mysql_query ("select evento.cod_event, 
		evento.comienzo,
		evento.fin,
		evento.coste,
		evento.nombre_event 
		from evento
		INNER JOIN usuario_evento
		WHERE (usuario_evento.cod_event = evento.cod_event  )
	");
	
				
		echo " <form method = 'POST'> " ; 
		echo " Usted se puede registrar a los siguientes eventos : </br>"; 
		echo " <table border = '1' ><tr><td>Codigo de evento</td><td>Comienzo</td><td>Fin</td><td>Costo</td><td>Nombre del evento</td><td>Seleccion de evento</td></tr>";

		while($dato=  mysql_fetch_array($query))
		{
			echo "<tr>";
			echo "<td>".$dato["cod_event"]."</td>" ;
			echo "<td>".$dato["comienzo"]."</td>"  ;
			echo "<td>".$dato["fin"]."</td>"  ;
			echo "<td>".$dato["coste"]."</td>"  ;
			echo "<td>".$dato["nombre_event"]."</td>" ;	
			
			if (isset($aux_cod_event) && $aux_cod_event == $dato["cod_event"] ) 
			{ 					
				echo "<td><input type='radio' name='aux_cod_event' checked value= '" . $dato["cod_event"] . "'></td>";
			} 
				else 
			{
				echo "<td><input type='radio' name='aux_cod_event' value= '" . $dato["cod_event"] . "'></td>";
			};
			echo "</tr>";
		}	
		
		echo "</table>";			

		echo "<input type  ='submit' name='submit' value='Registrarme'/> " ;
		echo "</form>";
		
		if (isset($_POST['submit'])) 
		{	
		    $aux_cod_event = $_POST['aux_cod_event'];
			echo $aux_cod_event."<br>";

			mysql_query("INSERT INTO usuario_evento VALUES ($aux_cod_event,$userlogueado)")
			or die ("La operacion no ha podido ser llevada a cabo");
			mysql_close($link);
			echo 'Se ha registrado exitosamente </br>';
		
		}
		 
		 
		echo "Eventos a los que se encuentra registrado "; 
	
		$query  = mysql_query ("select evento.cod_event, 
					evento.comienzo,
					evento.fin,
					evento.coste,
					evento.nombre_event 
					from evento 
					INNER JOIN usuario_evento
					WHERE evento.cod_event = usuario_evento.cod_event ");
		
		echo " <form method = 'POST'> " ; 
		echo " <table border = '1' ><tr><td>Codigo de evento</td><td>Comienzo</td><td>Fin</td><td>Costo</td><td>Nombre del evento</td></tr>";

		while($dato =  mysql_fetch_array($query))
		{
			echo "<tr>";
			echo "<td>".$dato["cod_event"]."</td>" ;
			echo "<td>".$dato["comienzo"]."</td>"  ;
			echo "<td>".$dato["fin"]."</td>"  ;
			echo "<td>".$dato["coste"]."</td>"  ;
			echo "<td>".$dato["nombre_event"]."</td>" ;	
		}	
	?>
</body>

What I need to do in the first query is that It must shown the data from events that the user is NOT registered, exactly the opposite of the second query where it must only shown the events that the user is registered(the second query works just fine) the only problem is with the first query.

if this is the second query
[php]$query = mysql_query ("select evento.cod_event,
evento.comienzo,
evento.fin,
evento.coste,
evento.nombre_event
from evento
INNER JOIN usuario_evento
WHERE evento.cod_event = usuario_evento.cod_event "[/php]

then to reverse it:
[php]$query = mysql_query ("select evento.cod_event,
evento.comienzo,
evento.fin,
evento.coste,
evento.nombre_event
from evento
INNER JOIN usuario_evento
WHERE evento.cod_event != usuario_evento.cod_event "[/php]

Yeah, ive already tried that. . . . I mean, the secon query works. The problem is with the first one. and I already tried what you said.

Try This

[php]$query = mysql_query (“select evento.cod_event,
evento.comienzo,
evento.fin,
evento.coste,
evento.nombre_event
from evento
INNER JOIN usuario_evento
WHERE evento.cod_event not in (select distinct usuario_evento.cod_event from usuario_evento)”[/php]

or

[php]$query = mysql_query (“select evento.cod_event,
evento.comienzo,
evento.fin,
evento.coste,
evento.nombre_event
from evento
INNER JOIN usuario_evento
WHERE not exists (select 1 from usuario_evento a where a.cod_event = evento.cod_event)”[/php]

Thank you topcoder ! It almost work except for the fact that if the user is registered in one event then the first query repeat all of the events that user is not registered. An id the user is registered to 2 events then the first query shows the rest of the events 2 times. Ill show you what happends trought images :

It corresponds to the user registered in none, 1, 2, and 3 :


registered0.jpg

registered1.jpg

Registered 2.jpg

Registered 3.jpg

The below queries should work, sorry my mistake…

[php] $query = mysql_query ("select distinct evento.cod_event,
evento.comienzo,
evento.fin,
evento.coste,
evento.nombre_event
from evento
WHERE evento.cod_event not in (select distinct usuario_evento.cod_event from usuario_evento)[/php]

or

[php]$query = mysql_query (“select distinct evento.cod_event,
evento.comienzo,
evento.fin,
evento.coste,
evento.nombre_event
from evento
WHERE not exists (select 1 from usuario_evento a where a.cod_event = evento.cod_event)”[/php]

Thanks top coder ! You are the best. It works just as I want it to.

Thanks for saying that! ;D

I find an HUGE error that I didn´t realize before. You see, when Ill try with a different user it appears that the different user is registered to the events that the last one was. I mean, the primary key isnt only cod.event, but cod.event AND idlogin_user.

The two querys should shown where THAT specific user is not registered, not where none of the users is registered to the event.

This are my tables :


It’s easy to fix, but I don’t see a variable anywhere in your code that you have posted that contains the login user id.

Unless this is it $userlogueado? I don’t read any language except english well.

Yeah, that is exactly the variable that takes de Id from the user that is logued. That`s is the variable that contains de login user ID. so what should I do ?

Either of these should work. You’re also going to have to adjust your query that pulls all the events the user is registered in.

[php]$query = mysql_query (“select distinct evento.cod_event,
evento.comienzo,
evento.fin,
evento.coste,
evento.nombre_event
from evento
WHERE evento.cod_event not in (select distinct usuario_evento.cod_event from usuario_evento where idlogin_user = '” . $userlogueado . “’”);[/php]

or

[php]$query = mysql_query (“select distinct evento.cod_event,
evento.comienzo,
evento.fin,
evento.coste,
evento.nombre_event
from evento
WHERE not exists (select 1 from usuario_evento a where a.cod_event = evento.cod_event and idlogin_user = '” . $userlogueado . “’”);
[/php]

Well. . . is giving me boolean back :

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\colegio\Principal\LoginIntranet\usuario\evento\evento.php on line 59.

Also, how do I change the second query to make it work?

Sorry my mistake, I forgot the ending )

[php] $query = mysql_query (“select distinct evento.cod_event,
evento.comienzo,
evento.fin,
evento.coste,
evento.nombre_event
from evento
WHERE evento.cod_event not in (select distinct usuario_evento.cod_event from usuario_evento where idlogin_user = '” . $userlogueado . “’)”);[/php]

or

[php]$query = mysql_query (“select distinct evento.cod_event,
evento.comienzo,
evento.fin,
evento.coste,
evento.nombre_event
from evento
WHERE not exists (select 1 from usuario_evento a where a.cod_event = evento.cod_event and idlogin_user = '” . $userlogueado . “’)”);[/php]

Finally ! ! ! IT WORKS ! ! ! Top coder, you`ve earn my eternal gratitude. If you ever need anything that I can do, ask it for it.
Really, thank you very much.

Sponsor our Newsletter | Privacy Policy | Terms of Service