php - PHP 和 MySQL 中的登录页面

标签 php mysql cloud9-ide

我正在使用 cloud9 服务器,并将我的数据库连接到该服务器。

我有一些用于简单登录的 PHP 代码,但它不起作用。

与数据库的连接已打开,但始终显示消息

"username and pas is not correct" (getting result==0)

这是我的代码:

    <?php


      ob_start();
      session_start();



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

//MySQL Database Connect
 $conn = mysql_connect(getenv("IP"),  getenv("C9_USER"), "");

if (!$conn) {
die("Could not connect: " . mysql_error());
}

$db_selected = mysql_select_db('homework5', $conn);

 if (!$db_selected) {
 die ("Can't use internet_database : " . mysql_error());
  }


 $username = mysql_real_escape_string($username);
$query = "SELECT *
FROM users
 WHERE (userName = '$userName') and (password = '$password') ;";

 $result = mysql_query($query);



     if (mysql_num_rows($result) == 0) // User not found. So, redirect to login_form again.
    { ?>
 <SCRIPT language=JavaScript>
  alert("The Password or The UserName is not Correct");
  </Script>
   <?php
  header("Location: login1.php?eror=1"); die();
  }


     $userData = mysql_fetch_array($result, MYSQL_ASSOC);
// store the needed session data for the home page session
   session_regenerate_id();
   $_SESSION['userName'] = $userData['userName'];
   $_SESSION['Level']= $userData['Level'];
   //End the current session and store session data.
   // header() is used to send a raw HTTP header to open execute home.php

   session_write_close();

    switch($userData['Level']){

     case 1  :    header("Location: maneger.php"); die();   break ;
     case 2 :    header("Location: intredection.php"); die();   break ; 

     default: header("Location: login1.php?eror=2"); die();  break;
    }

最佳答案

这是您选择的查询

$query = "SELECT * FROM users
WHERE (userName = '$userName') and (password = '$password') ;";

上面的选择查询应该是这样的

$query = "SELECT * FROM users
WHERE (userName = '$username') and (password = '$password')";

您将用户名设置为 $username。但是在选择查询中您使用 $userName 和 extra ;还有。

关于php - PHP 和 MySQL 中的登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23849549/

相关文章:

php - 当 php mysql_query 在 phpmyadmin 中工作时,某些列的空白结果

sql - 选择从查询中检索列名的列

ruby-on-rails - 让 postgresql 在 cloud9 上工作

postgresql - PG::CONNECTION BAD 连接到 pgAdmin III cloud9 IDE

php - mysql,从多个表中选择数据,表中没有公共(public)字段

php - CodeIgniter - 将变量传递给 CSS

php - 如何防止 PHP 中的 SQL 注入(inject)?

php - 为数据库中的每条记录创建 WAV 文件

php - 如何将 mysql 数据库从 Mamp 推送到我的域?

python - 在 Cloud 9 Ide 上使用 matplotlib