PDO无法删除所选记录

时间:2016-06-07 05:09:09

标签: php pdo

当我尝试删除我在菜单页面中选择的记录时遇到问题,在我点击删除后,标题显示该记录已被删除,同时它显示消息=删除,但我意识到记录仍然出现在我的菜单页面,我也检查了phpmyadmin它仍然存在,我想我没有到达删除页面中的选定记录,这里是我的代码到目前为止: delete.php

    <?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>";
                                        }



                }

2 个答案:

答案 0 :(得分:0)

这有很多问题但主要的问题是您尝试运行的查询根本没有意义:

DELETE FROM konsumen ORDER BY kode_pemesanan ASC LIMIT :page_number

您不仅没有指定要删除的记录,而且DELETE查询看起来更像是SELECTORDER BY的{​​{1}}查询。

您的LIMIT链接需要位于获取循环内并使用密钥,而不是页码:

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查询:

$key = $_GET['key'];

可能还有其他问题但我无法在您的网站上对此进行测试以找到它们。

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