请求 - 带有内部联接的下拉列表



>我创建了一个链接到另一个表的下拉列表;但是,当我希望通过我的页面"编辑"从下拉列表中更改元素时;以前选择的元素不会保存(视觉上(。

它总是从我的下拉列表中重新获取第一个元素。这很烦人..

这里有一个小例子:在我的页面添加中.php我的 dropdow 列表中有 5 个元素:

- France
- Belgium
- Italia
- Spain
- Netherlands

我选择比利时,然后在我的页面"编辑"中我想通过"西班牙"进行更改,但在我的形式中,重拍的元素是"法国",它自动总是第一个,这只是一个例子。

我认为问题出在哪里?

<td>
    <select name="fk_club">
        <?php 
            while($data = mysqli_fetch_array($reso)) 
            {?>             
                <option value="<?php echo $data['pk_club']?>"><?php echo $data['nom_club']?></option>  
            <?php }?>
    </select>
</td>

给你我的完整代码

<?php
// including the database connection file
include_once("config_bd.php");
if(isset($_POST['update']))
{   
    $pk_membre = mysqli_real_escape_string($mysqli, $_POST['pk_membre']);
    $nom_membre = mysqli_real_escape_string($mysqli, $_POST['nom_membre']);
    $prenom_membre = mysqli_real_escape_string($mysqli, $_POST['prenom_membre']);
    $dateNaissance_membre  = mysqli_real_escape_string($mysqli, $_POST['dateNaissance_membre']);
    $fk_club = mysqli_real_escape_string($mysqli, $_POST['fk_club']);   

    if(empty($nom_membre) || empty($prenom_membre) || empty($dateNaissance_membre) || empty($fk_club)){ 
        if(empty($nom_membre)) {
            echo "<font color='red'>Le nom du membre est vide.</font><br/>";
        }
        if(empty($prenom_membre)) {
            echo "<font color='red'>Le prenom du membre est vide.</font><br/>";
        }   
        if(empty($dateNaissance_membre)) {
            echo "<font color='red'>La date de naissance du membre est vide.</font><br/>";
        }   
        if(empty($fk_club)) {
            echo "<font color='red'>Le fk club est vide.</font><br/>";
        }
    } else {    
        //updating the table
        $result = mysqli_query($mysqli, "UPDATE membres SET nom_membre='$nom_membre',prenom_membre='$prenom_membre', dateNaissance_membre='$dateNaissance_membre', fk_club='$fk_club' WHERE pk_membre=$pk_membre");
        //redirectig to the display page. In our case, it is index.php
        header("Location: vue_membre.php");
    }
}

//getting id from url
$pk_membre = $_GET['pk_membre'];
//selecting data associated with this particular id
$result = mysqli_query($mysqli, "SELECT * FROM membres WHERE pk_membre=$pk_membre");
while($res = mysqli_fetch_array($result))
{
    $nom_membre = $res['nom_membre'];
    $prenom_membre = $res['prenom_membre'];
    $dateNaissance_membre = $res['dateNaissance_membre'];
    $fk_club = $res['fk_club'];
}
// request for dropdown list
$reso = mysqli_query($mysqli,"SELECT pk_club,nom_club FROM clubs"); 
?>
<html>
<head>  
</head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Palais des Sports</title>
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="style/style1.css">
<body>  
    <div class="bandeau-bleu">
    <h2>Palais des Sports</h2>
    <a href="index.php?logout='1'"><i class="material-icons">&#xe897;</i></a>
    <a href="welcome.php"><i class="material-icons">&#xE88A;</i></a>
    </div>
    <div class="form_encodage">
        <a href="vue_membre.php"><h3>Cliquez ici pour afficher les enregistrements</h3></a>
    <br />
        <h4> Editer un enregistrement</h4><br />
    <form name="form1" method="post" action="edit_membre.php">
        <table border="0">
            <tr> 
                <td>Nom du Membre:</td>
                <td><input type="text" name="nom_membre" value="<?php echo $nom_membre;?>"></td>
            </tr>
            <tr> 
                <td>Prenom du Membre:</td>
                <td><input type="text" name="prenom_membre" value="<?php echo $prenom_membre;?>"></td>
            </tr>
            <tr> 
                <td>Date Naissance du Membre:</td>
                <td><input type="date" name="dateNaissance_membre" value="<?php echo $dateNaissance_membre;?>"></td>
            <tr> 
                <td>Nom du Club:</td>
                    <td><select name="fk_club">
                <?php 
                        while($data = mysqli_fetch_array($reso)) 
                            { 
                        ?>  
                        <option value="<?php echo $data['pk_club']?>"><?php echo $data['nom_club']?></option>  
                        <?php }
                        ?>
                    </select></td>
                </tr>
        </td></tr>
                <td><input type="submit" name="update" class="bouton_bleu" value="Update"></td>
                <td><input type="hidden" name="pk_membre" value=<?php echo $_GET['pk_membre'];?>></td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>

<select size="1" name="fk_club" id="fk_club" >
<option value="" selected="selected">Select</option>
<?php
while($data = mysqli_fetch_array($reso)){
?>
<option value="<?=$data['fk_club]';?>" ><?=$data['nom_club'];?> </option>
                  <?php  }
                    ?>
                </select>

我认为这是工作。谢谢。

最新更新