简体   繁体   中英

Record can't delete in PHP

I'm currently working on an food restaurant system.

I'm a beginner in 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
    $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

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:

        $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. 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. Please refer to: CodeIgniter Query Builder Class

There is no ->delete function in MySQLi. You're going to just use

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

Each command in SQL is in the line. The calls to the SQL object are only a proxy to the internal database. SQL's library of commands is internal. 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.

I assume that you are using some class or framework that manages SQL queries to use the select and delete methods.

Just change the query variable name: $delData = $this->db->delete( $query );

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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