0

For some reason, I keep getting this error.

Here is the code:

<? 
if(isset($_POST['reciever'])){;

$aan = htmlspecialchars(filter_var($_POST['reciever'], FILTER_SANITIZE_STRING));
$stmt2 = $mysqli->prepare("SELECT `id` FROM `gebruikers` WHERE `naam` = ? ");
$stmt2->bind_param("s", $aan);
$stmt2->execute();
$kpey = $stmt2->get_result()->fetch_object();

$userid = htmlspecialchars(filter_var($_SESSION['user_id'], FILTER_VALIDATE_INT));
$stmt3 = $mysqli->prepare("SELECT `naam` FROM `gebruikers` WHERE `id` = ? ");
$stmt3->bind_param("i", $userid);
$stmt3->execute();
$kpeya = $stmt3->get_result(); 


$aan2 = $kpey->id;

$bericht = htmlspecialchars(filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING));
$date = date("Y-m-d G:i:s");
$userid = htmlspecialchars(filter_var($_SESSION['user_id'], FILTER_VALIDATE_INT));
$stmt1 = $mysqli->prepare("INSERT INTO msg (msg_van, msg_aan, msg_txt, msg_date) 
VALUES (?, ?, ? , ? )");
$stmt1->bind_param('ssss', $userid, $aan2, $bericht,$date);  
$stmt1->execute();

echo "Message has been send!"; ?>

Backend log:

PHP Notice: Trying to get property of non-object

This error is at line 69 which is: $aan2 = $kpey->id; in this example.

Dharman
  • 26,923
  • 21
  • 73
  • 125
  • This will indicate that `$kpey` is not an object. Likely meaning that the query did not return a result. You should probably null-check this. `if (!is_null($kpey)) {}` – Peter B Aug 02 '20 at 13:10
  • Thanks for your reaction. The code executes the way it should, but than with this error. So the value is not null than right? – newusertomysqli12 Aug 02 '20 at 13:14
  • 1
    You could also do a `var_dump($kpey); die();` just before line 69 and confirm the format of the variable for attempting to use it. – Peter B Aug 02 '20 at 13:16
  • I id the var_dump, it gives the result as i posted before. Not empty but exactly what it should produce. – newusertomysqli12 Aug 02 '20 at 17:09
  • Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Aug 03 '20 at 12:21
  • Thanks for your reaction. I have checked this topic but it says nothing about this sollution. The error reporting as they Descripe is allready on in my code. Probebly why it shows this “notice” in the first place. So no still no sollution :( – newusertomysqli12 Aug 04 '20 at 05:07

1 Answers1

0

I think I see what the problem is here. The first query isn't returning anything because you are telling MySQL that $aan is an integer in $stmt2->bind_param("i", $aan); when it is actually a string. Change the "i" to "s" and then you should get a response and get an object you can access.

EDIT: When testing myself, I realized that $aan is always an empty string. I am not familiar with the filter_input() function but when I use filter_var() instead I get the expected string, and the whole thing works as expected. Here is what that other line would look like:

$aan = htmlspecialchars(filter_var($_POST['reciever'], FILTER_SANITIZE_STRING));

Small note: 'reciever' is spelled like 'receiver', but I can see that it is not your native language. For consitincy try to use english or dutch everywhere (POST variables and SQL columns)

J0R1AN
  • 543
  • 6
  • 9
  • Correct, just saw that and edited it. but still the same error :) – newusertomysqli12 Aug 02 '20 at 13:18
  • Could you `var_dump()` both `$kpey` with and without `->fetch_object()`? I want to know if the database is actually returning anything – J0R1AN Aug 02 '20 at 13:24
  • I just found what the actual issue was. Look at my edited post – J0R1AN Aug 02 '20 at 13:39
  • Thank you for your comment. Tried what you said and still gives the same object error :(. – newusertomysqli12 Aug 02 '20 at 17:00
  • I tried what you said with the dump. Here is the result: With object: object(stdClass)#9 (1) { ["id"]=> int(123160) } Without object: object(stdClass)#9 (1) { ["id"]=> int(123160) } Do note, the code works, it does what it needs to do. Only produces this error while it does.. Still get this error : Backend log: PHP Notice: Trying to get property of non-object – newusertomysqli12 Aug 02 '20 at 17:04
  • Updated the code to your proposal, still gives the same eroor though. See updated code above.. – newusertomysqli12 Aug 02 '20 at 17:05
  • Correction: without the object it is this output object(mysqli_result)#5 (5) { ["current_field"]=> int(0) ["field_count"]=> int(1) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) } – newusertomysqli12 Aug 02 '20 at 17:37
  • Then I'm not sure why it still gives the error, since `var_dump()`ing it gives a normal object with an `->id` to access. What you can do to get rid of the Notice is use `if (!is_null($kpey)) {}` around the code that accesses it. That way it makes sure not to execute the code if the response is NULL – J0R1AN Aug 03 '20 at 07:15