2014-11-14 145 views
1

我不明白爲什麼我不能在此代碼中使用我的最後一個表格。我使用SELECT列表生成了一個表單來選擇我想要更新的成員,並且它可以工作,但是我不知道爲什麼我不能使用這個表單中的數據。其實,我甚至不能迴應一些東西(最後看echo "TEST";,提交表單時沒有任何反應)。從帖子表單中獲取數據

<?php $mysqli = new Mysqli("localhost", "root", "", "repertoire"); ?> 

<form method="post" action=""> 
    <label>Modifier</label> 
    <select name='id_modif'> 
     <?php 

      $resultat = $mysqli->query("SELECT * FROM annuaire"); 
      while($select = $resultat->fetch_assoc()){ 
       echo "<option value=". $select['id_annuaire'] . ">" . $select['prenom'] . " " . $select['nom'] . "</option>"; 
      } 

     ?> 
    </select> 
    <input type ="submit" name="modifier"> 
</form> 
<br> 

<?php 
    if (isset($_POST['modifier'])){ 

     //print_r($_POST); 
     $resultat = $mysqli->query("SELECT * FROM annuaire WHERE id_annuaire = '$_POST[id_modif]'"); 
     while ($modif = $resultat->fetch_assoc()) { 

     echo '<form method="post" action=""> 
     <label for="nom">Nom *</label><br> 
     <input type="text" name="nom" value="' . $modif['nom'] . '"> <br>'; 

     echo '<label for="prenom">prenom *</label><br> 
     <input type="text" name="prenom" value="' . $modif['prenom'] . '"> <br>'; 

     echo '<label for="telephone">telephone *</label><br> 
     <input type="text" name="telephone" value="' . $modif['telephone'] . '"> <br>'; 

     echo '<label for="profession">profession *</label><br> 
     <input type="text" name="profession" value="' . $modif['profession'] . '"> <br>'; 

     echo '<label for="ville">ville *</label><br> 
     <input type="text" name="ville" value="' . $modif['ville'] . '"> <br>'; 

     echo '<label for="codepostal">codepostal *</label><br> 
     <input type="text" name="codepostal" value="' . $modif['codepostal'] . '"> <br>'; 

     echo '<label for="adresse">adresse *</label><br> 
     <textarea name="adresse">' . $modif['adresse'] . '</textarea> <br>'; 

     echo '<label for="date_de_naissance">Date de naissance</label><br> 
     <input type="date" name="date_de_naissance" value="' . $modif['date_de_naissance'] . '"><br>'; 

     echo '<label for="sexe">sexe</label><br> 

     <input type="radio" name="sexe" class="sexe" value="m" checked>Homme 
     <input type="radio" name="sexe" classe="sexe" value="f">Femme<br>'; 

     echo '<label for="description">description *</label><br> 
     <textarea name="description">' . $modif['description'] . '</textarea> <br>'; 

     echo '<input type="submit" name="valider_modif" value="Modifier"> <br>'; 

     } 

     if (isset($_POST['valider_modif'])){ 


      echo "TEST"; 



     } 
    } 
?> 
+1

您永遠不會關閉第二個'

'標籤。 – TiiJ7 2014-11-14 22:45:26

+0

非常感謝,沒錯,但我仍然有同樣的問題:( – 2014-11-14 22:48:35

+0

'var_dump($ _ POST)'的結果是什麼? – 2014-11-14 22:53:16

回答

0

你的第二個if檢查關閉第二是另一個裏面,所以當兩個$_POST['modifier']$_POST['valider_modif']設置它將只運行。但你的第二種形式不會在任何地方發送modifier

你可以隱藏字段添加到您的第二種形式:

<input type="hidden" name="modifier" value="1" /> 

或者,如果你不想再次顯示第二種形式,將其他之外的第二if

此外,您不應該在SQL查詢中直接使用$_POST值以確保SQL注入安全。應該使用像mysqli_real_escape_string這樣的函數來首先轉義值。

+0

非常感謝!:) – 2014-11-14 22:58:55

0

你有2種形式,你是不是有</form>

+0

謝謝,我沒有看到它,但它仍然不起作用 – 2014-11-14 22:54:21

0

花了很長時間來瀏覽你的代碼。好的。第一件事。儘量不要回聲太多的HTML標記。它只是讓你編碼世界上最笨重的人。從我收集的內容中,當您點擊第一個按鈕時,Modifier按鈕會出現。如果你想看到TEST消息,你需要做的是將它從if語句中取出,因爲按鈕就像異或門。設置另一個取消另一個

+0

謝謝你的建議。我一週前開始使用PHP,所以我還不是很舒服。我會嘗試在未來使用較少的回聲和更多的PHP標籤;) – 2014-11-14 23:11:52