php代码在浏览器中显示

时间:2012-01-23 22:21:55

标签: php mysql web webserver xampp

我已经为用户登录编写了php脚本,但是没有显示结果,而是显示整个脚本。我已经将.php文件链接作为登录表单的操作。

我正在使用xampp与php和mysql运行我还需要其他什么吗?

代码是:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />

  <!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame 
       Remove this if you use the .htaccess -->
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

  <title>Ch</title>
  <meta name="description" content="education,India,College search in india,score evaluator" />
  <meta name="author" content="RAJATEJAS" />
  <meta name="viewport" content="width=device-width; initial-scale=1.0" />

  <!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
  <link rel="shortcut icon" href="/favicon.ico" />
  <link rel="apple-touch-icon" href="/apple-touch-icon.png" />

  <style type = "text/css">
    user_login , input
    {
        display = inline;   
    }   
  </style>

</head>

<body>
  <div>
    <header>
      <h1>Ch</h1>
    </header>

    <nav>
      <p><a href="/">Home</a></p>
      <p><a href="/contact">Contact</a></p>
    </nav>

    <div class = "user_login_form">
        <form action = "chalo_login.php" method="post">
            <label>Username:</label><input id = "username" type = "text" name = "username" autofocus placeholder="Enter Username"/><br />
            <label>Password:</label><input id = "password" type = "password" name = "password" placeholder="Enter Password"/><br />
            <input name = "submit" type = "submit" value = "Login" />
        </form>   
    </div>


    <footer>
     <p>&copy; Copyright  by RAJATEJAS</p>
    </footer>
  </div>
</body>
</html>

<?php
    session_start();
    $_POST['username'];
    $_POST['password'];

    if ($username&&$password)
    {
        $connect = mysql_connect("localhost","root","");
        mysql_select_db("phplogin") or die("could not find database");
        $query = mysql_query(SELECT * FROM users WHERE username = "$username");
        $numrow = mysql_num_rows($query);
        if ($numrows !=0)
        {
            while ($row = mysql_fetch_assoc($query))
            {
                $dbusername = $row["username"];
                $dbpassword = $row["password"];
            }
            if($username == $dbusername && $password == $dbpassword)
            {
                echo "You are logged in! ;
                <a href ="member.php">Click here</a>";
                $_SESSION["username"] = $dbusername;

            }
            else
            {
                echo "Incorrect password";
            }
        }else{die("Account does not exists");}
    } 
    else 
    {
    die ("Please enter details");   
    }

?>

2 个答案:

答案 0 :(得分:1)

您的代码中有一些错误,但我不知道它们是否是造成问题的原因。

首先在您的CSS user_login中应#user_login选择ID为“user_login”的元素。然后,display = inline;应为display: inline;

在你的PHP ...

$_POST['username'];
$_POST['password'];

......什么也没做。我想你应该有

$username = $_POST['username'];
$password = $_POST['password'];

正如Ethan在上面的评论中提到的,你的引号搞砸了:

echo "You are logged in! ;
<a href ="member.php">Click here</a>";

......应该是:

echo "You are logged in! <a href =\"member.php\">Click here</a>";

(使用反斜杠在引号内转义引号)。

修复这些错误并查看它是否有效......

另外:正如LeleDumbo所说,确保页面是通过Apache加载而不是作为文件打开。该网址应以127.0.0.1localhost开头。如果没有,只需将127.0.0.1放入地址栏,然后在显示的列表中浏览到您的文件。


另一个错误 - 您的SELECT语句需要是一个字符串:

$query = mysql_query("SELECT * FROM users WHERE username = '$username'");

答案 1 :(得分:0)

未分配变量$ username和$ password

if(isset($_POST['username'])
{
$username=$_POST['username'];
}
if(isset($_POST['password'])
{
$password=$_POST['password'];
}

并在上面的评论中选择正确的数据库,你说你的数据库名称也是“用户”,但你选择了“phplogin”