使用选择框删除行,而不在数据库中使用id

时间:2012-01-23 12:09:44

标签: php

我的php中的代码有问题。 我希望在用户时使用选择框从表中删除多行 按删除按钮。

但我的数据库中没有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");
        } 
  }

1 个答案:

答案 0 :(得分: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");
                } 
          }