user6930268
user6930268

Reputation:

How to resolved Call to a member function query() on a non-object in php?

I am calling function from URL and I am getting error Call to a member function query() on a non-object in this line( $result =$conn->query($sql);).My database connection is perfect. I am retrieving records from one table and inserting in second table. My SQL query is working.Would you help me in this?

URl:-http://domain.com/admin/delete.php?function=restore&res_id=7

$id = $_GET['res_id']; //retrive id from URL

include('db/connection.php');
switch($_GET['function']) {
    case 'delete':delete();break;
    case 'restore':restore();break;
    default : redirect('index.php');
}

function restore(){
    $sql="INSERT INTO request(Id, Name, Email, Mobile_no) SELECT Id, Name, Email, Mobile_no FROM trash WHERE Id=$id";
    $result =$conn->query($sql);
$delete="DELETE FROM trash  WHERE  Id='$id'";
    if ($conn->query($delete) === TRUE) {
        header('Location:dashboard.php?submitted=1');
    } else {
        echo "Error deleting record: " . $conn->error;
    }
}

Upvotes: 0

Views: 66

Answers (2)

Didarul Alam
Didarul Alam

Reputation: 79

Hope this solves your problem.

<?php
$conn = new mysqli('localhost', 'root', '', 'stakcoverflow');

/*
 * This is the "official" OO way to do it,
 * BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
 */
if ($conn->connect_error) {
    die('Connect Error (' . $conn->connect_errno . ') '
            . $conn->connect_error);
}

/*
 * Use this instead of $connect_error if you need to ensure
 * compatibility with PHP versions prior to 5.2.9 and 5.3.0.
 */
if (mysqli_connect_error()) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

//echo 'Success... ' . $conn->host_info . "\n";

function restore($conn){ //function restore(){
    $id = 1;
    echo $sql="INSERT INTO request(Id, Name, Email, Mobile_no) SELECT Id, Name, Email, Mobile_no FROM trash WHERE Id=".$id."";
    $result = $conn->query($sql);
}

if(restore($conn))
{
    echo "Success"; 
};

$conn->close();

Upvotes: 1

Aamir Sarwar
Aamir Sarwar

Reputation: 271

pass $conn variable to restore function e.g

restore($conn)

and change function

function restore(){

to

function restore($conn){

and

case 'restore':restore();break;

to

case 'restore':restore($conn);break;

...hope it will help you

Below is your updated code

URl:-http://domain.com/admin/delete.php?function=restore&res_id=7

$id = $_GET['res_id']; //retrive id from URL

include('db/connection.php');
switch($_GET['function']) {
    case 'delete':delete();break;
    case 'restore':restore($conn);break; //case 'restore':restore();break;
    default : redirect('index.php');
}

function restore($conn){ //function restore(){
    $sql="INSERT INTO request(Id, Name, Email, Mobile_no) SELECT Id, Name, Email, Mobile_no FROM trash WHERE Id=$id";
    $result =$conn->query($sql);
    if ($conn->query($delete) === TRUE) {
        header('Location:dashboard.php?submitted=1');
    } else {
        echo "Error deleting record: " . $conn->error;
    }
}

Also change

$sql="INSERT INTO request(Id, Name, Email, Mobile_no) SELECT Id, Name, Email, Mobile_no FROM trash WHERE Id=$id";

with

$sql="INSERT INTO request(Id, Name, Email, Mobile_no) SELECT Id, Name, Email, Mobile_no FROM trash WHERE Id=". (int)$_GET['res_id'];

Upvotes: 0

Related Questions