astral astral - 4 months ago 7
PHP Question

PHP - Variable not echoing

<?php
$output = NULL;
$ip = $_SERVER['REMOTE_ADDR'];

if (isset($POST['submit'])) {
$username = $mysqli->real_escape_string($_post['username']);
$password = $mysqli->real_escape_string($_post['password']);
$rpassword = $mysqli->real_escape_string($_post['rpassword']);
$email = $mysqli->real_escape_string($_post['email']);

$query = $mysqli->query("SELECT * FROM users WHERE username = '$username'");
if (empty($username) OR empty($password) OR empty($email) or empty($rpassword)) {
$output = "Please fill in all fields!";
} elseif ($query->num_rows != 0) {
$output = "That username is already taken!";
} elseif ($rpassword != $password) {
$output = "Password does not match!";
}
}
?>


Later on in the script, I use this:

<?php
echo $output;
?>


It does not echo, and yes, I have added the mysqli query, but I removed it for the safety of the database. You can also see that it does not echo at the website:
vobern.com

Answer

PHP is a case-sensitive Language. There is a difference between $_POST AND $_post. You may also want to take that into consideration. Now why don't you try doing it like below?

<?php
    $output         = NULL;
    $ip             = $_SERVER['REMOTE_ADDR'];

    if(isset($_POST['submit'])){
        // FOR THE VARIABLES BELOW, THERE IS A DIFFERENCE BETWEEN
        // $_POST AND $_post (AS YOU WROTE).... 
        $username   = htmlspecialchars(trim($_POST['username']));
        $password   = htmlspecialchars(trim($_POST['password']));
        $rpassword  = htmlspecialchars(trim($_POST['rpassword']));
        $email      = htmlspecialchars(trim($_POST['email']));

        $query      = $mysqli->query("SELECT * FROM users WHERE username = '$username'");

        if (empty($username) OR empty($password) OR empty($email) or empty($rpassword)){
            $output = "Please fill in all fields!";
        }elseif($query->num_rows != 0){
            $output = "That username is already taken!";
        }elseif ($rpassword != $password){
            $output = "Password does not match!";
        }
    }else{
        // IF THIS POINT IS REACHED, THEN EVERYTHING SHOULD BE OK
        $output     = "Login Data is Correct";
    }

    var_dump($output);
?>