我目前正在我的注册页面上工作,每次我点击提交按钮时,它都不会获取我的信息并将其提交到数据库。否则,如果在某些情况下它确实需要信息,它将带我进入一个空白屏幕,当我有一个标题将我带到一个受限部分时。我的语法错了吗?
<?php
//serror_reporting(E_ALL);
//ini_set('display_errors', 1);
define("HOST", "localhost"); // The host you want to connect to.
define("USER", "root"); // The database username.
define("PASSWORD", "root"); // The database password.
define("DATABASE", "dfa24"); // The database name.
session_start();
if($_SERVER['REQUEST_METHOD'] === 'POST'){
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE)
or die("Failed to connect");
if(isset($_POST['submit2'])) {
$regFirst = mysql_real_escape_string($_POST['firstname']); // stores the name
$regLast = mysql_real_escape_string($_POST['lastname']); // the last name
$regPassword = mysql_real_escape_string($_POST['password']); // the password
$regUser = mysql_real_escape_string($_POST['username']); // and username that was created.
$sql = " SELECT loginName FROM Member
WHERE loginName = '$regUser' "; //Double checks for duplicate usernames
$result = $mysqli->query($sql) or die($mysqli->error);
if($result->num_rows > 1){ // if no duplicates, creates the username.
$_SESSION['logged']='0';
echo "That username already exists, please choose another."; //username exists, create another one.
$x = false;
} else {
$_SESSION['logged']='1';
$sql1 = "INSERT INTO Member (loginName, password, lastname, firstname)
VALUES
('$regUser','$regPassword', '$regLast', '$regFirst')";
$result1 = $mysqli->query($sql1) or die($mysqli->error);
header("Location: MemberHomeScreen.php");
exit();
}
}else{
$x = false;
$errors = array(); // all of the errors that are found below will be stored here and called later.
$isValid = filter_var($emailaddress, FILTER_VALIDATE_EMAIL); //checks for valid email
$defaults = array("firstname" => "", "emailaddress" => "", "lastname" => "", "password" => "", "username" => ""); //this array stores the information that the user enters.
$defaults2 = array("userlogin" => "", "userpassword" => "");
if(isset($_POST['submit2'])) // Checks that the submit button has been pressed.
{
$x = true;
if(!isset($_POST['firstname']) || $_POST['firstname'] === '') //Checks for the name to be filled out
{
$x = false;
array_push($errors, "** Please fill in your name **");//Pushes an error
}else {
$defaults['firstname'] = $_POST['firstname']; //stores the item, so we do not lose it if there is an error.
}
if(!isset($_POST['emailaddress']) || $_POST['emailaddress'] === '') //Checks that the email field is filled out.
{
$x = false;
array_push($errors, "** Please fill in your email address **");
} else if(!filter_var($_POST['emailaddress'], FILTER_VALIDATE_EMAIL)) //Makes certain that the email is valid. I am checking for ANY and ALL valid email addresses, we can change this with preg if need be, but this seems more applicable to me.
{
$x = false;
array_push($errors,"** Please enter a valid email **");//Pushes an error
}else {
$defaults['emailaddress'] = $_POST['emailaddress']; //stores the item, so we do not lose it if there is an error.
}
if(!isset($_POST['lastname']) || $_POST['lastname'] === '') //Checks for anything in the subject line.
{
$x = false;
array_push($errors, "** Please enter your last name **");//Pushes an error
} else {
$defaults['lastname'] = $_POST['lastname']; //stores the item, so we do not lose it if there is an error.
}
if(!isset($_POST['username']) || $_POST['username'] === '') // Checks to make sure there is a message in the message box.
{
$x = false;
array_push($errors, "** Please enter your username **");//Pushes an error
} else {
$defaults['username'] = $_POST['username']; //stores the item, so we do not lose it if there is an error.
}
/*if(!isset($_POST['password']) || $_POST['password'] === '') // Checks to make sure there is a message in the message box.
{
$x = false;
array_push($errors, "** Please enter a password **");//Pushes an error
}*/
}
}
}
?>
<html>
<head>
<title> Registration Page </title>
<link href="projects.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div class="wrapper">
<h1 id="top"> Sign In/Register </h1>
<div id="menubar">
<div id="menu">
<ul id="a_links">
<li><a href="index.html">Main Page</a></li>
<li><a href="project1.html" class="on">About Me</a></li>
<li><a href="contactform.php">Contact Me</a></li>
<li><a href="Resume.html">Resumé</a></li>
<li><a href="membership.php">Sign In</a></li>
</ul>
</div>
</div><br/>
<body>
<?php
$userregistration = array("firstname" => "First Name: ", "lastname" => "Last Name: ", "username" => "Username: ", "password" => "Password: ", "emailaddress" => "Email Address");
if($x == false){
foreach($errors as $error){ // goes through my errors and echos them in HTML.
echo "<div class='error'> $error </div>";
}
}
echo '<form action="" method="post">';
echo '<br>';
foreach($userregistration as $names => $label){
echo "<div class = 'field'>",
"<label for='$name'>$label</label>";
echo "<input type='text' name='$names' id='$names' value=".$defaults[$names]." >";
}
echo "<input type='submit' name='submit2'>";
?>
<div id="footer">
<p> © Copyright 2013 | Email Address: Dfa24@nau.edu </p>
</div>
</div>
</body>
</html>
数据库现在正在接受信息,但它只是将我发送到一个空白页面。
编辑:这是我要连接的页面。它是我目录的一部分:
<?php
define("HOST", "localhost"); // The host you want to connect to.
define("USER", "root"); // The database username.
define("PASSWORD", "root"); // The database password.
define("DATABASE", "dfa24");
session_start();
if ($_SESSION['logged'] != '1')
{
header('Location: MemberIndex.php');
exit();
}else{
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE)
or die("Failed to connect");
$sql = "SELECT firstName,lastName FROM Member
WHERE loginName= '{$_SESSION['logname']}' ";
$result = $mysqli->query($sql)
or die("Couldn't execute query");
$row = mysqli_fetch_assoc($result);
extract($row);
}
?>
<html>
<head>
<title> Member's Only Page </title>
<link href="projects.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?php
echo "<div class='wrapper'>
<h1 id='top'> Welcome To Duke's Homepage </h1>
<div id='menubar'>
<div id='menu'>
<ul id='a_links'>
<li><a href=''>About Duke</a></li>
<li><a href='contactform.php'>Contact Me</a></li>
<li><a href='Resume.html'>My Resumé</a></li>
<li><a href='membership.php'>Sign In</a></li>
<li><a href='EditMyInfo.php'>Edit Info</a></li>
</ul>
</div>
</div>
</div>"; ?>
</body>
</html>