Jump to content

Fatal error: Uncaught Error: Call to a member function query() on null in


Recommended Posts

Someone can help with this problem? :(

Error:

"Fatal error: Uncaught Error: Call to a member function query() on null in /profiles/d/d4/d4v/d4vez/listazakupow.cba.pl/pages/config.php:16 Stack trace: #0 /profiles/d/d4/d4v/d4vez/listazakupow.cba.pl/index.php(18): listaZakupow->usun('6') #1 {main} thrown in /profiles/d/d4/d4v/d4vez/listazakupow.cba.pl/pages/config.php on line 16"

 

index.php

<!doctype html>

<head>

    <meta charset="utf-8" />

    <title>Lista Zakupów</title>

</head>

<body>

    <?php

        require_once 'pages/config.php';

 

        $lz = new listaZakupow;

 

        if(isset($_GET['akcja'])){

            switch($_GET['akcja']){

                case 'dodaj':

                    $lz->dodaj($_POST['towar']);

                    break;

                case 'usun':

                    $lz->usun($_GET['id']);

                    break;

                case 'zmien':

                    $lz->zmienStan($_GET['id']);

                    break;

            }

        }

 

        $sql = 'SELECT * FROM zakupy';

        $result = mysqli_query($link,$sql);

 

        if(mysqli_num_rows($result) == 0){

            echo 'Nie masz jeszcze dodanego żadnego przedmiotu do kupienia';

        } else{

            echo 'Twoja lista zakupów:';

            while($row = mysqli_fetch_assoc($result)){

 

                $towarid = $row['id'];

                $towar = $row['towar'];

                $stan = $row['stan'];

 

                echo '<ul>';

                    if($stan=='T'){

                        $kupione = 'x';

                    } else{

                        $kupione = ' ';

                    }

                    echo '<li>'.$towar.' <a href="index.php?akcja=zmien&id='.$towarid.'">['.$kupione.']</a>

                    <a href="index.php?akcja=usun&id='.$towarid.'">usuń</a></li>';

                echo '</ul>';

            }

        }

    ?>

    <form action="index.php?akcja=dodaj" method="post">

        Towar: <input type="text" name="towar" />

        <input type="submit" value="Dodaj" />

</body>

</html>

 

config.php

<?php

    $link = new mysqli('localhost,'name','password','database');

        if ($link -> connect_errno) {

            echo 'Nie udało się połączyć z MySQL: ' . $mysqli -> connect_error;

            exit();

        }

   

    class listaZakupow{

 

        function dodaj($nazwa){

            $link->query('INSERT INTO zakupy VALUES(null,\''.mysqli_real_escape_string($nazwa,$link).'\',\'N\');');

            header("location: index.php");

        }

 

        function usun($id){

            $link->query('DELETE FROM zakupy WHERE id='.intval($id).' limit 1');

            header("location: index.php");

        }

 

        function zmienStan($id){

            $akt = mysqli_fetch_assoc($link->query('SELECT stan FROM zakupy WHERE id='.intval($id).' limit 1'));

            if($akt['stan']=='T'){

                $nw = 'N';

            } else{

                $nw = 'T';

            }

            mysqli_query('UPDATE zakupy SET stan=\''.$nw.'\' where id='.intval($id).' limit 1');

            header("location: index.php");

        }

 

    }

?>

Edited by Dawid
function dodaj($nazwa){
	$link->query('INSERT INTO zakupy VALUES(null,\''.mysqli_real_escape_string($nazwa,$link).'\',\'N\');');

Variables like $link defined outside of functions are not available inside of functions.

Pass $link as an argument to the function, like you're already doing with $nazwa.

43 minutes ago, requinix said:
function dodaj($nazwa){
	$link->query('INSERT INTO zakupy VALUES(null,\''.mysqli_real_escape_string($nazwa,$link).'\',\'N\');');

Variables like $link defined outside of functions are not available inside of functions.

Pass $link as an argument to the function, like you're already doing with $nazwa.

Could you show me how? Because I guess I didn't understand 😕

Edited by Dawid

Spend some time learning about PHP so that you can understand what sort of changes you will be making.

// this variable is defined outside of foo()
$outside = 123;

function foo() {
	// you cannot use $outside here
	echo $outside; // warning: undefined variable
}
foo();

// if you want to use a variable inside a function then you must pass it as a function argument

function bar($inside) {
	// you can use $inside here and it will have the same value as $outside
	echo $inside; // 123
}
bar($outside);

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.