2016-06-07 107 views
0

我遇到問題,當我嘗試刪除我在我的菜單頁中選擇的記錄後,我點擊刪除,標題顯示我該記錄已被刪除,作爲以及它顯示我的消息=刪除,但我意識到,該記錄仍然出現在我的菜單頁面,我也檢查了phpmyadmin,它也仍然存在,我想我沒有達到在刪除頁面選定的記錄,這裏是到目前爲止我的代碼: delete.phpPDO,無法刪除選定的記錄

<?php 
    include('koneksi.php'); 
    $page_number = $_GET['page']; 
    <?php 
include('koneksi.php'); 
$page_number = $_GET['page']; 
        $dbdit = new PDO('mysql:host=localhost;dbname=laundry','root',''); 
        $results = $dbdit->prepare("SELECT COUNT(*) FROM konsumen"); 
        $results->execute(); 
        $get_total_rows = $results->fetch(); 
        $results = $dbdit->prepare("DELETE FROM konsumen ORDER BY kode_pemesanan ASC LIMIT :page_number"); 
        $results->bindValue(':page_number',$page_number); 
        $results->execute(); 
        if($results){ 
         header('location:menu.php?message=deleted&&?page=1'); 
        } 

menu.php

if($typeuser == 'admin'){ 
         $item_per_page = 1; 
         $dbcon = new PDO('mysql:host=localhost;dbname=laundry','root',''); 
         $results = $dbcon->prepare("SELECT COUNT(*) FROM konsumen"); 
         $results->execute(); 
         $get_total_rows = $results->fetch(); 

         //breaking total records into pages 
         $pages = ceil($get_total_rows[0]/$item_per_page); 

         if(isset($_GET["page"])){ 
         $page_number = filter_var($_GET["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); 

          if(!is_numeric($page_number)){die('Invalid page number!<a href="?page=1"> Back! </a>');} //incase of invalid page number 
         }else{ 
         $page_number = 1; 
         } 

         $position = (($page_number-1) * $item_per_page); 

         $results = $dbcon->prepare("SELECT * FROM konsumen ORDER BY kode_pemesanan ASC LIMIT $position, $item_per_page"); 
         $results->execute(); 
         while($row = $results->fetch()){ 
          print_r ("<table> 
           <tr> 
           <td>Kode Pemesanan</td><td>:</td><td>".$row['kode_pemesanan']."</td></tr> 
           <tr><td>Atas Nama</td><td>:</td><td>".$row['atas_nama']."</td></tr> 
           <tr><td>Jumlah Baju</td><td>:</td><td>".$row['jmlhbaju']."</td></tr> 
           <tr><td>Jumlah Celana</td><td>:</td><td>".$row['jmlhcelana']."</td></tr> 
           <tr><td>Jumlah Jaket</td><td>:</td><td>".$row['jmlhjaket']."</td></tr> 
           <tr><td>Jumlah Jas</td><td>:</td><td>".$row['jmlhjas']."</td></tr> 
           <tr><td>Jumlah Dress</td><td>:</td><td>".$row['jmlhdress']."</td></tr> 
           <tr><td>Pengurus Pesanan</td><td>:</td><td>".$row['penguruspesanan']."</td></tr> 
           <tr><td>Status Pesanan</td><td>:</td><td>".$row['statuspesanan']."</td></tr> 
           <tr><td>Total Pesanan</td><td>:</td><td>".$row['totalpesanan']."</td> 
           </tr></table> 
           <br> 
           ");  
         } 
         $min = 1; 
         if(isset($_GET["page"]) && $_GET["page"] > $min){ 
         $previous = $page_number - 1; 
         echo "<a href='?page=$previous'>&larr;Previous</a>&nbsp;&nbsp;"; 
        } else echo "<a href='?page=1'>&larr;Previous</a>&nbsp;&nbsp;"; 

        echo "<a href='edit.php?page=$page_number'>Edit</a>&nbsp;&nbsp;"; 
        echo "<a href='delete.php?page=$page_number'>Delete</a>&nbsp;&nbsp;"; 

              if(isset($_GET["page"]) && $_GET["page"] < $pages){ 
         $next = $page_number + 1; 
         echo "<a href='?page=$next'>Next&rarr;</a>"; 
        } else echo "<a href='?page=$pages'>Next&rarr;</a>"; 
             } 



       } 
+0

這裏有很多錯誤,這就解釋了爲什麼收到的答案如此糟糕!請至少爲我們提供包括主鍵在內的'konsumen'表定義。 –

+0

抱歉我缺乏,所以konsumen表有kode_pemesanan作爲主鍵,atas_nama,jmlhbaju.jmlhcelana,jmlhjaket,jmlhjas,jmlhdress,penguruspesanan,statuspesanan和totalpesanan列,我在menu.php中使用分頁顯示konsumen表,它顯示好,它只能刪除選定的記錄,我想我無法達到選定的記錄。 – roger

回答

0

沒有與此的許多問題,但最主要的是,你試圖運行查詢是沒有意義的:

DELETE FROM konsumen ORDER BY kode_pemesanan ASC LIMIT :page_number 

不僅是你不指定記錄刪除但是您的DELETE查詢看起來更像是SELECT查詢,其中ORDER BYLIMIT

echo "<a href='delete.php?page=$page_number'>Delete</a>&nbsp;&nbsp;";鏈接將需要抓取循環內,並且使用的關鍵,而不是頁碼:

echo "<a href='delete.php?key={$row['kode_pemesanan']}'>Delete</a>&nbsp;&nbsp;"; 

當然,你需要糾正delete.php使用該密鑰。

相反的$page_number = $_GET['page'];,你可以把,最好做基本的驗證,然後校正PDO查詢:

$results = $dbdit->prepare("DELETE FROM konsumen WHERE kode_pemesanan = :kode_pemesanan"); 
$results->bindValue(':kode_pemesanan', $key, PDO::PARAM_INT); 

if($results->execute()){ 
    header('location:menu.php?message=deleted&&?page=1'); 
} 
else { // you can do better than this but it's just to show how to properly deal with returned values 
    die('error in delete query'); 
} 

可能還有其他的問題,但我不能對此進行測試您的網站上找到他們。

+0

我編輯我的代碼,使用主鍵作爲選定的記錄,但現在仍在工作..記錄仍然出現在menu.php和我的phpmyadmin – roger

+0

我試圖編輯消息=刪除到'。$ key。'=刪除,它不顯示我$ key值,它顯示我的菜單,php?=刪除&&?page = 1 – roger

+0

我給了你確切的代碼使用。 –

-1

請嘗試撥打$results->execute();PREPARING您的delete查詢語句。

$results = $dbdit->prepare("DELETE FROM konsumen ORDER BY kode_pemesanan ASC LIMIT :page_number"); 
$results->bindValue(':page_number',$page_number); 
$results->execute(); 
if($results){ 
    header('location:menu.php?message=deleted&&?page=1'); 
} 
+0

謝謝你回覆這篇文章,但它仍然無法正常工作.. – roger