douwuying4709 2014-11-14 22:41
浏览 95
已采纳

从帖子表单中获取数据

I don't understand why I can't use my last form in this code. I generated a form using a SELECT list to select the member that I want to update and it works, but I don't know why I can't use datas from this form. Actually, I can't even echo something (see the echo "TEST"; at the end, nothing happens when I submit the form).

<?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";



        }
    }
?>
  • 写回答

3条回答 默认 最新

  • duanpanbo9476 2014-11-14 22:53
    关注

    Your second if check is inside the other one, so it will only run when both $_POST['modifier'] and $_POST['valider_modif'] are set. But you second form does not send modifier anywhere.

    You could add a hidden field to your second form:

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

    Or if you don't want to show the second form again, move the second if outside the other.

    Also, you should not use $_POST values in SQL queries directly to be safe from SQL injection. A function like mysqli_real_escape_string should be used to escape the value first.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题