2015-02-23 47 views
0

我正在創建一個我希望能夠批准,拒絕或刪除的用戶列表。我不想一個接一個地做,因爲這樣做太多了。 My list of users試圖批准,拒絕或刪除多個用戶

我有以下的批准,拒絕和刪除列代碼:

<tr> 
 
    <input type="hidden" name="user_id" value="'.$row['user_id'].'"> 
 
    <td style="text-align:center; padding:20px; background-color:#DFF0D8;"> 
 
     <input type="radio" name="approve[]" value="1"> 
 
    </td> 
 
    <td style="text-align:center; padding:20px; background-color:#FCF8E3;"> 
 
     <input type="radio" name="approve[]" value="0"> 
 
    </td> 
 
    <td style="text-align:center; padding:20px; background-color:#FCDEDE;"> 
 
     <input type="radio" name="approve[]" value="3"> 
 
    </td> 
 
</tr>

我希望能夠設置「批准」在表列「用戶「在MySQL中爲1,如果它被批准,並且在被拒絕的情況下保持爲0。如果選擇「刪除」,那麼我希望能夠刪除該行數據。我怎樣才能做到這一點的用戶名單?如果只是一個接一個,那很容易,但我不知道如何爲多個用戶做到這一點。

我想通過「批准」數組循環,但我無法更改數據庫中的值,因爲我不知道如何將其與user_id匹配。

任何幫助表示讚賞。

+0

我看到這裏有兩個可能的路徑:要麼有一個新的集爲每個用戶提供一個新的'name',或者認爲這是三個多選擇'select',批准,拒絕,刪除和委託,爲客戶端JS提供更好的前端顯示。 – 2015-02-23 18:41:18

+0

謝謝@UlrichSchwarz Schwwarz!我是這個東西的新手......如果我要求你更詳細地解釋一下這個問題,會不會太麻煩?我只是不確定如何去做這些選擇。 – Rookie 2015-02-25 17:24:25

回答

0

這裏是多刪除樣本:

的index.php

<?php include('dbcon.php'); ?> 
<form method="post" action="delete.php" > 
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example"> 
         <div class="alert alert-info"> 

          <strong>Check the radion Button and click the Delete button to Delete Data</strong> 
         </div><thead> 

          <tr> 
           <th></th> 
           <th>FirstName</th> 
           <th>LastName</th> 
           <th>MiddleName</th> 
           <th>Address</th> 
           <th>Email</th> 
          </tr> 
         </thead> 
         <tbody> 
         <?php 
         $query=mysql_query("select * from member")or die(mysql_error()); 
         while($row=mysql_fetch_array($query)){ 
         $id=$row['member_id']; 
         ?> 

            <tr> 
            <td> 
            <input name="selector[]" type="checkbox" value="<?php echo $id; ?>"> 
            </td> 
            <td><?php echo $row['firstname'] ?></td> 
            <td><?php echo $row['lastname'] ?></td> 
            <td><?php echo $row['middlename'] ?></td> 
            <td><?php echo $row['address'] ?></td> 
            <td><?php echo $row['email'] ?></td> 
          </tr> 

           <?php } ?> 
         </tbody> 
        </table> 
        <input type="submit" class="btn btn-danger" value="Delete" name="delete"> 

delete.php

<?php 
include('dbcon.php'); 
if (isset($_POST['delete'])){ 
$id=$_POST['selector']; 
$N = count($id); 
for($i=0; $i < $N; $i++){ 
$result = mysql_query("DELETE FROM member where member_id='$id[$i]'");} 
header("location: index.php"); } ?> 
+0

請不要暗示不推薦使用的庫(在本例中爲**'mysql_ *'**),因爲它們不安全/過時。 – Darren 2015-02-24 01:41:36

0

對於多個用戶,你只需要添加一個維度到所有表單變量名稱:

<tr> 
    <td style="text-align:center; padding:20px; background-color:#DFF0D8;"> 
     <input type="radio" name="approve[<?= $row['user_id'] ?>]" value="1"> 
    </td> 
    <td style="text-align:center; padding:20px; background-color:#FCF8E3;"> 
     <input type="radio" name="approve[<?= $row['user_id'] ?>]" value="0"> 
    </td> 
    <td style="text-align:center; padding:20px; background-color:#FCDEDE;"> 
     <input type="radio" name="approve[<?= $row['user_id'] ?>]" value="3"> 
    </td> 
</tr> 

當表單發送您會收到這樣的事情:

approve[123]=1&approve[456]=2&... 

這將打開$_POST['approve']到一個數組:

array(
    123 => 1 
    456 => 2 
); 
... 
+0

謝謝@Ja͢ck爲您解答。什麼是123?並將1選項(批准,否認,刪除)? – Rookie 2015-02-25 16:39:44