Jump to content

echo doesn't output-- help, please.


SpartanTacoDuckie

Recommended Posts

Hello, there. I've been messing around with this stuff recently and ran across this error when trying to output a message when not all login fields are filled in, as well as an error when trying to output a successful login message..

 

These parts of the code are:

 

 

Check all fields filled:

 


//get the form data
$myusername = ($_POST['username']);
$mypassword = ($_POST['password']);

//check if all fields are filled in
if ( (!$myusername) || (!mypassword) ) ) {
echo "Please fill in all fields";
exit;
}
 

Check if user exists & log in:

 


//check if user exists
if ($account ==1) {
$_SESSION["username"] = $myusername;
$_SESSION["password"] = $mypassword;
$_SESSION["userrecord"] = mysql_fetch_assoc($result);

echo "You have been logged in successfully. Please click <a href=account.php>here</a> to continue.";
}

As you can probably tell from the title, the echo does not output onto the webpage.

 

 

Any help is very much appreciated. Thanks.

Edited by SpartanTacoDuckie
Link to comment
Share on other sites

For the first part of code on the top,

 

it should be:

//get the form data
$myusername = ($_POST['username']);
$mypassword = ($_POST['password']);

//check if all fields are filled in
if ( ($myusername == "") || (mypassword == "") ) ) {
echo "Please fill in all fields";
exit;
}

 

 

 

 

---------------

for the second part of code

it should be :

 

//check if user exists

 if (mysqli_num_rows($result) >0) {
            $row = mysqli_fetch_array($result);
            $_SESSION['username'] = $row['username'];
            $_SESSION['password'] = $row['password'];
 

echo "You have been logged in successfully. Please click <a href=account.php>here</a> to continue.";
}

Link to comment
Share on other sites

 

Hmm.... doesn't seem to work correctly either. Still no output.

 

Also, between those two codes I posted I have:

//check the form in database
$sql = "SELECT = FROM {$usertable} WHERE username = '{$myusername}' AND password = '{$mypassword}'";
$result = mysql_query($sql);

$account = mysql_num_rows($result)

This Query is wrong.

Link to comment
Share on other sites

Here's my whole code:

<?php

session_start();

?>


<html lang="en">
<html>
<head>
<title>Login page</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="./CSSPAGE.css"
</head>
<body>
<h2 style="text-align:center; color:red">Please log in using the form below.</h2>
<br />
<br />
<center>
<form name="Login" action="login.php" method="post">
<br />
<br />
Username: <input type="text" placeholder="Enter your username" name="username">
<br />
<br />
Password: <input type="password" placeholder="Enter your password" name="password">
<br />
<br />
<input type="submit" value="Log in">
</center>
</form>

<?php

include 'config.php';

mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

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

    //get the form data
    $myusername = ($_POST['username']);
    $mypassword = ($_POST['password']);

    //check if all fields are filled in
    if ( (!$myusername == "") || (!$mypassword == "") ) {
        echo "Please fill in all fields";
        exit;
    }

    //check the form in database
    $sql = "SELECT = FROM {$usertable} WHERE username = '{$myusername}' AND password = '{$mypassword}'";
    $result = mysql_query($sql);

    $account = mysql_num_rows($result);

    //check if user exists
    if ($account == 1) {
        $_SESSION["username"] = $myusername;
        $_SESSION["password"] = $mypassword;
        $_SESSION["userrecord"] = mysql_fetch_assoc($result);

        echo "You have been logged in successfully. Please click <a href=account.php>here</a> to continue.";
    }
}

?>

</body>
</html>

Link to comment
Share on other sites

The query should change to $sql = "SELECT id,username,password FROM usertable WHERE username = '$myusername' AND password = '$mypassword'";

 

Assuming the table in your database is called usertable.

please, it shouldn't be with quotes or brackets.

 

standard format in a query, e.g > "SELECT * FROM table WHERE id = '$id';

 

on the example above, table represents table name without quotes or brackets.

id represent the variable in the database

$id represent the variable assigned in the php code.

 

 

 

---

besides that, if you gonna do some security checks on your form

 

it should be:

$myusername = mysql_real_escape_string($_POST['username']);

$mypassword = mysql_real_escape_string($_POST['password']);

 

 

and

 

your register page query should do a SHA1($mypassword) instead of just passing the text value of the password.

followed to this login page, The query can changed to `password`='".sha1($mypassword)."'

Edited by KaiSheng
Link to comment
Share on other sites

I'm not worried about security on this, really. It's just a test. :)

 

As for the query, I changed it to what you suggested, with no success.

 

However, my database is phpMyAdmin using XAMPP (LAMP since it's a Linux (at the moment)) so it should work fine, bit it doesn't seem to be.

Edited by SpartanTacoDuckie
Link to comment
Share on other sites

thanks for showing that config.php

 

it's of great help.

 

1) is your database name really named login-register?

if yes, continue to question 2.

 

2) remove $usertable ="users";

and it should work according to the amendments of the codes in my previous post.

 

----

Please take note that this config.php

supposed to be a file that is used for several pages.

it is best to keep only functions and connection to database instead of setting a table there.

If you require information from other tables, errors will occur very easily.

Edited by KaiSheng
Link to comment
Share on other sites

Yes, my login/register testing environment's database is called login-register.

 

As for removing $usertable = "users";   ....

 

The table in my database which holds the id, username, and password is called "users". Removing it didn't end up making it work.

 

Maybe an error with my query?

$sql = "SELECT * FROM usertable WHERE username = '$myusername' AND password = '$mypassword'";
$result = mysql_query($sql);

$account = mysql_num_rows($result);
Link to comment
Share on other sites

Config:

<?php

$user = "root";
$host = "localhost";
$password = "";
$database = "login-register";

?>

Login:

<?php

session_start();

?>


<html lang="en">
<html>
<head>
<title>Login page</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="./CSSPAGE.css"
</head>
<body>
<h2 style="text-align:center; color:red">Please log in using the form below.</h2>
<br />
<br />
<center>
<form name="Login" action="login.php" method="post">
<br />
<br />
Username: <input type="text" placeholder="Enter your username" name="username">
<br />
<br />
Password: <input type="password" placeholder="Enter your password" name="password">
<br />
<br />
<input type="submit" value="Log in">
</center>
</form>

<?php

include 'config.php';

mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

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

    //get the form data
    $myusername = ($_POST['username']);
    $mypassword = ($_POST['password']);

    //check if all fields are filled in
    if ( (!$myusername == "") || (!$mypassword == "") ) {
        echo "Please fill in all fields";
        exit;
    }

    //check the form in database
    $sql = "SELECT * FROM users WHERE username = '$myusername' AND password = '$mypassword'";
    $result = mysql_query($sql);

    $account = mysql_num_rows($result);

    //check if user exists
    if ($account == 1) {
        $_SESSION["username"] = $myusername;
        $_SESSION["password"] = $mypassword;
        $_SESSION["userrecord"] = mysql_fetch_assoc($result);

        echo "You have been logged in successfully. Please click <a href=account.php>here</a> to continue.";
    }
}

?>

</body>
</html>

>.>

Link to comment
Share on other sites


<?php

session_start();

?>


<html lang="en">
<html>
<head>
<title>Login page</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="./CSSPAGE.css"
</head>
<body>
<h2 style="text-align:center; color:red">Please log in using the form below.</h2>
<br />
<br />
<center>
<form name="Login" action="login.php" method="post">
<br />
<br />
Username: <input type="text" placeholder="Enter your username" name="username">
<br />
<br />
Password: <input type="password" placeholder="Enter your password" name="password">
<br />
<br />
<input type="submit" value="Log in">
</center>
</form>

<?php

include 'config.php';



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

//get the form data
$myusername = $_POST['username'];
$mypassword = $_POST['password'];

//check if all fields are filled in
if ( ($myusername == "") || ($mypassword == "") ) {
echo "Please fill in all fields";
exit;
}

//check the form in database
$link = mysqli_connect($host, $username, $password, $database);
$sql = "SELECT * FROM users WHERE username = '$myusername' AND password = '$mypassword'";
$result = mysqli_query($link, $sql) or die(mysqli_error($link));
//check if user exists
if (mysqli_num_rows($result) > 0) {
$row = mysql_fetch_array($result);
$_SESSION["username"] = $row['username'];
$_SESSION["password"] = $row['password'];

echo "You have been logged in successfully. Please click <a href=account.php>here</a> to continue.";
}
}

?>

</body>
</html>
Edited by KaiSheng
Link to comment
Share on other sites

Let me change your style.

Since its only for testing.

do 3 pages.

1) login.php (for forms to be filled in)

2) doLogin.php (for background php to be processed)

3) config.php (for establishing database connection)

 

This is config.php

<?php

        $HOST = "localhost";
        $USERNAME = "root";
        $PASSWORD = "";
        $DB = "login-register";
        
        $link = mysqli_connect($HOST, $USERNAME, $PASSWORD, $DB);
?>

-------

This is login.php

<?php
include 'config.php';
// check if user is authenticated
session_start();
if (isset($_SESSION['user_id'])) {
echo "You are already logged in.<br/>.";
     }

     else
     {
      
     }
?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login Page</title>
    </head>
    <body bgcolor ="lightblue">
         <form method="post" action="doLogin.php">
             <table>
                    <tr>
                        <td><label for="username">Username:</label></td>
                        <td><input type="text" id="username" name="username" required/></td>
                    </tr>
                    <tr>
                        <td><label for="password">Password:</label></td>
                        <td><input type="password" id="password" name="password" required/></td>
                    </tr>
                    
             </table>
             <input type="submit" value="Login" name="submit"/>
        </form> 
        
    </body>
</html>

----

This is doLogin.php

<?php
session_start();
include ('config.php');
if (!isset($_SESSION['user_id'])) {
    if (isset($_POST['username'])) {
        //retrieve form data
        $username = $_POST['username'];
        $password = $_POST['password'];
        $link = mysqli_connect($HOST, $USERNAME, $PASSWORD, $DB);

        //match the username and password entered with database record
        $query = ("SELECT * FROM `users` WHERE `username`='$username' AND `password`='$password'");
        $result = mysqli_query($link, $query) or die(mysqli_error($link));

        //if record is found, store id and username into session
        if (mysqli_num_rows($result) >0) {
            $row = mysqli_fetch_array($result);
            $_SESSION['user_id'] = $row['id'];
            $_SESSION['username'] = $row['username'];

            $msg = '<p><i>Hello, ' . $row['username'] . '!<br />';
            $msg .= 'You are logged in.<br /><a href="home.php">Home</a></p>';
        } else { //record not found
            $msg = '<p class="error">Sorry, you must enter a valid username and password to log in.<a href="login.php"> Back</a></p>';
            
        }
    }
} else {
    $msg = 'You are already logged in.<br /><a href="home.php">Home</a></p>';
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" type="text/css" href="style.css" />
        <title>Login page</title>
        <link rel="stylesheet" type="text/css" href="style.css" />

    <header><b></b></header>

</head>
<body bgcolor ="lightblue">

    <?php
    echo $msg;
    ?>
    <?php
    header("refresh:4;url=index.php");
    ?>

</body>
</html>

---

Change the query according to your database table name. 

Edited by KaiSheng
Link to comment
Share on other sites

Now it has errors:

 

Notice: Undefined variable: host in /opt/lampp/htdocs/files/Login-register/login.php on like 37

 

Notice: Undefined variable: user in /opt/lampp/htdocs/files/Login-register/login.php on like 37

 

Notice: Undefined variable: password in /opt/lampp/htdocs/files/Login-register/login.php on like 37

 

Notice: Undefined variable: database in /opt/lampp/htdocs/files/Login-register/login.php on like 38

No database selected

 

Now here's what I don't understand about this:

 

Line 37 is: </head>  so it's just closing off the heading... what..?

 

Line 38 is blank; absolutely nothing there.

 

So where is it missing the variables at?

 

 

Edit: I'm off for today... getting late. I'll check back for any responses tomorrow, try them out, and get back on it.

 

By the way, thanks for the help you've provided so far. Very quick to respond, helpful, and also patient. Thanks. ;3

Edited by SpartanTacoDuckie
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.