-1

I have the code that has been giving me the following error.

Trying to get property of non-object in G:\xampp\htdocs\abc\admin\delete.php on line 68

Here's the code,

<?php
    if(isset($_POST["submit"])) {
    include '../includes/db.php';

    $sql = "DELETE FROM admin WHERE aid= '".$_POST['aid']. "'";

    $result = $conn->query($sql);
    $count = $result->num_rows;

    if ($count > 0) {
        echo "success";
    } else {
        echo "fail";
    }

    $conn->close();
    }
?>

db.php has the code that connects to the database.

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "fgfg";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

?>

It executes the query but the error message is displayed.

What is causing it?

innowqhy
  • 53
  • 8

1 Answers1

1

What you delete what you need to return is affected_rows. On Delete query it gives affected rows not num rows.

Use $conn->affected_rows instead of $result->num_rows

Please refer Affected_rows.

Edit: You need to pass connection string not result.

Community
  • 1
  • 1
Apb
  • 979
  • 1
  • 8
  • 25