2016-05-25 91 views
0

下面是混合的HTML和PHP。我沒有將它們分開,因爲標籤被php填充。下拉式數據不會從數據庫中刪除

<form method="post"> 
    <select name="Select User" value="Select User"> 
    <?php 
    $sth = $conn->prepare('Select username From users'); 
    $sth->execute(); 
    $data = $sth->fetchAll(); 
    foreach ($data as $row){ 
     if($row['username']!="") 
    echo " <option id=\"Select_user\" name=\"Select_user\" value=\"Select_user\">".$row['username']."</option>"; 
    } 
    ?> 

    </select><br> 
    <input type="submit" name="submit" value="Delete"</input> 

    <?php 
      if(! empty($_POST['Select_user'])) 
      { 
      $del_user=$_POST['Select_user']; 
      $sql = "DELETE FROM users WHERE username=:del"; 
      $sth = $conn->prepare($sql); 
      $sth->execute(array(':del'=>$del_user)); 
      } 

    ?> 

下拉正確地獲取數據,但刪除沒有發生。爲什麼?

+0

回聲你的查詢並查看。 –

+0

我在那看到什麼? –

回答

-1

你需要設置選項值正確的數據,這將在PHP公佈,選定的選項值將被張貼所以,只要你想的用戶名在$ _ POST的選項值,設置用戶名:

<form method="post"> 
    <select name="user_selection"> 

    <?php 
     $sth = $conn->prepare('Select username From users'); 
     $sth->execute(); 


    $data = $sth->fetchAll(); 
    foreach ($data as $row){ 
     if($row['username']!="") 
     echo '<option id="Select_user" name="Select_user" value="'.$row["username"].'">'.$row["username"].'</option>'; 
    } 
    ?> 

    </select><br> 
    <input type="submit" name="submit" value="Delete"</input> 

    <?php 
      if(! empty($_POST['user_selection'])) // access element by its name 
      { 
      $del_user=$_POST['user_selectio']; 
      $sql = "DELETE FROM users WHERE username=:del"; 
      $sth = $conn->prepare($sql); 
      $sth->execute(array(':del'=>$del_user)); 
      } 

    ?> 
+0

下拉菜單顯示正確的值。我無法刪除刪除按鈕中的值。 –

+0

不是元素'用戶名'的名稱而不是'user_selection'? –

+0

我剛在此處將其更改爲您的理解 –