2012-01-23 50 views
-1

我的代碼在php中遇到問題。 我想用一個用戶按下刪除按鈕的時候,從一個表格中刪除多行選擇框。在數據庫中刪除沒有ID的選擇框的行

但我沒有在我的數據庫中的id字段。所以我很難做到這一點 你能幫我嗎?

$user_query= "SELECT * FROM user"; 
    $user_result = mysql_query($user_query); 
    $count=mysql_num_rows($user_result); 

    echo "<form name='form1' method='post' action=''>"; 
    echo "<table>"; 
    echo "<tr>"; 
    echo "<th scope='col'>#</th>"; 
    echo "<th scope='col'>username</th>"; 
    echo "<th scope='col'>Email</th>"; 
    echo "<th scope='col'>tel</th>"; 
    echo "<th scope='col'>name</th>"; 
    echo "<th scope='col'>surname</th>"; 
    echo "<th scope='col'>type</th>"; 
    echo "</tr>"; 

    while($row = mysql_fetch_array($user_result)) 
    { 
     echo "<tr>"; 
     echo '<td><input name="checkbox[]" type="checkbox" username="checkbox[]" value="'.$row['username'].'"></td>'; 
     echo '<td><b>'.$row['username'].'</b></td>'; 
     echo '<td><a href="mailto:'.$row['e_mail'].'" title="send Email">'.$row['e_mail'].'</a></td>'; 
     echo '<td>'.$row['phone_number'].'</td>'; 
     echo '<td>'.$row['name'].'</td>'; 
     echo '<td>'.$row['surname'].'</td>'; 
     echo '<td><img src="images/admin.png" width="40" height="45" title="admin"/></td>'; 
     echo '</tr>'; 
    } 

    echo '<tr><td colspan="7"><input name="delete" type="submit" id="delete" value="Deleting"></td></tr>'; 
    echo "</table>"; 
    echo "</form>"; 



    if (isset($_POST['delete'])) 
    { 
     // how can I take checkboxes value and delete values ?? 
     // $sql = "DELETE FROM user WHERE username=...; 
     $result = mysql_query($sql); 

     // if successful redirect 
     if($result) 
     { 
      header("location: ShowUsers.php"); 
     } 
    } 
+2

如果你沒有一個ID字段,什麼是你的主鍵?如果我是你,我會做一個ID字段。 –

+0

當我們唯一知道的就是它*沒有特定的列時,不可能告訴你如何從表中刪除特定的行。 – Quentin

+0

我想'用戶名'將是一個獨特的領域,是不是你的ID? – Quentin

回答

1

至於評論,如果您有您的用戶名是唯一的標識符,然後用相應的改變取代:=

 $user_query= "SELECT * FROM user"; 
      $user_result = mysql_query($user_query); 
      $count=mysql_num_rows($user_result); 

      echo "<form name='form1' method='post' action=''>"; 
      echo "<table>"; 
      echo "<tr>"; 
      echo "<th scope='col'>#</th>"; 
      echo "<th scope='col'>username</th>"; 
      echo "<th scope='col'>Email</th>"; 
      echo "<th scope='col'>tel</th>"; 
      echo "<th scope='col'>name</th>"; 
      echo "<th scope='col'>surname</th>"; 
      echo "<th scope='col'>type</th>"; 
      echo "</tr>"; 

      while($row = mysql_fetch_array($user_result)) 
      { 
       echo "<tr>"; 
       echo '<td><input name="checkbox[]" type="checkbox" username="checkbox[]" value="'.$row['username'].'"></td>'; 
       echo '<td><b>'.$row['username'].'</b></td>'; 
       echo '<td><a href="mailto:'.$row['e_mail'].'" title="send Email">'.$row['e_mail'].'</a></td>'; 
       echo '<td>'.$row['phone_number'].'</td>'; 
       echo '<td>'.$row['name'].'</td>'; 
       echo '<td>'.$row['surname'].'</td>'; 
       echo '<td><img src="images/admin.png" width="40" height="45" title="admin"/></td>'; 
       echo '</tr>'; 
      } 

      echo '<tr><td colspan="7"><input name="delete" type="submit" id="delete" value="Deleting"></td></tr>'; 
      echo "</table>"; 
      echo "</form>"; 



      if (isset($_POST['delete'])) 
      { 
       if(!empty($_POST['checkbox'])){ 
        $WhereCondition = $usernames = ''; 
       foreach($_POST['checkbox'] as $user) 
        $usernames.= "'".$user."',"; 
       $usernames = trim($usernames,','); 
       $WhereCondition = " username in($usernames) "; 
       }else{ 
        $WhereCondition = " 0 "; 
       } 

       $sql = "DELETE FROM user where $WhereCondition "; 
       $result = mysql_query($sql); 

       // if successful redirect 
       if($result) 
       { 
        header("location: ShowUsers.php"); 
       } 
      } 
+0

它工作正常,但如果我按刪除沒有ckecked領域我採取 警告 警告:無效的參數爲foreach()提供的 我試過error_reporting(E_ALL^E_NOTICE);但它並沒有消失 – user494766

+0

回答已編輯。與if條件。 – Bajrang

+0

非常感謝您的時間 – user494766

相關問題