繁体   English   中英

通过 $ID 从数据库中删除用户不起作用

[英]Delete user from Database by $ID not working

我无法使此代码正常工作。 这是我的 manageclients.php 如何将单击的用户 ID 传递给 URL,以便我可以执行删除查询? 当我执行此操作时,没有任何反应,如果我将 if 语句移到表查询下方,它会起作用,但它会删除我所有的表行。

 <?php
      if (isset($_POST['AddUsr']))
                {
        $new_username = $_POST['add_username'];
        $new_password = $_POST['add_password'];
        $new_vorname = $_POST['add_vorname'];
        $new_nachname = $_POST['add_nachname'];
        $new_email = $_POST['add_email'];
        $new_handy = $_POST['add_handy'];
        $new_admin = $_POST['add_admin'];
                    // if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['vorname']) || empty($_POST['nachname']) || empty($_POST['Email']) || empty($_POST['handy']) || empty($_POST['admin']))
                    // {
                    //  $show -> showError("Bitte alle Felder ausfüllen"/*isset($_POST['Enabled']) ? "1" : "0"*/);
                    // }
                        // if
                        {
                            $SQLCheckUser = $odb -> prepare("SELECT * FROM `Account` WHERE `email` = :email");
                            $SQLCheckUser -> execute(array(':email' => $_POST['add_email']));
                            $SQLCheckUser = $SQLCheckUser -> fetchColumn(0);
                            if ($SQLCheckUser > 0)
                            {
                                $show -> showError('Benutzer bereits in der Datenbank');
                            }
                            else
                            {
                                $SQLAddClient = $odb -> prepare("INSERT INTO `Account` VALUES(NULL, :username, :password, :vorname, :nachname, :email, :handy, :admin)");
                                $SQLAddClient -> execute(array(':username' => $_POST['add_username'], ':password' => $_POST['add_password'], ':vorname' => $_POST['add_vorname'], ':nachname' => $_POST['add_nachname'], ':email' => $_POST['add_email'], ':handy' => $_POST['add_handy'], ':admin' => $_POST['add_admin']));
              $show -> showSuccess('Benutzer wurde hinzugefügt');
              echo "<meta http-equiv='refresh(10)' content='0'>";
                            }
                        }
      }
      if(isset($_GET['DeleteID']))
      {
          if($_GET['DeleteID'])
          {
              // $id = strip_tags($_GET['RemoveID']);
              // mysqli_query($con, "DELETE FROM `Account` WHERE `id` = '$id'") or die(mysqli_error($con));

              // $removeid = "DELETE FROM Account WHERE Id = :id LIMIT 1";
              // $stmt = $dbh ->prepare($command);
              // $stmt->bindParam(':id', $_GET['Id'], PDO::PARAM_INT);
              // $stmt->execute();

              $stmt = $odb->prepare("DELETE FROM Account WHERE id='$ID' ");
              // $stmt->bindValue(':id', $ID, PDO::PARAM_STR);
              $stmt->execute();
              // removeUser();
            }
            $show -> showSuccess('Benutzer wurde entfernt');
            echo "<meta http-equiv='refresh()' content='0'>";
      }
                ?>
            <!-- Modal -->
            <div class="modal fade" id="AddUser" aria-hidden="false" aria-labelledby="AddUserModal"
              role="dialog" tabindex="-1">
                <div class="modal-dialog modal-simple modal-center">
                  <form class="modal-content" role="form" action="" method="post">
                    <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                      </button>
                      <h4 class="modal-title" id="AddUserModal">Benutzer eingeben</h4>
                    </div>
                    <div class="modal-body">
                      <div class="row">
                        <div class="col-xl-6 form-group">
                          <input type="text" class="form-control" id="username" name="add_username" placeholder="Username" />
                        </div>
                        <div class="col-xl-6 form-group">
                          <input type="password" class="form-control" id="password" name="add_password" placeholder="Passwort" required="">
                        </div>
                        <div class="col-xl-4 form-group">
                          <input type="text" class="form-control" id="vorname" name="add_vorname" placeholder="Vorname" required="">
                        </div>
                        <div class="col-xl-8 form-group">
                        <input type="text" class="form-control" id="nachname" name="add_nachname" placeholder="Nachname" required="">
                      </div>
                      <div class="col-xl-8 form-group">
                          <input type="text" class="form-control" id="email" name="add_email" placeholder="email" required="">
                        </div>
                        <div class="col-xl-4 form-group">
                        <input type="text" class="form-control" id="handy" name="add_handy" placeholder="TelefonNr." required="">
                      </div>
                      <div class="col-xl-12 form-group">
                          <input type="text" class="form-control" id="admin" name="add_admin" required="">
                        </div>

                        <div class="col-md-12 float-right">
                        <button type="submit" name="AddUsr" class="btn btn-primary btn-outline" >Benutzer Hinzufügen</button>
                        </div>
                      </div>
                    </div>
                  </form>
                </div>
              </div>
              <!-- End Modal -->
          </div>
        </div>
      </div>
      <table class="table table-bordered table-hover table-striped" cellspacing="0" id="exampleAddRow">

        <thead>
          <tr>
            <th >ID</th>
            <th>Username</th>
            <th>Vorname</th>
            <th>Nachname</th>
            <th>Email</th>
            <th>TelefonNr</th>
            <th>Aktion</th>
          </tr>
        </thead>
        <tbody>
          <!-- <tr class="gradeA"> -->

    <?php $SQLGetLogs = $odb -> query("SELECT * FROM `Account` ORDER BY `ID`");
                         while($ArrayInfo = $SQLGetLogs -> fetch(PDO::FETCH_ASSOC))
                         {                      
                            // Assign variables to column values in database                     
                            $ID = $ArrayInfo['ID'];
                            $username  = $ArrayInfo['username'];
            $vorname = $ArrayInfo['vorname'];
            $nachname = $ArrayInfo['nachname'];
            $email = $ArrayInfo['email'];
            $handy = $ArrayInfo['handy'];

                            // Token Used / Unused
                            // if($used == 0)
                            // $used = "<center><span class='label label-warning'>No</span></center>";
                            // else (
                            // $used = "<center><span class='label label-info'>Yes</span></center>");


                            echo '<tr class="gradeA">
                            <td>'.$ID.'</td>
                            <td>'.$username.'</td>
            <td>'.$vorname.'</td>
            <td>'.$nachname.'</td>
            <td>'.$email.'</td>
            <td>'.$handy.'</td>
            <td class="actions">
              <a href="?edituser=' .$ID. '" class="btn btn-sm btn-icon btn-pure btn-default on-default edit-row"
              data-original-title="Bearbeiten" ref="" data-toggle="modal" data-target="#EditUser">
              <i class="icon wb-edit" aria-hidden="true"></i></a>

              <a href="?DeleteID=' .$ID. '" class="btn btn-sm btn-icon btn-pure btn-danger on-default remove-row"
              data-toggle="tooltip" data-original-title="Löschen">
              <i class="icon wb-trash" aria-hidden="true"></i></a>
              <div class="modal fade" id="EditUser" aria-hidden="false" aria-labelledby="EditUserModal"
                 role="dialog" tabindex="-1">
                   <div class="modal-dialog modal-simple modal-center">
                     <form class="modal-content">
                       <div class="modal-header">
                         <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                           <span aria-hidden="true">×</span>
                         </button>
                         <h4 class="modal-title" id="EditUserModal">Benutzer bearbeiten</h4>
                       </div>
                       <div class="modal-body">
                         <div class="row">
                           <div class="col-xl-12 form-group">
                           <p><b>Username</b></p>
                             <input type="text" class="form-control" name="username" placeholder="'.$username.'">
                           </div>
                           <div class="col-xl-4 form-group">
                              <p><b>Vorname</b></p>
                             <input type="text" class="form-control" name="vorname" placeholder="'.$vorname.'">
                           </div>
                           <div class="col-xl-8 form-group">
                           <p><b>Nachname</b></p>
                           <input type="text" class="form-control" name="nachname" placeholder="'.$nachname.'">
                         </div>
                         <div class="col-xl-8 form-group">
                         <p><b>Email</b></p>
                             <input type="email" class="form-control" name="email" placeholder="'.$email.'">
                           </div>
                           <div class="col-xl-4 form-group">
                           <p><b>TelefonNr.</b></p>
                             <input type="text" class="form-control" name="handy" placeholder="'.$handy.'">
                           </div>
                           <div class="col-md-12 float-right">
                             <button class="btn btn-primary btn-outline" data-dismiss="modal" type="button">Speichern</button>
                           </div>
                         </div>
                       </div>
                     </form>
                   </div>
                 </div>
            </td>
                            </tr>';
                         }
           ?>

这个自动取款机头疼。

尝试这个

 $stmt = $odb->prepare("DELETE FROM Account WHERE id=? ");
 $stmt->execute(array($_GET['DeleteID']));

你必须设置你的变量 $ID

$ID = $_GET['DeleteID'];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM