0

The problem is that my register and login server-side validation doesn't work. The client-side validation still works to check if fields are filled out the way I want them to be.

The submit button also use to work, but no longer does. Instead you click it and there is an error on the server side validation that causes it not to submit.

Here is My Login Form HTML:

<div id="content">
    <form id="login-form" name="login-form" class="content-padding" method="post" autocomplete="off" action="index.php">
        <div class="login-container">
            <div>
                <label for="l_username">Username</label>
                <input type="text" placeholder="Enter Username" name="l_username" id="l_username">
            </div>

            <div>
                <label for="l_password">Password</label>
                <input type="password" placeholder="Enter Password" name="l_password" id="l_password">
            </div>

            <button id="l_submit" name="l_submit" type="submit">Login</button>
        </div>

        <div class="cancel-container">
            <button type="reset" class="clear-btn">Clear</button>
            <button type="button" class="register-btn" onclick="location.href='register.php'">Register</button>
        </div>
    </form>
</div>

Here is My Login Form JS:

$().ready(function() {
    $("#login-form").validate({
        rules: {
            l_username: {
                required: true,
            },
            l_password: {
                required: true,
            }
        },
        messages: {
            l_username: {
                required: "Please Enter a Username"
            },
            l_password: {
                required: "Please Enter a Password"
            }
        },
        submitHandler: function(form){
            $.ajax({
                url: 'includes/login.php',
                type: 'post',
                data: $('#login-form').serialize(),
                success: function(response){
                    if(response=="ok"){
                        $("#l_submit").html('<img src="images/ajax-loader.gif" />   Signing In ...');
                        setTimeout('window.location.href = "index.php";', 3000);
                        } else {
                        $("#loginError").fadeIn(1000, function(){
                            $("#loginError").html('<div class="alert alert-danger"><i class="fas fa-info-circle"></i>   '+response+' !</div>');
                            $("#l_submit").html('<i class="fas fa-sign-in-alt"></i>   Login');
                        });
                    }
            }
        });
        return false;
    }
    });
});

Here is My Login Form PHP:

<?php
    include 'dbconfig.php';

    $loginUsername = $loginPassword = "";

    if ($_SERVER['REQUEST_METHOD'] == "POST"){
        $loginUsername = test_input($_POST['l_username']);
        $loginPassword = test_input($_POST['l_password']);
    }

    function test_input($data){
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    $loginHashed = hash('sha512', $loginPassword);  //CHAR 128 - length field

    $stmt = $conn->prepare("SELECT * FROM users WHERE username = ? && password = ?");
    $stmt->bind_param("ss", $loginUsername, $loginHashed);
    $stmt->execute();

    $result = $stmt->get_result();
    if($result->num_rows == 1){
        $_SESSION['currentuser']=$loginUsername;
        echo 'ok';
    }else{
        echo "Invalid Username and/or Password";
    }

        $stmt->close();
        $conn->close();

?>

It connects to the database fine as i can tell from the dbconfig.php file. I'm not exactly sure where it goes wrong. The only error I ever saw was something like:

PHP Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::get_result()

However, I don't know why this would occur as I can't seem to get it to work on both my web server and a fresh most up-to-date version of MAMP.

I have tried my friend's website code as well as my teacher's working website code on my web server/MAMP and both seem to have the same issue where when you click the submit button on both the login and register forms it sits there doesn't do anything and doesn't update the database with a new user.

Carl Binalla
  • 5,393
  • 5
  • 27
  • 46
Pixels_
  • 13
  • 3
  • I believe you'll find the answer here: https://stackoverflow.com/questions/8321096/call-to-undefined-method-mysqli-stmtget-result – sam28 May 02 '18 at 02:56
  • I changed the `get_result()` to `fetch()` in both the register and login forms and it seemed to work for the register form. It submits properly and everything. However, anytime I try to login it still gives me the error saying: Invalid Username and/or Password. – Pixels_ May 02 '18 at 03:48
  • I don't think you can use num_rows after fetch since it gathers one row from the results set. Try using store_results instead of fetch. – sam28 May 02 '18 at 14:26
  • $stmt->store_results; $result = $stmt->num_rows; And echo $result to check if your query in fact returns something – sam28 May 02 '18 at 14:36

0 Answers0