-1
<?php
   include("config.php");
   session_start();

   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form 

      $myusername = mysqli_real_escape_string($db,$_POST['username']);
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 

      $sql = "SELECT user_id FROM user WHERE username = $myusername and password = $mypassword";
      $result = mysqli_query($db,$sql);
      $rows = mysqli_fetch_array($result);
      $active = $rows["user_id"];

      $count = mysqli_num_rows($active);

      // If result matched $myusername and $mypassword, table row must be 1 row

      if($count == 1) {
         session_register("myusername");
         $_SESSION['login_user'] = $myusername;

         header("location: index.php");
      }else {
         $error = "Your Account Credentials is Invalid, Please Try Again";
      }
   }
?>

I am new to PHP and have a new question. I'm starting fresh with mysqli. And i want to Create a simple Login Form with php. can someone help me with this Error?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Alexis Villar
  • 371
  • 1
  • 3
  • 15
  • 1
    Try `$count = mysqli_num_rows($result);` instead of `$count = mysqli_num_rows($active);`! – the.salman.a Mar 27 '18 at 08:38
  • yes as @the.salman.a suggests you have to count the varibal $result that really has all the entries, or either instead of `$count == 1` you could check if `$rows["user_id"]` exists or now, without counting. – sissy Mar 27 '18 at 08:44
  • How about this problem? – Alexis Villar Mar 27 '18 at 08:46
  • Fatal error: Call to undefined function session_register() in C:\wamp64\www\alexis\page-login.php on line 20 – Alexis Villar Mar 27 '18 at 08:46
  • `session_register()` is deprecated remove it and use `$_SESSION[]` instead! Also, updated the answer, please accept if it solved your previous issue. – the.salman.a Mar 27 '18 at 08:52
  • 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 Apr 10 '20 at 12:23

2 Answers2

0

mysqli_num_rows($result) contains an argument $result which Specifies a result set identifier returned by mysqli_query().

Here's your modified code:

<?php
    include("config.php");
    session_start();

    if($_SERVER["REQUEST_METHOD"] == "POST") {
        // username and password sent from form 

        $myusername = mysqli_real_escape_string($db,$_POST['username']);
        $mypassword = mysqli_real_escape_string($db,$_POST['password']); 

        $sql = "SELECT user_id FROM user WHERE username = $myusername and password = $mypassword";
        $result = mysqli_query($db,$sql);
        $rows = mysqli_fetch_array($result);
        $active = $rows["user_id"];

        $count = mysqli_num_rows($result);

        // If result matched $myusername and $mypassword, table row must be 1 row

        if($count == 1) {
            //session_register("myusername");
            $_SESSION['login_user'] = $myusername;

            header("location: index.php");
        }else {
            $error = "Your Account Credentials is Invalid, Please Try Again";
      }
   }
?>

For more on this please visit mysqli_num_rows($result)

the.salman.a
  • 945
  • 8
  • 29
-1

replace

$count = mysqli_num_rows($active);

by

$count = mysqli_num_rows($result);
Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40