PHP关联表下拉框编辑页无法回显选中值问题求助
解决编辑页面下拉列表无法保留选中值的问题
我来帮你搞定这个问题!你遇到的情况是,在编辑会员信息时,关联俱乐部的下拉列表总是默认显示第一个选项,而不是之前选中的那个。这是因为你的下拉选项没有标记出当前会员已关联的俱乐部选项。
问题根源
你已经从数据库中获取了当前会员对应的俱乐部ID($fk_club),但在循环生成<option>标签时,没有添加selected属性来指定哪个选项应该被默认选中,所以浏览器只能默认展示第一个选项。
修复方案
只需要在生成下拉选项的循环里,增加一个判断:当当前循环到的俱乐部ID($data['pk_club'])和会员关联的俱乐部ID($fk_club)相等时,给该<option>添加selected='selected'属性。
修改后的下拉列表代码片段:
<td><select name="fk_club"> <?php while($data = mysqli_fetch_array($reso)) { // 判断当前俱乐部是否是会员关联的俱乐部,是的话添加selected属性 $selected = ($data['pk_club'] == $fk_club) ? 'selected="selected"' : ''; ?> <option value="<?php echo $data['pk_club']?>" <?php echo $selected; ?>><?php echo $data['nom_club']?></option> <?php } ?> </select></td>
完整的修改后页面代码
这里是整合了修复后的完整edit_membre.php代码,你可以直接替换使用:
<?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"></i></a> <a href="welcome.php"><i class="material-icons"></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)) { $selected = ($data['pk_club'] == $fk_club) ? 'selected="selected"' : ''; ?> <option value="<?php echo $data['pk_club']?>" <?php echo $selected; ?>><?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>
额外提示
- 确保
$fk_club和pk_club的数据类型一致(比如都是整数类型),否则可能会出现判断不匹配的情况。 - 建议后续逐步迁移到PDO或MySQLi的预处理语句,避免SQL注入风险,让代码更安全。
内容的提问来源于stack exchange,提问作者tamzoro
相关产品推荐
相关产品推荐

