0

I am trying to create a php login page, and capture the username and password input into html text boxes. But my issue is that my echo statement always returns null for the variables.

Why are these variables not beting set?

<?php
  include("config.php");
  session_start();

  if($_SERVER["REQUEST_METHOD"] == "POST") {
      $myusername = mysqli_real_escape_string($db,$_POST['username']);
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 
      echo $myusername;
      echo $mypassword;
  }
?>
<html>   
<head>
  <title>Login Page</title>      
  <style type = "text/css">
     body {
        font-family:Arial, Helvetica, sans-serif;
        font-size:14px;
     }
     label {
        font-weight:bold;
        width:100px;
        font-size:14px;
     }
     .box {
        border:#666666 solid 1px;
     }
  </style>      
   </head>   
   <body bgcolor = "#FFFFFF">             
           <form action = "" method = "post">
              <label>UserName:</label><input type = "text" name = "username" class = "box"/><br /><br />
              <label>Passwore:</label><input type = "password" name = "password" class = "box" /><br/><br />
              <input type = "submit" value = " Submit "/><br />
           </form>
</body>
</html>

EDIT
This is my config.php where I am using the mysqli_connect()

<?php
 define('DB_SERVER', 'localhost:3036');
 define('DB_USERNAME', 'user');
 define('DB_PASSWORD', 'pass');
 define('DB_DATABASE', 'db');
 $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>
13
  • Are you sure your action param on the form has to be blank? Commented Sep 20, 2018 at 19:38
  • 3
    @SalmonKiller Blank action means to use the current URL as the action. Commented Sep 20, 2018 at 19:39
  • 1
    What does var_dump($_POST) show? Commented Sep 20, 2018 at 19:39
  • 1
    If you haven't yet connected to the database, i.e. through mysqli_connect, then the mysqli_real_escape_string function won't work. This is your problem. Commented Sep 20, 2018 at 19:42
  • 1
    You have an incorrect quote on the second define line. You have a curly quote instead of a straight quote. Commented Sep 20, 2018 at 19:45

1 Answer 1

1

You can't specify a port number in the server name like that. It works for an external hostname, but it doesn't work when using localhost, according to this comment in the documentation. You need to use the optional port argument.

 define('DB_SERVER', 'localhost');
 define('DB_USERNAME', 'user');
 define('DB_PASSWORD', 'pass');
 define('DB_DATABASE', 'db');
 define('DB_PORT', 3036);
 $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE,DB_PORT);
Sign up to request clarification or add additional context in comments.

1 Comment

That was it - adding the port to it's own define has the code functioning as it should

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.