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("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
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>

最佳答案

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

关于php - 数据发送后,MySQL出现空白页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19850204/

相关文章:

c# - 我可以运行应用程序(或存储过程)并从触发器获取结果吗

mysql - 两个表的 AUTO_INCRMENT

php - Wordpress 可以用来创建与预先存在的 php 文件和数据库相关的网页吗?

MYSQL:DECIMAL,逗号后精度为 10 位

Mysql在多个数据库中查找值(相同结构)

php - 有没有更好的方法在 PHP 中获取货币汇率?

php - 如何解决 facebook og title 属性中的双引号问题

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

php - 在选择查询中从两个不同的表中减去两个不同的列总和

php - MySql 将 "mm:ss"存储为 "hh:mm"