2015-09-05 80 views
1

我有一個PHP Crud系統。創建新記錄時的選項之一是具有兩個選項的選擇框。 Netman和管理員。當我更新記錄時,我看到選項,但它重複,並有另一個選項, 像這樣Duplicate selection。我不知道如何顯示數據庫和其他選項的記錄。這可能嗎?PHP Crud更新選擇顯示重複

<?php 
    include "config.php"; 
    include "header.php"; 
    if(isset($_GET['u'])): 
    if(isset($_POST['bts'])): 
    $stmt = $mysqli->prepare("UPDATE passwordtable SET school=?,usertype=?, password=?, notes=? WHERE id_personal=?"); 
    $stmt->bind_param('sssss', $nm, $gd, $tl, $ar, $id); 

    $nm = $_POST['nm']; 
    $gd = $_POST['gd']; 
    $tl = $_POST['tl']; 
    $ar = $_POST['ar']; 
    $id = $_POST['id']; 

    if($stmt->execute()): 
    echo "<script>location.href='index.php'</script>"; 
    else: 
    echo "<script>alert('".$stmt->error."')</script>"; 
    endif; 
    endif; 
    $res = $mysqli->query("SELECT * FROM passwordtable WHERE id_personal=".$_GET['u']); 
    $row = $res->fetch_assoc(); 
    ?> 

和選擇框代碼我們

<label for="gd">User Type</label> 
    <select class="form-control" id="gd" name="gd"> 
    <option><?php echo $row['usertype'] ?></option> 
    <option>Administrator</option> 
    <option>NetMan</option> 
    </select> 

回答

1

如果仍然沒有找到答案,請有看看下面的代碼

<?php 
    $usertypes = array('Administrator' => 'Administrator', 'NetMan' => 'NetMan'); 
    $selcted_usertype = !empty($row['usertype']) ? $row['usertype'] : ""; 
    ?> 
    <label for="gd">User Type</label> 
    <select class="form-control" id="gd" name="gd"> 
     <?php 
     foreach ($usertypes as $user_type_val => $usertype_txt) { 
      $selected = ($user_type_val == $selcted_usertype) ? "selected='selected'" : ""; 
      ?> 
      <option <?php echo $selected; ?> value="<?php echo $user_type_val; ?>"><?php echo $usertype_txt; ?></option> 
     <?php } 
     ?> 

    </select> 

您將需要價值爲屬性選項。

+0

Yogesh,非常感謝,這正是我想要的,我再次感謝,再次感謝 – Jay