我有两个在一个div中指定的表单。一个用于登录,另一个用于创建帐户。登录表单运行良好,但即使要调用create-account.php文件,创建帐户按钮也会出现登录错误。
下面我们有代码。
登录创建/帐户表单
<?php
include('login.php'); // Includes Login Script
if(isset($_SESSION['login_user'])){
header("location: index.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="main">
<div id="login">
<h2>Login Form</h2>
<form action="" method="post">
<label>UserName :</label>
<input id="name" name="username" placeholder="username" type="text">
<label>Password :</label>
<input id="password" name="password" placeholder="**********" type="password">
<input name="submit" type="submit" value=" Login ">
<span><?php echo $error; ?></span>
</form>
<form action="create-account.php" method="post" >
<input name="submit" type="submit" value=" Create User ">
</form>
</div>
</div>
</body>
</html>
login.php代码如下所示
<?php
include('config2.php');
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
}
else
{
// Define $username and $password
$username=$_POST['username'];
$password=$_POST['password'];
// Create connection
$con = mysqli_connect($db_hostname, $db_username, $db_password, $db_database , 3306);
// Check connection
if(mysqli_connect_error()){
die("Connection failed: ".$con->connect_error);
}
// To protect MySQL injection for Security purpose
$username = stripslashes($username);
$password = stripslashes($password);
//$username = mysql_real_escape_string($username);
//$password = mysql_real_escape_string($password);
// Retrieve data from database
$sql="select * from admin where password='$password' AND user_name='$username'";
$result = mysqli_query($con,$sql);
$rows = mysqli_num_rows($result);
echo sizeof($rows);
if($rows == 1) {
$_SESSION['login_user']=$username; // Initializing Session
header("location: profile.php"); // Redirecting To Other Page
} else {
$error = "Username or Password is invalid";
}
$con->close(); // Closing Connection
}
}
?>
create-account.php文件没有代码。 任何建议。