Php 数据发送后,将进入一个空白页MySQL

Php 数据发送后,将进入一个空白页MySQL,php,mysql,Php,Mysql,我目前正在处理我的注册页面,每次我点击提交按钮,它都不会将我的信息提交到数据库。否则,如果在某些情况下它确实获取了信息,它会将我带到一个空白屏幕,当我有一个标题时,它只会将我带到一个受限制的部分。我的语法错了吗 <?php //serror_reporting(E_ALL); //ini_set('display_errors', 1); define("HOST", "localhost"); // The host you want to connect to. define("USE

我目前正在处理我的注册页面,每次我点击提交按钮,它都不会将我的信息提交到数据库。否则,如果在某些情况下它确实获取了信息,它会将我带到一个空白屏幕,当我有一个标题时,它只会将我带到一个受限制的部分。我的语法错了吗

<?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&#233</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> &copy; Copyright 2013 | Email Address: Dfa24@nau.edu </p>
    </div>
    </div>
</body>
</html>

所以我打开了错误报告,它说“警告:无法修改标题信息”,我用谷歌搜索了这个问题,这是因为在我的php标记之前有一行额外的内容。。。现在一切似乎都很好。

mysql\u real\u escape\u我认为需要打开mysql连接。因为您使用的是mysqli,所以这是错误的。无论如何,您可能应该使用准备好的语句。我在不同的页面上使用相同的转义字符串,它似乎工作正常。我会去掉转义字符串,改用$\u POST吗?不,你应该使用准备好的语句,或者至少使用mysqli\u real\u escape\u字符串。单独使用$\u POST可能会造成灾难。一定要清理您的输入,但要与您使用的db驱动程序兼容。我更改了它,它会将信息发送到数据库,但之后当我点击提交时,它会将我发送到空白页。。
    <?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&#233</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>