Why i get this error Notice: Trying to get property of non-object in C:\xampp\htdocs\asdf\navigation.php on line 214 i have this code:

<?php
                                    if(isset($_POST['sendTicket'])) {
                                        $ticketID = $_POST['ticketID'];
                                        $ticketReceiver = $_POST['ticketReceiver'];

                                        $ticket_id = getUserData('kladilnica', 'ticket_id');
                                        $sender = getUserData('users', 'Username');

                                        if(!empty($ticketID) && !empty($ticketReceiver)) {
                                            $sql = "INSERT INTO ticket (senderName, receiverName, Date, ticketID, match1, match2, match3, match4, match5, match6, match7, match8, match9, match10, match11, match12, match13, match14, match15, match16, tip1, tip2, tip3, tip4, tip5, tip6, tip7, tip8, tip9, tip10, tip11, tip12, tip13, tip14, tip15, tip16, Bet, Gain, Odd) 
                                            SELECT Username, '".$ticketReceiver."', Date, ticket_id, match1, match2, match3, match4, match5, match6, match7, match8, match9, match10, match11, match12, match13, match14, match15, match16, tip1, tip2, tip3, tip4, tip5, tip6, tip7, tip8, tip9, tip10, tip11, tip12, tip13, tip14, tip15, tip16, Uplata, Dobivka, Odds FROM kladilnica WHERE ticket_id='".$ticketID."' AND `Username`='".$ticketReceiver."'";
                                            $result = $conn->query($sql);
                                            if($result === false) {
                                                trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
                                            } else {
                                                if($ticketReceiver == $sender) {
                                                ?>
                                                    <p>You cannnot send to yourself!</p>
                                                <?php
                                                } else {
                                                    if($result->num_rows > 0) {
                                                    ?>
                                                        <p>Sended successfully</p>
                                                    <?php
                                                    } else {
                                                    ?>
                                                        <p>Invalid Ticket or Receiver</p>
                                                    <?php
                                                    }
                                                }
                                            }
                                        } else {
                                        ?>
                                            <p>Can't leave empty</p>
                                        <?php
                                        }
                                    }
                                ?>

Btw the line 214 is this line if($result->num_rows > 0)

Member Avatar
diafol

If you're using mysqli, then the query method on and INSERT statement will give TRUE on success. So $result is TRUE, not an object (as it would be on successful query of a SELECT statement).

If you need the number of affeted rows:

echo $conn->affected_rows;

Note that you get this from the mysqli object itself, not from an object created by it.

No i just need to remove this error line because it shows the info lines and works well

$result->num_rows

is a method for select query, while

$conn->affected_rows;

is for the affected rows after executing an insert query. That's what I think. I might be wrong ????

I dont quite get it what should i do to remove the line ? :/
Sorry about this long time no PHP ...
BTW yes im using MySQLi

okay, I double checked here and I was wrong, affected_rows can be use either update, insert, and select queries.

so if you defined $conn like this

$conn = new mysqli("localhost", "my_user", "my_password", "your_database_name");

you can replace the

$result->num_rows

with ( as suggested by Diafol above)...

$conn->affected_rows;

you should be able to evaluate

<?php if($conn->affected_rows > 0){ ?>

    message sent.

<?php

}

?>

but than it gives me error Invalid Ticket or Receiver even if i type right input