0

I have a PHP script that sends an email, selects data from a table and then inserts some data into a table. I have 3 querys in the same file. But on the last query I get the error message

Trying to get property 'num_rows' of non-object in C:\xampp\htdocs\Revolutionen\includes\accept.php on line 76

Ive looked for any typo-errors but coulden't find any. Anyone that's smarter than me that could help me out? Hehe.

The full code:

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "emildeveloping5";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

mysqli_set_charset($conn,"utf8");

$sql = "UPDATE ansokningar SET besvarad=2 WHERE id=".$_GET['row_id'];

if ($conn->query($sql) === TRUE) {
     echo "Ansökningen godkänndes.";
} else {
    echo "Error updating record: " . $conn->error;
}

$sql2 = "SELECT email FROM ansokningar WHERE id=".$_GET['row_id'];
$result = $conn->query($sql2);

if ($result->num_rows > 0) {
    echo "";
    while($row = $result->fetch_assoc()) {
     $email = $row['email'];
    }

     $to = "$email";
     $subject = "Ansökan | Stockholm Emergency Roleplay";
     $txt = "
     <html>
     <head>
     </head>
     <body>
     <p>Din ansökan är godkänd!</p>
     <p>Intervju tider står i våran Discord.</p>
     <p>Mvh Stockholm Emergency Roleplay.</p>
     </body>
     </html>

     ";
     $headers = "From: noreply@stockholmemergencyroleplay.se" . "\r\n" .
     "CC: noreply@stockholmemergencyroleplay.se";

     mail($to,$subject,$txt,$headers);

     echo ('Email är skickat.');

} else {
    echo "Error" . $conn->error;
}

$sql3 = "SELECT steam FROM ansokningar WHERE id=".$_GET['row_id'];
$result2 = $conn->query($sql3);

if ($result2->num_rows > 0) {
    echo "";
    while($row = $result2->fetch_assoc()) {
        $steam = $row['steam'];
     echo "";
    }
     echo ('Steam HEX fångat.');

} else {
    echo "Error" . $conn->error;
}

$sql4 = "INSERT INTO whitelist (identifier, whitelisted) VALUES ('$steam', '1')";
$result3 = $conn->query($sql4);

if ($result3->num_rows > 0) {
    echo "";
    while($row = $result3->fetch_assoc()) {
     echo "";
    }
     echo ('Personen blev automatiskt whitelistad.');

} else {
    echo "Error" . $conn->error;
}

$conn->close();
?>```

2 Answers2

-1

Actually the problem is you are trying to access a variable out side of while loop.

if ($result2->num_rows > 0) 
{ 
 echo ""; 
 while($row =$result2->fetch_assoc())     { 
$steam = $row['steam']; 
echo ""; 
$sql4 = "INSERT INTO whitelist (identifier, whitelisted) VALUES.   ('$steam', '1')"; 
} 
echo 'Steam HEX fångat.'; } 
else { echo "Error" . $conn->error; }

Then $sql4 = select query of whitelist

-1

INSERT queries don't return results. So when you do

$result3 = $conn->query($sql4);

$result3 will be either TRUE or FALSE, not a mysqli_result object. You shouldn't try to check the number of rows or call $result3->fetch_assoc(), since there's nothing to fetch. Just check whether it was successful:

if ($result3) {
    echo 'Personen blev automatiskt whitelistad.';
} else {
    echo "Error" . $conn->error;
}
Barmar
  • 669,327
  • 51
  • 454
  • 560