PHP变量始终为空

时间:2018-09-20 19:36:02

标签: php

我正在尝试创建一个php登录页面,并捕获输入到html文本框中的用户名和密码。但是我的问题是我的echo语句始终为变量返回null。

为什么不设置这些变量?

<?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>

编辑
这是我使用mysqli_connect()

的config.php文件
<?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);
?>

1 个答案:

答案 0 :(得分:1)

您不能像这样在服务器名称中指定端口号。根据文档中的this comment,它适用于外部主机名,但在使用localhost时不起作用。您需要使用可选的port参数。

 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);