PHP Login does work on localhost, but not on another?

  authentication, mysql, mysqli, php, prepared-statement

My PHP login script works fine on localhost (xampp), but it does not work when I upload it on another server. I added some echos and concluded that it stops working after executing the prepared statement.
When I use mysqli::$affected_rows it shows me "-1", so there is likely problem with the database.
When I login without using prepared statements, it suddenly works.
Is there a reason, why getting data from the database using prepared statements works on some servers, but not on others, or is there something wrong with my script? I am a programming beginner, so any advice idea would be appreciated. I donĀ“t know what to try anymore.

Here is my login script:

<?php

if (isset($_POST["submit"])) {

  $username = $_POST["uid"];
  $pwd = $_POST["pwd"];

  require_once "mini.php";



// Check for empty input login
function emptyInputLogin($username, $pwd) {
$result;
if (empty($username) || empty($pwd)) {
  $result = true;
}
else {
  $result = false;
}
return $result;
}

// Log user into website
function loginUser($sqli, $username, $pwd) 
{
  $sql = "SELECT * FROM users WHERE usersUid = ? OR usersEmail = ?";

  $stmt = DB::$sqli->prepare($sql);

  $stmt->bind_param('ss', $username, $username);

  $stmt->execute();

  $result = $stmt->get_result();
  $row = $result->fetch_assoc();  

  $pwdHashed = $row["usersPwd"];
  $checkPwd = password_verify($pwd, $pwdHashed);

  if ($checkPwd === false) {

    header("location: login.php?error=wronglogin");
     exit();
   }

   elseif ($checkPwd === true) {
    session_start();
    $_SESSION["userid"] = $row["usersId"];
    $_SESSION["useruid"] = $row["usersUid"];
    header("location: submitpost.php?error=none");
    exit();
  }
}

  
  // User left inputs empty
  if (emptyInputLogin($username, $pwd) === true) {
    header("location: login.php?error=emptyinput");
   exit();
   }

  loginUser(DB::$sqli, $username, $pwd);


} else {
    header("location: login.php");

    exit();
}

Here is the Login form:


    <h2>Log in</h2>
    <div class="login-form">
     <form action="login.inc.php" method="post">
        <input type="text" name="uid" class="fullname" placeholder="Username/Email..."><br>
        <input type="password" name="pwd" class="pwd" placeholder="Password..."><br>
        <button type="submit" name="submit" id="registerbutton">Log in</button>
        <a href="signup.php">Sign up here!</a>
     </form>
    </div>
    <?php

if (isset($_GET["error"])) {
    if ($_GET["error"] == "emptyinput") {
        echo "<p>Fill in all fields!</p>";
    }
    else if ($_GET["error"] == "wronglogin") {
        echo "<p>Incorrect Login information!</p>";
    }
}
?>

The database connection:

<?php

  //$host = "UNI";
  $host = "local";
  

  if ($host == "UNI")
  {
      echo "uni<br>";
      require "/web/Verwaltung/sicher/DBi_uni_globe.php";
  }
  
  else   
  {
      //echo "local<br>";
      class DB {
        public static $sqli;
        
        }  
        DB::$sqli = new mysqli ("localhost","root", "", "globe");
        DB::$sqli->query("SET NAMES 'utf8'");      
  }

Source: Ask PHP

LEAVE A COMMENT