Dawid Posted January 20, 2022 Share Posted January 20, 2022 (edited) 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 January 20, 2022 by Dawid Quote Link to comment https://forums.phpfreaks.com/topic/314438-fatal-error-uncaught-error-call-to-a-member-function-query-on-null-in/ Share on other sites More sharing options...
requinix Posted January 20, 2022 Share Posted January 20, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314438-fatal-error-uncaught-error-call-to-a-member-function-query-on-null-in/#findComment-1593479 Share on other sites More sharing options...
Dawid Posted January 20, 2022 Author Share Posted January 20, 2022 (edited) 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 January 20, 2022 by Dawid Quote Link to comment https://forums.phpfreaks.com/topic/314438-fatal-error-uncaught-error-call-to-a-member-function-query-on-null-in/#findComment-1593480 Share on other sites More sharing options...
requinix Posted January 20, 2022 Share Posted January 20, 2022 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); Quote Link to comment https://forums.phpfreaks.com/topic/314438-fatal-error-uncaught-error-call-to-a-member-function-query-on-null-in/#findComment-1593481 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.