我已经制作了注册码,但我不确定如何使用它来登录

时间:2014-10-21 04:19:21

标签: php mysql

基本上我需要的是一个php / mysql代码来检查列(用户名列)是否与另一列(密码列)位于同一行但是我不确定如何这样做,我&# 39;使用MySQL时相当新。这是我的注册码:

<?php

define('DB_HOST', 'localhost');
define('DB_NAME', 'credentials');
define('DB_USER', 'julianbuscema');
define('DB_PASSWORD', '');
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db = mysql_select_db(DB_NAME, $con) or die("Failed to connect to MySQL: " . mysql_error());

function NewUser()
{
    $fullname = $_POST['name'];
    $userName = $_POST['user'];
    $email = $_POST['email'];
    $password = $_POST['pass'];
    $query = "INSERT INTO websiteusers (fullname,userName,email,pass) VALUES ('$fullname','$userName','$email','$password')";
    $data = mysql_query($query) or die(mysql_error());
    if ($data){
        echo "YOUR REGISTRATION IS COMPLETED...";
    }
}

function SignUp()
{
   if (!empty($_POST['user']))
   { 
       $query = mysql_query("SELECT * FROM websiteusers WHERE userName = '$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error()); 
       if(!$row = mysql_fetch_array($query) or die(mysql_error())) { 
          newuser(); 
       } else { 
          echo "SORRY...YOU ARE ALREADY REGISTERED USER..."; 
       } 
   } 
} 

if(isset($_POST['submit'])) { 
   SignUp();
}

?>

1 个答案:

答案 0 :(得分:0)

使用此基本登录页面:

的index.php

<?php
include('login.php');
?>

<form action="index.php" method="POST" id="login-form" >

  <div class="xxxx"><?php echo $error; ?></div>      
  <div class="xxxxx">
        <input type="text" name="username" placeholder="Username" required>
        <input type="password" name="password" placeholder="Password" required>
        <input type="submit" value="Log in" name="login">
    </div>
</form>

的login.php

<?php

mysql_connect("hostname","db_username","db_password");
mysql_select_db("dbname");

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

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

    $check_user = "select * from table where username='$username' AND password='$password'";

    $run = mysql_query($check_user);

    if(mysql_num_rows($run)>0){

    header("location: other_page.php"); 
    }
    else {
    $error='Username or password is incorrect!';
    }
}

?>