0

I have this piece of code that I wrote

$stmt = $mysqli->prepare("INSERT INTO `verifyAccounts` (`CUsername`, `CPassword`) VALUES (?,?)");
$stmt->bind_param("ss", $CUsername, $CPassword);
$stmt->execute();
$stmt = $mysqli->prepare("SELECT `Started` FROM `verifyAccounts` WHERE `CUsername` = ?");
do {
    $stmt->bind_param("s", $CUsername);
    $stmt->execute();
    $stmt->bind_result($Startedvalue);
    echo $Startedvalue;
    sleep(2);
} while ($Startedvalue < 2);

if ($Startedvalue == 2) {
    echo "Correct";
} elseif ($Startedvalue == 3) {
    echo "Incorrect";
}
$stmt = $mysqli->prepare("DELETE FROM `verifyAccounts` WHERE `CUsername` = ?");
$stmt->bind_param("s", $CUsername);
$stmt->execute();

For some reason, this doesn't seem to be working, I never get an echo back saying Correct or Incorrect and the account isn't deleted either. It seems like it's stuck in the Do While loop, but $Startedvalue is never echoed either.

Here is the Ajax I have

 function verifyAccount(f) {
            f.preventDefault();
            var CUsername = $('#CUsername').val();
            var CPassword = $('#CPassword').val();
            $.ajax({
                type: 'POST',
                data: {
                    CUsername: CUsername,
                    CPassword: CPassword
                },
                url: 'verifyAccount.php',
                success: function(data) {
                    console.log(data);
                    if (data == "Correct") {
                        $("#verifyPasswordText").text('Valid Login');
                    } else if (data == "Incorrect") {
                        $("#verifyPasswordText").text('Invalid Login');
                        $("#verifyPasswordOKC").prop("disabled", false);
                    } else if (data == "CUsername") {
                        $("#verifyPasswordText").text('Fill out your OKC Username');
                        $("#verifyPasswordOKC").prop("disabled", false);
                    } else if (data == "CPassword") {
                        $("#verifyPasswordText").text('Fill out your OKC Password');
                        $("#verifyPasswordOKC").prop("disabled", false);
                    }           
                },
                error: function(xhr, err) {
                    console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                    console.log("responseText: " + xhr.responseText);
                }
            });
        }

1 Answers1

-1

Honnestly dont know if it is a good idea to process login this way but

If nothing modifies 'Started', nothing in the code you presented would, then you will be stuck in the do..while

As for the absence of echoes

  1. Makes sure the content type is set before outputting header( 'Content-type: text/html; charset=utf-8' );
  2. flush after each echo flush(); ob_flush();
user3802077
  • 849
  • 9
  • 22
  • Right, so while the happens, there's a script going on in the background and I can see inside phpMyAdmin that the `Started` changes into either a 2 or a 3. – PhpisHardForMe Apr 11 '16 at 05:16
  • What happens if you move $stmt = $mysqli->prepare("SELECT `Started` FROM `verifyAccounts` WHERE `CUsername` = ?"); inside the do..while? – user3802077 Apr 11 '16 at 05:18
  • Missed this, but you are also missing a $stmt->fetch() I believe.. after the bind_result – user3802077 Apr 11 '16 at 05:21
  • I put a `$stmt->fetch()` after the bind_result and I'm getting the correct thing back, but now the error I'm getting is Fatal error: Call to a member function bind_param() on boolean in /home/d/sadasd.com/verifyAccount.php on line 109
    and line 109 is `$stmt->bind_param("s", $CUsername);`
    – PhpisHardForMe Apr 11 '16 at 08:46
  • This is probably because stmt is false and there is probably an error reported. http://stackoverflow.com/a/27394772/3802077 – user3802077 Apr 11 '16 at 12:22