Problème d'affichag du resultat de ma requete
RMAN
-
Alain_42 Messages postés 5361 Date d'inscription Statut Membre Dernière intervention -
Alain_42 Messages postés 5361 Date d'inscription Statut Membre Dernière intervention -
Bonjour,
En fait je suis confonteé à un petit problème d'affichage d'une requète. J'ai ma table eleve avec un champ matricule. Dans un formulaire j'ai un champ qui se nomme mat. je voudrais que quand je tape un numero matricule et que l'on click sur le bouton Rechercher que dans la mème page, on m'affiche l'élève correspondant. si l'on ne tape rien dans le champ mat, obtenir dans la mème page la liste de tous les élèves.
Merci de m'aider
Voici mon code:
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("DB");
//-----------------------------------------------------
// Vérification 1 : est-ce qu'on veut poster une news ?
//-----------------------------------------------------
$retour = mysql_query ('SELECT * FROM eleve ORDER BY nom');
?>
<form name="recherche" action="#" method="post">
<fieldset><legend>Recherche</legend>
<table width="100%">
<tr>
<td width="24%"> Matricule:</td>
<td width="28%"><input name="mat" type="text" size="33" /></td>
<td width="48%">
<input name="ok" type="button" value="Rechercher" onclick="
<?php
$mat = addslashes($_POST['mat']);
if ( mat != ""){
$retour = mysql_query ('SELECT * FROM eleveWHERE matricule = '. $mat. ' ');
}
?>"/>
</td>
</tr>
</table>
</fieldset>
</form>
<fieldset><legend><i><b>Liste des admis</b></i></legend>
<div class="affiche">
<table width="100%" border="0">
<tr>
<th> Matricule</th>
<th> Nom</th>
<th> Prenom</th>
</tr>
<?php
while ($donnees = mysql_fetch_array($retour))
{
?>
<tr>
<td><?php echo stripslashes($donnees['matricule']); ?></td>
<td><?php echo stripslashes($donnees['nom']); ?></td>
<td><?php echo stripslashes($donnees['prenom']); ?></td>
</tr>
<?php
}
?>
</table>
En fait je suis confonteé à un petit problème d'affichage d'une requète. J'ai ma table eleve avec un champ matricule. Dans un formulaire j'ai un champ qui se nomme mat. je voudrais que quand je tape un numero matricule et que l'on click sur le bouton Rechercher que dans la mème page, on m'affiche l'élève correspondant. si l'on ne tape rien dans le champ mat, obtenir dans la mème page la liste de tous les élèves.
Merci de m'aider
Voici mon code:
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("DB");
//-----------------------------------------------------
// Vérification 1 : est-ce qu'on veut poster une news ?
//-----------------------------------------------------
$retour = mysql_query ('SELECT * FROM eleve ORDER BY nom');
?>
<form name="recherche" action="#" method="post">
<fieldset><legend>Recherche</legend>
<table width="100%">
<tr>
<td width="24%"> Matricule:</td>
<td width="28%"><input name="mat" type="text" size="33" /></td>
<td width="48%">
<input name="ok" type="button" value="Rechercher" onclick="
<?php
$mat = addslashes($_POST['mat']);
if ( mat != ""){
$retour = mysql_query ('SELECT * FROM eleveWHERE matricule = '. $mat. ' ');
}
?>"/>
</td>
</tr>
</table>
</fieldset>
</form>
<fieldset><legend><i><b>Liste des admis</b></i></legend>
<div class="affiche">
<table width="100%" border="0">
<tr>
<th> Matricule</th>
<th> Nom</th>
<th> Prenom</th>
</tr>
<?php
while ($donnees = mysql_fetch_array($retour))
{
?>
<tr>
<td><?php echo stripslashes($donnees['matricule']); ?></td>
<td><?php echo stripslashes($donnees['nom']); ?></td>
<td><?php echo stripslashes($donnees['prenom']); ?></td>
</tr>
<?php
}
?>
</table>
A voir également:
- Problème d'affichag du resultat de ma requete
- Resultat foot - Télécharger - Vie quotidienne
- Lexer resultat - Télécharger - Sport
- Problème affichage page internet google chrome ✓ - Forum Google Chrome
- Resultat loto d'aujourd'hui rdc ✓ - Forum Excel
- Problème affichage fenêtre windows 10 - Guide
1 réponse
comme ça:
<?php //init valeur matricule à vide pour le premier affichage du formulaire $matricule=""; if(isset($_POST['rechercher'])){//le formulaire est posté //recupération valeur du champ mat du formulaire if(!empty($_POST['mat']){ //si matricule saisi $matricule=$_POST['mat']; } //connexion base mysql_connect("localhost", "root", ""); mysql_select_db("DB"); //requette if($matricule!=""){ //matricule saisi donc on sort seulement l'éléve correspondant $query="SELECT * FROM eleve WHERE matricule='".mysql_real_escape_string($matricule)."' "; }else{ //pas de matricule saisi donc tous les éléves $query="SELECT * FROM eleve ORDER BY nom"; } //soumission de la requette $resultat=mysql_query($query); } ?> <form name="recherche" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <fieldset><legend>Recherche</legend> <table width="100%"> <tr> <td width="24%"> Matricule:</td> <td width="28%"><input name="mat" type="text" size="33" value="<?php echo $matricule; ?>" /></td> <!-- ci dessus echo $matricule; permet de conserver le numéro matricule saisi lors du réaffichage du formulaire --> <td width="48%"> <!-- onclick est un evenement javascript tu ne peux pas y faire du php ! --> <input name="rechercher" type="submit" value="Rechercher" /> </td> </tr> </table> </fieldset> </form> <?php //on n'affiche cette partie que quand le bouton recherche a été cliqué if(isset($_POST['rechercher'])){//le formulaire est posté, (bouton recherche cliqué) ?> <fieldset><legend><i><b>Liste des admis</b></i></legend> <div class="affiche"> <table width="100%" border="0"> <tr> <th> Matricule</th> <th> Nom</th> <th> Prenom</th> </tr> <?php while ($donnees = mysql_fetch_assoc($resultat)) { ?> <tr> <td><?php echo stripslashes($donnees['matricule']); ?></td> <td><?php echo stripslashes($donnees['nom']); ?></td> <td><?php echo stripslashes($donnees['prenom']); ?></td> </tr> <?php } ?> </table> <?php } //fin du if(isset($_POST['rechercher'])) ?>