简体   繁体   English

PHP 中的记录无法删除

[英]Record can't delete in PHP

I'm currently working on an food restaurant system.我目前正在研究食品餐厅系统。

I'm a beginner in PHP.我是 PHP 的初学者。 I'm having a bit of a problem deleting a record from the database.When I try to delete a record, it unlink the image, but not deleting the row.我在从数据库中删除记录时遇到了一点问题。当我尝试删除记录时,它会取消链接图像,但不会删除行。 How can I solve it?我该如何解决?

Any help would be appreciated.任何帮助,将不胜感激。 Thank you.谢谢你。

Here is my script::这是我的脚本::

productlist.php产品列表.php

<?php
    $pd = new Product();
    $fm = new Format();
    if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['submit'])){
        $insertProduct = $pd->productInsert($_POST,$_FILES);
    }
    if(isset($_GET['delpro'])){
        $id = $_GET['delpro'];
        $delpro = $pd->delProById($id);
    }
?>
<div class="grid_10">
    <div class="box round first grid">
        <h2>Post List</h2>
        <div class="block">  
            <table class="data display datatable" id="example">
            <tbody>
            <?php
                $getPd = $pd->getAllProduct();
                if($getPd){
                    $i = 0;
                    while($result = $getPd->fetch_assoc()){
                       $i++;
            ?>
                <tr class="odd gradeX">
                    <td><?php echo $i;?></td>
                    <td><?php echo $result['productName'];?></td>
                    <td><?php echo $result['catName'];?></td>
                    <td><?php echo $result['brandName'];?></td>
                    <td><?php echo $fm->textShorten($result['body'],50);?></td>
                    <td><?php echo $result['price'];?></td>
                    <td><img src="<?php echo $result['image']; ?>" height="40px" width="60px"/></td>
                    <td>
                    <?php 
                        if($result['type'] == 0){
                            echo "Featured";
                        } else{
                            echo "General";
                        }

                    ?></td>
                    <td><a href="productedit.php?proid=<?php echo $result['productId'];?>">Edit</a> || <a onclick="return confirm('Are you sure to delete!')" href="?delpro=<?php echo $result['productId'];?>">Delete</a></td>
                </tr>
                <?php
                        }
                    }
                ?>
            </tbody>
        </table>

       </div>
    </div>
</div>

Product.php产品.php

public function delProById($id){
            $query = "SELECT * FROM tbl_product WHERE productId = '$id'";
            $getData = $this->db->select($query);
            if($getData){
                while($delImg = $getData->fetch_assoc()){
                    $dellink = $delImg['image']; 
                    unlink($dellink);

                }
            }


            $delquery = "DELETE FROM tbl_product WHERE productId = '$id'";
            $delData = $this->db->delete($query);
            if($delData){
                $msg = "<span class='success'>Product Deleted Successfully</span>";

                return $msg;
            }
            else {
                $msg = "<span class='error'>Product Not Deleted.</span>";
                return $msg;
            }


        }

You are executing delete statement using the wrong query.您正在使用错误的查询执行删除语句。 Instead of $query which holds the SELECT statement you should do the following:您应该执行以下操作,而不是包含 SELECT 语句的 $query:

        $delData = $this->db->delete($delquery);

I don't know what framework you are using (it seems like CodeIgniter or so) but clearly you are trying to run the wrong query.我不知道您使用的是什么框架(看起来像 CodeIgniter 左右)但显然您正在尝试运行错误的查询。 Try:尝试:

$delData = $this->db->query($delQuery)

instead of代替

$delData = $this->db->delete($query)

If the PHP framework is really CodeIgniter (or whatever framework), it is highly discouraged to run raw sql in PHP.如果 PHP 框架确实是 CodeIgniter (或任何框架),则强烈建议不要在 Z2FEC392304A7F9B37AC1 中运行原始 sql Please refer to: CodeIgniter Query Builder Class请参考: CodeIgniter 查询生成器 Class

There is no ->delete function in MySQLi. MySQLi中没有->delete function。 You're going to just use你将只使用

$this->db->query($query);

Each command in SQL is in the line. SQL 中的每个命令都在该行中。 The calls to the SQL object are only a proxy to the internal database.对 SQL object 的调用只是内部数据库的代理。 SQL's library of commands is internal. SQL 的命令库是内部的。 So we just use the variable, which is the object, now after connecting, and we delete the record(s) brought up by the query itself.所以我们只使用变量,即 object,现在连接后,我们删除查询本身带来的记录。

I assume that you are using some class or framework that manages SQL queries to use the select and delete methods.我假设您正在使用一些 class 或管理 SQL 查询的框架来使用 select 和删除方法。

Just change the query variable name: $delData = $this->db->delete( $query );只需更改查询变量名称: $delData = $this->db->delete( $query );

to: $delData = $this->db->delete( $delquery );到:$delData = $this->db->delete( $delquery );

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

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