2017-12-06 6 views
-1

whileループ内でデータベースレコードを削除しようとしています。 whileループを使用してユーザーリストをテーブルに表示しています。私はボタンを持って、ブートストラップモーダルはモーダルウィンドウを開きます。そのウィンドウで私は削除ボタンに提出している。 whileループ。 問題は、このレコードを削除しようとしていますが、そのレコードを削除しています。あなたは問題があることを確認できますか?whileループ内のクエリを削除する

ありがとうございました。

<table class="table table-hover"> 
    <thead> 
     <tr> 
     <th>Id</th> 
     <th>Name</th> 
     <th>Surname</th> 
     <th>Email</th> 
     <th>Password</th> 
     <th>*</th> 
     </tr> 
    </thead> 
    <tbody> 


<?php 

    $q = "SELECT * FROM users"; 
    $r = mysqli_query($dbc,$q); 
    while($list = mysqli_fetch_assoc($r)){ 
     if(isset($_POST['del_submit'])){ 
      $q = "DELETE FROM users WHERE id = '$list[id]' "; 
      $r = mysqli_query($dbc, $q); 
       header('Location: index.php?page=7'); 
      } 

     echo '<tr>'; 
     echo '<td>'.$list['id'].'</td>'; 
     echo '<td>'.$list['name'].'</td>'; 
     echo '<td>'.$list['surname'].'</td>'; 
     echo '<td>'.$list['email'].'</td>'; 
     echo '<td>'.$list['password'].'</td>'; 
     echo '<td><button class="btn btn-danger btn-xs" data-toggle="modal" data-target=".delete'.$list['id'].'"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button> '; 

     echo '</tr><form method="post" action="#">';   
     echo '<div class="modal fade delete'.$list['id'].'"> 
       <div class="modal-dialog modal-sm" role="document"> 
       <div class="modal-content">    
        <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
        <h4 class="modal-title">Delete <strong class="text-primary">'.$list['name'].' ?</strong></h4> 
        </div> 

        <div class="modal-body"> 
        <strong class="text-primary">'.$list['name'].' '.$list['surname'].'</strong><br> 
        Are you Sure? 
        </div> 
        <div class="modal-footer"> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> 

        <button type="submit" name="del_submit" id="del_submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button> 
        </div> 
       </div> 
       </div> 
      </div></form>'; 

    } 
?> 




    </tbody> 
</table> 
+0

になり、あなたのエラーログを確認しましたか?クエリが機能していると仮定しています。 あなたのファイルの先頭にエラー報告を追加するのは、あなたのファイルの最初の '<?php'タグ' error_reporting(E_ALL);の直後です。 ini_set( 'display_errors'、1); ' –

+0

最後のレコードを削除するかどうか選択ループに項目を削除するのは奇妙です。 '$ list ['id']'のようなナンセンスではなく、 'id =(int)$ _ POST ['id']'という条件を使ってレコードを削除します。 – panther

+0

リスト内の最初のIDを削除します。たとえば、最初のid = 1秒= 2 ... 4:4 :たとえば、3番目の削除ボタンをクリックすると、その最初のレコードが削除されます。 –

答えて

0

にこれを追加しactionidパラメータ

<?php 
// replace 
?> 
<button type="submit" name="del_submit" id="del_submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button> 
<?php 
// with this: 
?> 
<a href='?id=<?= (Int) $list['id']; ?>&action=delete'><button class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button></a> 

と同じページにリンクを追加する必要がありますこちらです。 Ajaxでの削除操作を処理して別のスクリプトを呼び出すのはなぜですか?
削除ボタンでidの値を渡し、代わりにボタンで渡したid値を使用する必要があります。
モーダル追加value属性中にあなたのために削除ボタン

<button type="submit" name="del_submit" value="'.$list['id'].'" id="del_submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button> 

そして、IDを取得し、IDを使用しています。あなたがユーザーをリダイレクトされている場合、単に

`ヘッダー使用、削除要素のidである場合は、そのは

if(isset($_POST['del_submit'])){ 
    $listItemID = $_POST['del_submit']; 
    $q = "DELETE FROM users WHERE id = '$listItemID' "; 
    $r = mysqli_query($dbc, $q); 
     header('Location: index.php?page=7'); 
} 

を動作するはずです(「?場所:index.phpのページを= $ listItemID」); header( 'Location:index.php?page = 7')の代わりに。

完全なコードは

<table class="table table-hover"> 
    <thead> 
     <tr> 
      <th>Id</th> 
      <th>Name</th> 
      <th>Surname</th> 
      <th>Email</th> 
      <th>Password</th> 
      <th>*</th> 
     </tr> 
    </thead> 
    <tbody> 


     <?php 

     $q = "SELECT * FROM users"; 
     $r = mysqli_query($dbc,$q); 
     while($list = mysqli_fetch_assoc($r)){ 
      if(isset($_POST['del_submit'])){ 
       $listItemID = $_POST['del_submit']; 
       $q = "DELETE FROM users WHERE id = '$listItemID' "; 
       $r = mysqli_query($dbc, $q); 
       header('Location: index.php?page=7'); 
      } 

      echo '<tr>'; 
      echo '<td>'.$list['id'].'</td>'; 
      echo '<td>'.$list['name'].'</td>'; 
      echo '<td>'.$list['surname'].'</td>'; 
      echo '<td>'.$list['email'].'</td>'; 
      echo '<td>'.$list['password'].'</td>'; 
      echo '<td><button class="btn btn-danger btn-xs" data-toggle="modal" data-target=".delete'.$list['id'].'"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button> '; 

      echo '</tr><form method="post" action="#">';   
      echo '<div class="modal fade delete'.$list['id'].'"> 
       <div class="modal-dialog modal-sm" role="document"> 
       <div class="modal-content">    
        <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
        <h4 class="modal-title">Delete <strong class="text-primary">'.$list['name'].' ?</strong></h4> 
        </div> 

        <div class="modal-body"> 
        <strong class="text-primary">'.$list['name'].' '.$list['surname'].'</strong><br> 
        Are you Sure? 
        </div> 
        <div class="modal-footer"> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> 

        <button type="submit" name="del_submit" value="'.$list['id'].'" id="del_submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button> 
        </div> 
       </div> 
       </div> 
      </div></form>'; 

     } 
     ?> 




    </tbody> 
</table> 
+0

その完全に動作します。 Thnks百万 –

+0

あなたは大歓迎です。 –

1

がそれに削除ページとリンクを作成します。 はここに私のコードです。 最初のあなたは、私が最初にあなたのスクリプトは、安全かつ効率的ではないことを認めるだろう、あなたのページの上部

<?php 

if(isset($_GET['id']) && isset($_GET['action']) && $_GET['action'] == 'delete') { 
    $id = (Int) $_GET['id']; 
    $query = 'DELETE FROM `users` WHERE `users`.`id` = ?'; 
    $mysqli = new mysqli('localhost','user','password','database'); 
    $stmt = $mysqli->prepare($query); 
    $stmt->bind_param('i',$id); 
    $stmt->execute(); 
    $stmt->close(); 
    $mysqli->close(); 
    echo 'Deleted! (if exists)'; 
} 
+0

thnksしかし、私は同じページにこれを作る必要があります –

+0

あなたのページの上部に今追加することができる場合は固定 – azjezz

関連する問題