2016-05-16 18 views
-2

我想給管理員激活用戶帳號的能力裏面PHP else語句,所以我不得不地位活性和非活性 當我運行我的代碼,我得到4個狀態 活動 無效 活動 無效 你會發現這裏的截圖來理解這個問題我現在面臨如果和的回聲

http://i.imgur.com/2c0VcN7.png

if(isset($_GET['id_user'])){ 

     include("conexion.php"); 

     $rep=$db->query("select * from user WHERE id_user=" .$_GET['id_user'] ); 

     while($l=$rep->fetch()){ 
echo " 

<form class= mainSettingsForm add method='POST'> 


    <label>Numero utlisateur :</label> 
    <input disabled='disabled' type='text' name='ref' value=' ".$l[0]." ' ></input> 
    <input name='id_user2' type='hidden' value='".$l[0]."' ></input> 

    <label>Nom utlisateur : </label>  
    <input type='text' name='username2' value='".$l[1]."' > 

    <label> nom : </label> 
    <input type='text' name='nom2' value='".$l[3]."' > 

    <label> prenom :  </label> 
    <input type='text' name='prenom2' value='".$l[4]."' > 

    <label> Statut : </label>  
    <select name=statut > 


     if ($l[6] ==active) { 
     echo ' 
     <option value=active >active</option> 
     <option value=inactive >inactive</option> }' 

     else { 
     echo ' 
     <option value=inactive >inactive</option>  
     <option value=active >active</option> }' 

     </select> 



    <input class=btn-primary type='submit' name='enregistrer' value='enregistrer' > 

    </form>"; // fin echo 
    } 
    } 
?> 
+2

close echo,run if,open echo。學習語言語法的基礎知識將有助於 – 2016-05-16 00:32:17

+1

[如果塊內部的echo語句?]可能重複(http://stackoverflow.com/questions/3507042/if-block-inside-echo-statement)或[如何嵌入如果聲明裏面回聲](http://stackoverflow.com/questions/21175753/how-to-embed-if-statement-inside-echo) – Sean

回答

0

您的代碼可能沒有正確形成。試試這個:

<?php 

     if(isset($_GET['id_user'])){    
      include("conexion.php");     
      $rep = $db->query("select * from user WHERE id_user=" . $_GET['id_user'] ); 

      // YOU ARE FETCHING A SINGLE USER... NO NEED FOR A LOOP... 
      // JUST GET THE ARRAY OBJECT AND USE IT DIRECTLY... 
      $l = $rep->fetch(); 
    ?> 

    <form class= mainSettingsForm add method='POST'>    
     <label>Numero utlisateur :</label> 
     <input disabled='disabled' type='text' name='ref' value='<?php echo $l[0]; ?>'/> 
     <input name='id_user2' type='hidden' value='<?php echo $l[0]; ?>'/> 

     <label>Nom utlisateur :</label> 
     <input type='text' name='username2' value='<?php echo $l[1]; ?>' /> 

     <label>Nom :</label> 
     <input type='text' name='nom2' value='<?php echo $l[3]; ?>' /> 

     <label>Prenom :</label> 
     <input type='text' name='prenom2' value='<?php echo $l[4]; ?>' /> 

     <label>Statut :</label>   
     <select name=statut >    
      <option value='active' <?php if($l[6] == "active"){ echo "selected";} ?> >active</option> 
      <option value='inactive' <?php if($l[6] == "inactive"){ echo "selected";} ?>inactive</option> 
     </select>   

     <input class=btn-primary type='submit' name='enregistrer' value='enregistrer' >  
    </form> 

    <?php } ?> 
+0

謝謝你的回答,但我仍然作爲第一選擇積極即使用戶的狀態是不活動的!問候。 –

+0

@AbdelhakOhammou $ l [6]的內容是什麼?你可以做一個var_dump($ l [6])嗎?這會告訴你$ l [6]是否包含字符串「active」或「inactive」。讓我知道你發現了什麼。乾杯... – Poiz

+0

我修正了這個問題,謝謝你的時間,我真的很感激。 –