2014-11-14 144 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