IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

PHP & Base de données Discussion :

[SQL] Moteur de recherche problème affichage donnée dans select


Sujet :

PHP & Base de données

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    295
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 295
    Points : 87
    Points
    87
    Par défaut [SQL] Moteur de recherche problème affichage donnée dans select
    Bonjour, j'ai 1 soucis avec mon moteur de recherche SQL.
    Il est donc cencé, après la saisie de mot(s) clé(s) dans 1 formulaire, effectuer une recherche SQL dans mes champs d'une TABLE,
    afin de vérifier si le(s) mot(s) sont contenus dans 1 des champs ( reprèsentant des nomFiches, descriptions, ( des champs, date,texte, entier,... ).
    Et donc afficher les nomFiche correspondant à la recherche dans mon select.

    j'affiche donc dans un select le résultat, seulement mon select est vide:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    $result = mysql_query($sql);
    			if ($result)
    				{
    				if (mysql_num_rows($result) == 0) 
    					{
    					// Si il n'y a pas de résultats
    					echo '<script language=javascript> alert ("Pas de résultat");</script>';
    					echo '<SCRIPT LANGUAGE="JavaScript">document.location.href="accueil.php" </SCRIPT>';
    					exit;
    					}else{
    						echo "<select name='searchselection'>";
    						while($datafichiers = mysql_fetch_assoc($result))
    							{
    							echo '<option>'.$row['nomFiche'].'</option>';
    							echo '<br>';
    							}
    						}
    					echo'</select>';
    				}
    	mysql_free_result($result);
    	}else{	
    		echo '<script language=javascript> alert ("Indiquez un mot clé pour effectuer une recherche");</script>';
    		echo '<SCRIPT LANGUAGE="JavaScript">document.location.href="accueil.php" </SCRIPT>';
    		exit;
    		}
    le code source donne:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    <select name='searchselection'><option></option><br><option></option>
    <br><option></option><br><option></option><br><option></option><br></select>

    la base ne contient que 5 nomFiche

    Il esai donc d'en afficher 5, mais

    1) la recherche ne selectionne pas elle affiche les 5

    2) rien n apparait dans mon select

  2. #2
    Membre régulier
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    295
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 295
    Points : 87
    Points
    87
    Par défaut
    j'ai oublier de poster la page complete avec la requete
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    <?php 
    include ('secure.php');
    include ('mysqlconnect.php'); 
    $projet = $_POST['btprojet'];
    $search = $_POST['textfieldsearch'];
    if(isset($search) && trim($search) != '') 
    	{
    	$search = str_replace('+', ' ', trim($search));
    	$search = str_replace('\'', ' ', $search);
    	$search = str_replace(',', ' ', $search);
    	$search = str_replace(':', ' ', $search);
    	$tabsearch = explode(' ' , $search );
    	//on compte le nombre de mots
    	$nbsearch = count($tabsearch);
    	//on prépare la requête SQL.
    	$sql = "select * 
    			from fiches
    			where numFiche ='".$tabsearch[0]."' 
    			or nomFiche like %'".$tabsearch[0]."'%
    			or datecreaFiche ='".$tabsearch[0]."'
    			or loginF ='".$tabsearch[0]."'
    			or descriptifFiche like %'".$tabsearch[0]."'%
    			or resolutionFiche like %'".$tabsearch[0]."'%
    			or nomProjet ='".$search."'
    			or nbhresolution = '".$tabsearch[0]."'
    			or enseigne = '".$tabsearch[0]."'
    			or privee ='".$tabsearch[0]."'
    			or emailF = '".$tabsearch[0]."'";
    			// on boucle pour integrer tous les mots dans la requête
    			$sql = "select * from fiches where 1"; 
    			for ($i = 0; $i < $nb; $i++)
    				{ 
    				$sql .= " AND ( numFiche='".$tabsearch[i]."'
    				or nomFiche like %'".$tabsearch[i]."'%
    				or datecreaFiche ='".$tabsearch[i]."'
    				or loginF ='".$tabsearch[i]."'
    				or descriptifFiche like %'".$tabsearch[i]."'%
    				or resolutionFiche like %'".$tabsearch[i]."'%
    				or nomProjet '".$search."'
    				or nbhresolution = '".$tabsearch[i]."'
    				or enseigne = '".$tabsearch[i]."'
    				or privee ='".$tabsearch[i]."' 
    				or emailF = '".$tabsearch[i]."' )"; 
    				}
    			// on execute la requête SQL.
    			$result = mysql_query($sql);
    			if ($result)
    				{
    				if (mysql_num_rows($result) == 0) 
    					{
    					// Si il n'y a pas de résultats
    					echo '<script language=javascript> alert ("Pas de résultat");</script>';
    					echo '<SCRIPT LANGUAGE="JavaScript">document.location.href="accueil.php" </SCRIPT>';
    					exit;
    					}else{
    						echo "<select name='searchselection'>";
    						while($datafichiers = mysql_fetch_assoc($result))
    							{
    							echo '<option>'.$row['nomFiche'].'</option>';
    							echo '<br>';
    							}
    						}
    					echo'</select>';
    				}
    	mysql_free_result($result);
    	}else{	
    		echo '<script language=javascript> alert ("Indiquez un mot clé pour effectuer une recherche");</script>';
    		echo '<SCRIPT LANGUAGE="JavaScript">document.location.href="accueil.php" </SCRIPT>';
    		exit;
    		}
    ?>

  3. #3
    Membre émérite
    Avatar de yiannis
    Profil pro
    Inscrit en
    Septembre 2005
    Messages
    1 494
    Détails du profil
    Informations personnelles :
    Âge : 59
    Localisation : France

    Informations forums :
    Inscription : Septembre 2005
    Messages : 1 494
    Points : 2 791
    Points
    2 791
    Par défaut
    bonjour,

    essai comme cela:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
                      while($datafichiers = mysql_fetch_assoc($result))
                         {
                         echo '<option>'.$datafichiers['nomFiche'].'</option>';
                         echo '<br>';
                         }
    car $row ne correspond a rien

  4. #4
    Membre régulier
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    295
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 295
    Points : 87
    Points
    87
    Par défaut
    Exact, l'affichage fonctionne, par contre j'ai 1 soucis, j'ai l'impression qu'il m'affiche toute les fiches de ma bdd.
    1 problème de code ? 1problème de requete ? 1 probleme de performance?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    				if(isset($search) && trim($search) != '') 
    					{
    					$search = str_replace('+', ' ', trim($search));
    					$search = str_replace('\'', ' ', $search);
    					$search = str_replace(',', ' ', $search);
    					$search = str_replace(':', ' ', $search);
    					$tabsearch = explode(' ' , $search );
    					//on compte le nombre de mots
    					$nbsearch = count($tabsearch);
    					//on prépare la requête SQL.
    					$sql = "select * 
    							from fiches
    							where numFiche ='".$tabsearch[0]."' 
    							or nomFiche like %'".$tabsearch[0]."'%
    							or datecreaFiche ='".$tabsearch[0]."'
    							or loginF ='".$tabsearch[0]."'
    							or descriptifFiche like %'".$tabsearch[0]."'%
    							or resolutionFiche like %'".$tabsearch[0]."'%
    							or nomProjet ='".$search."'
    							or nbhresolution = '".$tabsearch[0]."'
    							or enseigne = '".$tabsearch[0]."'
    							or privee ='".$tabsearch[0]."'
    							or emailF = '".$tabsearch[0]."'";
    							// on boucle pour integrer tous les mots dans la requête
    							$sql = "select * from fiches where 1"; 
    							for ($i = 0; $i < $nb; $i++)
    								{ 
    								$sql .= " AND ( numFiche='".$tabsearch[i]."'
    								or nomFiche like %'".$tabsearch[i]."'%
    								or datecreaFiche ='".$tabsearch[i]."'
    								or loginF ='".$tabsearch[i]."'
    								or descriptifFiche like %'".$tabsearch[i]."'%
    								or resolutionFiche like %'".$tabsearch[i]."'%
    								or nomProjet '".$search."'
    								or nbhresolution = '".$tabsearch[i]."'
    								or enseigne = '".$tabsearch[i]."'
    								or privee ='".$tabsearch[i]."' 
    								or emailF = '".$tabsearch[i]."' )"; 
    								}

  5. #5
    Rédacteur

    Avatar de Yogui
    Homme Profil pro
    Directeur technique
    Inscrit en
    Février 2004
    Messages
    13 721
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yonne (Bourgogne)

    Informations professionnelles :
    Activité : Directeur technique

    Informations forums :
    Inscription : Février 2004
    Messages : 13 721
    Points : 29 985
    Points
    29 985
    Par défaut
    Salut

    i ou $i ?
    Suggestion : utiliser str_replace() avec des arrays plutôt que 4 fois à la suite.
    Attention : dans le cas où ton token est vide, tu compares avec une chaîne vide sur tous tes champs, ce qui fait qu'ils retournent tous vrai. Cela ne m'étonne pas que tu aies toute la table dans le résultat ^^

    Vérifie à chaque fois que $tabsearch[$i] est !empty(), par exemple.

  6. #6
    Membre régulier
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    295
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 295
    Points : 87
    Points
    87
    Par défaut
    Exact c'etait bien $i, j'ai donc appliqué le !empty dans ma boucle ^^, seulement j'ai toujours toute mes fiches

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    					$sql = "select * 
    							from fiches
    							where numFiche ='".$tabsearch[0]."' 
    							or nomFiche like %'".$tabsearch[0]."'%
    							or datecreaFiche ='".$tabsearch[0]."'
    							or loginF ='".$tabsearch[0]."'
    							or descriptifFiche like %'".$tabsearch[0]."'%
    							or resolutionFiche like %'".$tabsearch[0]."'%
    							or nomProjet ='".$search."'
    							or nbhresolution = '".$tabsearch[0]."'
    							or enseigne = '".$tabsearch[0]."'
    							or privee ='".$tabsearch[0]."'
    							or emailF = '".$tabsearch[0]."'";
    							// on boucle pour integrer tous les mots dans la requête
    							$sql = "select * from fiches where 1"; 
    							for ($i = 0; $i < $nb; $i++)
    								{ 
    								if(!empty($tabsearch[$i]))
    									{
    									$sql .= " AND ( numFiche='".$tabsearch[$i]."'
    									or nomFiche like %'".$tabsearch[$i]."'%
    									or datecreaFiche ='".$tabsearch[$i]."'
    									or loginF ='".$tabsearch[$i]."'
    									or descriptifFiche like %'".$tabsearch[$i]."'%
    									or resolutionFiche like %'".$tabsearch[$i]."'%
    									or nomProjet '".$search."'
    									or nbhresolution = '".$tabsearch[$i]."'
    									or enseigne = '".$tabsearch[$i]."'
    									or privee ='".$tabsearch[$i]."' 
    									or emailF = '".$tabsearch[$i]."' )"; 
    									}
    								}

  7. #7
    Rédacteur

    Avatar de Yogui
    Homme Profil pro
    Directeur technique
    Inscrit en
    Février 2004
    Messages
    13 721
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yonne (Bourgogne)

    Informations professionnelles :
    Activité : Directeur technique

    Informations forums :
    Inscription : Février 2004
    Messages : 13 721
    Points : 29 985
    Points
    29 985
    Par défaut
    Il faut aussi le vérifier avec $tabsearch[0]

  8. #8
    Membre régulier
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    295
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 295
    Points : 87
    Points
    87
    Par défaut
    Toujours pareil

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    if(isset($search) && trim($search) != '') 
    					{
    					$search = str_replace('+', ' ', trim($search));
    					$search = str_replace('\'', ' ', $search);
    					$search = str_replace(',', ' ', $search);
    					$search = str_replace(':', ' ', $search);
    					$tabsearch = explode(' ' , $search );
    					//on compte le nombre de mots
    					$nbsearch = count($tabsearch);
    					//on prépare la requête SQL.
    					if(!empty($tabsearch[0]))
    						{
    						$sql = "select * 
    								from fiches
    								where numFiche ='".$tabsearch[0]."' 
    								or nomFiche like %'".$tabsearch[0]."'%
    								or datecreaFiche ='".$tabsearch[0]."'
    								or loginF ='".$tabsearch[0]."'
    								or descriptifFiche like %'".$tabsearch[0]."'%
    								or resolutionFiche like %'".$tabsearch[0]."'%
    								or nomProjet ='".$search."'
    								or nbhresolution = '".$tabsearch[0]."'
    								or enseigne = '".$tabsearch[0]."'
    								or privee ='".$tabsearch[0]."'
    								or emailF = '".$tabsearch[0]."'";
    						}
    							// on boucle pour integrer tous les mots dans la requête
    							$sql = "select * from fiches where 1"; 
    							for ($i = 0; $i < $nb; $i++)
    								{ 
    								if(!empty($tabsearch[$i]))
    									{
    									$sql .= " AND ( numFiche='".$tabsearch[$i]."'
    									or nomFiche like %'".$tabsearch[$i]."'%
    									or datecreaFiche ='".$tabsearch[$i]."'
    									or loginF ='".$tabsearch[$i]."'
    									or descriptifFiche like %'".$tabsearch[$i]."'%
    									or resolutionFiche like %'".$tabsearch[$i]."'%
    									or nomProjet '".$search."'
    									or nbhresolution = '".$tabsearch[$i]."'
    									or enseigne = '".$tabsearch[$i]."'
    									or privee ='".$tabsearch[$i]."' 
    									or emailF = '".$tabsearch[$i]."' )"; 
    									}
    								}
    							// on execute la requête SQL.
    							$result = mysql_query($sql);
    							if ($result)
    								{
    								if (mysql_num_rows($result) == 0) 
    									{
    									// Si il n'y a pas de résultats
    									echo '<script language=javascript> alert ("Pas de résultat");</script>';
    									echo '<SCRIPT LANGUAGE="JavaScript">document.location.href="accueil.php" </SCRIPT>';
    									exit;
    									}else{
    										echo "<select name='searchselection'>";
    										while($datafichiers = mysql_fetch_assoc($result)) 
    											{ 
    											 echo '<option>'.$datafichiers['nomFiche'].'</option>'; 
    											 echo '<br>'; 
    											}
    									echo'</select>';
    										}
    								}
    					mysql_free_result($result);

  9. #9
    Membre régulier
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    295
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 295
    Points : 87
    Points
    87
    Par défaut
    Personne a une idée :

  10. #10
    Candidat au Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 3
    Points : 4
    Points
    4
    Par défaut
    Salut,
    je debarque via une recherche sur google alors je sais pas du tout si tu as regle ton soucis mais il me semble que sur ton dernier post, une petite erreur apparaisse , je suis pas sur mais verifie:
    tu as for($i =0;$i<$nb;$i++)......ca serait pas mieux avec
    for($i=0;$i<$nbsearch;$i++) ?

Discussions similaires

  1. Problème Affichage données dans requete synthèse
    Par Sebastien1977 dans le forum Access
    Réponses: 1
    Dernier message: 21/02/2012, 11h06
  2. Réponses: 3
    Dernier message: 17/02/2012, 12h28
  3. Réponses: 4
    Dernier message: 25/11/2011, 09h12
  4. Réponses: 5
    Dernier message: 23/01/2009, 11h12
  5. Problème affichage données dans listview
    Par Alixe80 dans le forum VB 6 et antérieur
    Réponses: 12
    Dernier message: 13/05/2008, 20h42

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo