请求下拉列表



我有一个包含 5 个元素的下拉列表(排球、手球、橄榄球、篮球、Autres(例如,我希望在我的表格中选择元素"橄榄球",然后确认。

我的

问题是当我希望在我的表格(编辑(中更改我的旧选择时;元素"橄榄球"不是以前保存的元素。默认情况下,我总是有"排球"元素。

[![<td>Type de Club:</td><td>
                <select name="type_club" style="width:144px">
                    <option>Volley-Ball</option>
                    <option>Hand-Ball</option>
                    <option>Rugby</option>
                    <option>Basket-Ball</option>
                    <option>Autres</option>
                </select>][1]][1]

  [1]: https://i.stack.imgur.com/xQ83B.png

这是我的代码。

<?php
// including the database connection file
include_once("config_bd.php");
if(isset($_POST['update']))
{   
    $pk_club = mysqli_real_escape_string($mysqli, $_POST['pk_club']);
    $nom_club = mysqli_real_escape_string($mysqli, $_POST['nom_club']);
    $type_club = mysqli_real_escape_string($mysqli, $_POST['type_club']);   
    // checking empty fields
    if(empty($nom_club) || empty($type_club)) { 
        if(empty($nom_club)) {
            echo "<font color='red'>Le nom du club est vide.</font><br/>";
        }
        if(empty($type_club)) {
            echo "<font color='red'>Le type du club est vide.</font><br/>";
        }       
    } else {    
        //updating the table
        $result = mysqli_query($mysqli, "UPDATE clubs SET nom_club='$nom_club',type_club='$type_club' WHERE pk_club=$pk_club");
        //redirectig to the display page. In our case, it is index.php
        header("Location: vue_club.php");
    }
}

//getting id from url
$pk_club = $_GET['pk_club'];
//selecting data associated with this particular id
$result = mysqli_query($mysqli, "SELECT * FROM clubs WHERE pk_club=$pk_club");
while($res = mysqli_fetch_array($result))
{
    $nom_club = $res['nom_club'];
    $type_club = $res['type_club'];
}
?>
<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_club.php"><h3>Cliquez ici pour afficher les enregistrements</h3></a>
    <br />
        <h4> Editer un enregistrement</h4><br />
    <form name="form1" method="post" action="edit_club.php">
        <table border="0">
            <tr> 
                <td>Nom du Club:</td>
                <td><input type="text" name="nom_club" value="<?php echo $nom_club;?>"></td>
            </tr>
            <tr> 
                <td>Type de Club:</td><td>
            <select name="type_club" style="width:144px">
                <option>Volley-Ball</option>
                <option>Hand-Ball</option>
                <option>Rugby</option>
                <option>Basket-Ball</option>
                <option>Autres</option>
            </select>
        </td></tr>
                <td><input type="submit" name="update" class="bouton_bleu" value="Update"></td>
                <td><input type="hidden" name="pk_club" value=<?php echo $_GET['pk_club'];?>></td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>

您可以将selected属性与 options 一起使用。

下面是您放置薄td下拉列表的更新代码。

试试这个:

<td>Type de Club:</td><td>
            <select name="type_club" style="width:144px">
                <option <?php if(isset($type_club) and $type_club=='Volley-Ball'){ echo 'selected'; }?>>Volley-Ball</option>
                <option <?php if(isset($type_club) and $type_club=='Hand-Ball'){ echo 'selected'; }?>>Hand-Ball</option>
                <option <?php if(isset($type_club) and $type_club=='Rugby'){ echo 'selected'; }?>>Rugby</option>
                <option <?php if(isset($type_club) and $type_club=='Basket-Ball'){ echo 'selected'; }?>>Basket-Ball</option>
                <option <?php if(isset($type_club) and $type_club=='Autres'){ echo 'selected'; }?>>Autres</option>
            </select>
        </td>

完成更新的代码:

<?php
// including the database connection file
include_once("config_bd.php");
if(isset($_POST['update']))
{   
    $pk_club = mysqli_real_escape_string($mysqli, $_POST['pk_club']);
    $nom_club = mysqli_real_escape_string($mysqli, $_POST['nom_club']);
    $type_club = mysqli_real_escape_string($mysqli, $_POST['type_club']);   
    // checking empty fields
    if(empty($nom_club) || empty($type_club)) { 
        if(empty($nom_club)) {
            echo "<font color='red'>Le nom du club est vide.</font><br/>";
        }
        if(empty($type_club)) {
            echo "<font color='red'>Le type du club est vide.</font><br/>";
        }       
    } else {    
        //updating the table
        $result = mysqli_query($mysqli, "UPDATE clubs SET nom_club='$nom_club',type_club='$type_club' WHERE pk_club=$pk_club");
        //redirectig to the display page. In our case, it is index.php
        header("Location: vue_club.php");
    }
}

//getting id from url
$pk_club = $_GET['pk_club'];
//selecting data associated with this particular id
$result = mysqli_query($mysqli, "SELECT * FROM clubs WHERE pk_club=$pk_club");
while($res = mysqli_fetch_array($result))
{
    $nom_club = $res['nom_club'];
    $type_club = $res['type_club'];
}
?>
<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_club.php"><h3>Cliquez ici pour afficher les enregistrements</h3></a>
    <br />
        <h4> Editer un enregistrement</h4><br />
    <form name="form1" method="post" action="edit_club.php">
        <table border="0">
            <tr> 
                <td>Nom du Club:</td>
                <td><input type="text" name="nom_club" value="<?php echo $nom_club;?>"></td>
            </tr>
            <tr> 
                <td>Type de Club:</td><td>
            <select name="type_club" style="width:144px">
                <option <?php if(isset($type_club) and $type_club=='Volley-Ball'){ echo 'selected'; } ?>>Volley-Ball</option>
                <option <?php if(isset($type_club) and $type_club=='Hand-Ball'){ echo 'selected'; } ?>>Hand-Ball</option>
                <option <?php if(isset($type_club) and $type_club=='Rugby'){ echo 'selected'; } ?>>Rugby</option>
                <option <?php if(isset($type_club) and $type_club=='Basket-Ball'){ echo 'selected'; } ?>>Basket-Ball</option>
                <option <?php if(isset($type_club) and $type_club=='Autres'){ echo 'selected'; }?>>Autres</option>
            </select>
        </td></tr>
                <td><input type="submit" name="update" class="bouton_bleu" value="Update"></td>
                <td><input type="hidden" name="pk_club" value=<?php echo $_GET['pk_club'];?>></td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>

您需要在加载选择表单之前查询数据库以获取以前的保存,然后构建列表

$sqla= "SELECT * FROM `save_table` WHERE `id` = $id ";
$result = $conn->query($sqla);
if ($result->num_rows > 0) {
$save = $row['save'];
$select= ('<td>Type de Club:</td><td>
        <select name="type_club" style="width:144px">
            <option>');
$select.='$save';
$select.='</option> <option>Volley-Ball</option>
            <option>Hand-Ball</option>
            <option>Rugby</option>
            <option>Basket-Ball</option>
            <option>Autres</option>
        </select>';
echo $select;

您还可以使用 if 语句在生成第二个 $save 变量之前将其从列表中删除,以便列表中没有重复选项。

最新更新