hello my dear Friends,
for a long time i was not here. I hope u all are geting well. again i have problem in login form anyone can guide me plz, code is below

<?php
   include "conn.php";

  if($_SERVER["REQUEST_METHOD"] == "POST"){ 
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_SPECIAL_CHARS);
$password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_SPECIAL_CHARS);

$sql = "SELECT * FROM admintb WHERE username = '$username' and passcode = '$password'";

$result = $conn->query($sql);

if( $result->num_rows > 0)
 {

         echo "welcome!! You are loged in ";
      }else {
         $error = "Your Login Name or Password is invalid";
      }
  }
?>

Pleaez hepl me as soon as posible.

Hi,

can you describe the problem?

yes , with below code i always receive error " Trying to get property of non-object in "

<?php
   include "conn.php";

 if ($_SERVER["REQUEST_METHOD"] == "POST"){ 
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_SPECIAL_CHARS);
$password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_SPECIAL_CHARS);

$sql = "SELECT * FROM admintb WHERE username = '$username' and passcode = '$password'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<br> id: ". $row["id"]. " - Name: ". $row["AdminName"]. " " . $row["Position"] . "<br>";
     }
} else {
     echo "0 results";
}
  }
$conn->close();
?>

with below code i always receive error " Trying to get property of non-object in "

What line exactly?

Line No 11

if ($result->num_rows > 0) 
Member Avatar for diafol

From: http://php.net/manual/en/mysqli.query.php

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

Seems you are getting FALSE as $result is not an object.

You can always check the last error :

if(!$result) printf("Errormessage: %s\n", $conn->error);

then what can i use instead of

$result->num_rows > 0

Please guide me in detail ....

Member Avatar for diafol

You must first ascertain whether you have a successful query or not. If so, use your code, if not, check what the problem is (my one-liner).

ok thx i soved this problem, some mistype was there .....i post correct code here.

<?php
   include "conn.php";

 if ($_SERVER["REQUEST_METHOD"] == "POST"){ 
$username = filter_input(INPUT_POST, 'user', FILTER_SANITIZE_SPECIAL_CHARS);
$password = filter_input(INPUT_POST, 'pass', FILTER_SANITIZE_SPECIAL_CHARS);

$sql = "SELECT * FROM admintb WHERE username ='$username' and password='$password'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<br> id: ". $row["id"]. " - Name: ". $row["NameAdmin"]. " " . $row["Position"] . "<br>";
     }
} else {
     echo "0 results";
}
  }
$conn->close();
?>  
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.