Probleme "!empty($_POST"
Posté : 18 oct. 2010, 20:20
Bonjour à tous,
J'ai créé un formulaire de recherche avec les champs "N° licence" et "nom joueur" ... je récupère donc "$_POST['nom_joueur']" ou $_POST['licence']
Mon problème c'est que lorsque rien n'est indiqué dans les champs, tous les joueurs sont listés
ce qui génère un temps de chargement de la page important sans intérêt...
Voici mon code :
Merci d'avance pour votre aide.
J'ai créé un formulaire de recherche avec les champs "N° licence" et "nom joueur" ... je récupère donc "$_POST['nom_joueur']" ou $_POST['licence']
Mon problème c'est que lorsque rien n'est indiqué dans les champs, tous les joueurs sont listés
Voici mon code :
Code : Tout sélectionner
mysql_query("SET NAMES 'utf8'");
$sql = 'SELECT joueur.id_joueur, joueur.nom, joueur.prenom, joueur.date_naiss, joueur.rang, '
.'joueur.div_N, equipe.label_equipe, association.label_asso '
.'FROM association JOIN etablissement ON association.id_asso = etablissement.id_asso '
.'JOIN equipe ON etablissement.id_etabl = equipe.id_etabl '
.'JOIN joueur ON equipe.id_equipe = joueur.id_equipe '
.'WHERE div_N <> "" ';
if(isset($_POST['nom_joueur']) && !empty($_POST['nom_joueur']))
{
$sql = $sql.'AND joueur.nom = "'.quote_smart($_POST['nom_joueur']).'" ';
}
if(isset($_POST['licence']) && !empty($_POST['licence']))
{
$sql = $sql.'AND joueur.id_joueur = "'.quote_smart($_POST['licence']).'" ';
}
$req_joueur = mysql_query($sql) or die('Erreur SQL !<br />'.$req_joueur.'<br />'.mysql_error());
if(($data = mysql_fetch_array($req_joueur)) != NULL)
{
echo '<table bgcolor="#FFFFFF" width="650px">'."\n";
echo '<tr height="25" id="legend">';
echo '<td align="center" class="Style2"><b>Nom</b></td>';
echo '<td align="center" class="Style2"><b>Prénom</b></td>';
echo '<td align="center" class="Style2"><b>Equipe</b></td>';
echo '<td align="center" class="Style2"><b>Division</b></td>' ;
echo '<td align="center" class="Style2"><b>N° licence</b></td>' ;
echo '<td align="center" class="Style2"><b>Rang</b></td>';
echo '<td align="center" class="Style2"><b>Association</b></td>';
echo '</tr>'."\n";
do
{
echo '<tr>';
echo '<td bgcolor="#f3fbff" class="Style1" height="25"> '.$data['nom'].'</td>';
echo '<td bgcolor="#f3fbff" class="Style1" height="25"> '.$data['prenom'].'</td>';
echo '<td bgcolor="#f3fbff" align="center" class="Style1" height="25">'.$data['label_equipe'].'</td>';
echo '<td bgcolor="#f3fbff" align="center" class="Style1" height="25">'.$data['div_N'].'</td>';
echo '<td bgcolor="#f3fbff" align="center" class="Style1" height="25">'.$data['id_joueur'].'</td>';
echo '<td bgcolor="#f3fbff" align="center" class="Style1" height="25">'.$data['rang'].'</td>';
echo '<td bgcolor="#f3fbff" align="center" class="Style1" height="25">'.$data['label_asso'].'</td>';
echo '</tr>'."\n";
}while(($data = mysql_fetch_array($req_joueur)) != NULL);
echo '</table>'."\n";
?>
<br />
<?php
}
else
{
}
$sql = 'SELECT joueur.id_joueur, joueur.nom, joueur.prenom, joueur.date_naiss, joueur.rang, '
.'joueur.div_N, etablissement.label_etabl, association.label_asso '
.'FROM joueur JOIN espoir ON joueur.id_espoir = espoir.id_espoir '
.'JOIN etablissement ON etablissement.id_etabl = espoir.id_etabl '
.'JOIN association ON association.id_asso = etablissement.id_asso '
.'WHERE joueur.div_N <> "" ';
if(isset($_POST['nom_joueur']) && !empty($_POST['nom_joueur']))
{
$sql = $sql.'AND joueur.nom = "'.quote_smart($_POST['nom_joueur']).'" ';
}
if(isset($_POST['licence']) && !empty($_POST['licence']))
{
$sql = $sql.'AND joueur.id_joueur = "'.quote_smart($_POST['licence']).'" ';
}
$req_esp = mysql_query($sql) or die('Erreur SQL !<br />'.$req_esp.'<br />'.mysql_error());
if(($data = mysql_fetch_array($req_esp)) != NULL)
{
echo '<table bgcolor="#FFFFFF" width="650px">'."\n";
echo '<tr height="25" id="legend">';
echo '<td align="center" class="Style2"><b>Nom</b></td>';
echo '<td align="center" class="Style2"><b>Prénom</b></td>';
echo '<td align="center" class="Style2"><b>Etablissement</b></td>';
echo '<td align="center" class="Style2"><b>Division</b></td>' ;
echo '<td align="center" class="Style2"><b>N° licence</b></td>' ;
echo '<td align="center" class="Style2"><b>Rang</b></td>';
echo '<td align="center" class="Style2"><b>Association</b></td>';
echo '</tr>'."\n";
do
{
echo '<tr>';
echo '<td bgcolor="#f3fbff" class="Style1" height="25"> '.$data['nom'].'</td>';
echo '<td bgcolor="#f3fbff" class="Style1" height="25"> '.$data['prenom'].'</td>';
echo '<td bgcolor="#f3fbff" align="center" class="Style1" height="25">'.$data['label_etabl'].'</td>';
echo '<td bgcolor="#f3fbff" align="center" class="Style1" height="25">'.$data['div_N'].'</td>';
echo '<td bgcolor="#f3fbff" align="center" class="Style1" height="25">'.$data['id_joueur'].'</td>';
echo '<td bgcolor="#f3fbff" align="center" class="Style1" height="25">'.$data['rang'].'</td>';
echo '<td bgcolor="#f3fbff" align="center" class="Style1" height="25">'.$data['label_asso'].'</td>';
echo '</tr>'."\n";
}while(($data = mysql_fetch_array($req_esp)) != NULL);
echo '</table>'."\n";
}
elseif(($data = mysql_fetch_array($req_joueur)) != NULL)
{
echo "<p>Aucun résultat ne correspond à votre recherche.</p>";
}
elseif(($data = mysql_fetch_array($req_esp)) != NULL)
{
echo "<p>Aucun résultat ne correspond à votre recherche dans les joueurs Espoirs.</p>";
}
?>