PHP - 未定义的索引错误:注册表单上的电子邮件和密码

时间:2015-12-15 21:01:39

标签: php html

我是php的新手。我的课程有一个网站项目。我被困在这一部分,无法移动更多。需要帮助解决这个问题。我尝试在我的代码上找到电子邮件和密码,这个错误说它不是这样的。

我有这样的错误:

  

注意:未定义的索引:第6行的C:\ xampp \ htdocs \ CONFIG.PHP中的电子邮件   注意:未定义的索引:C:\ xampp \ htdocs \ CONFIG.PHP中的密码   第7行信息无法写入数据库!

(用

解决
if($_SERVER['REQUEST_METHOD'] == 'POST') {
//...

)但现在任何信息都不会转到数据库

的config.php:

<?php
    ob_start();
    include "dbconnect.php";

    $email=$_POST["email"];
    $password=$_POST["password"];
    $username=$_POST["username"];
    $submit = isset($_POST['submit']) ? $_POST['submit'] : null;
        $sql="select * from users where email = '".$email."' and password = '".$password."' and username = '".$username."'";
        $query=mysql_query($sql);
        $counter=mysql_num_rows($query);
        if ($counter!=0){ 
            echo "<font size= 3 >This user has been registered before. Please check information.</font>";
        }else{
            $sql2 = "INSERT INTO users(email, password, username) VALUES ('".$email."','".$password."','".$username."'";
            $add = mysql_query($sql2);
             if($submit){ 
                echo "<br>You have registered successfully!
                Please click. ";
                header("Location:regedIndex.html");
             }else{ 
                echo "Info could not write on database!"; 

             } 
            ob_end_flush();
         }
?>

register.html:

<html>
    <head>
        <script type="text/javascript" src="jquery-1.9.1.min.js"></script>
        <link type="text/css" rel="stylesheet" href="popupbox.css"/>
        <link type="text/css" rel="stylesheet" href="main.css"/>
        <script type="text/javascript" src="https://www.google.com/jsapi">
    </script>
    </head>
    <body>
    <div id="logo">
            <div id="left" style="width: 500px;
                height: 100px; margin: 10px 15px 10px 8px;">
                <a href="index.php">
                    <img src="images/logo.png">
                </a>
            <div id="right">

            </div>
        </div>

        <div id="login">
            <div id="triangle"></div>
            <h1>Register</h1>
                <form action="config.php" method="post">
                    <input type="email" name="email" placeholder="Email" />
                    <input type="text" name="username" placeholder="Username" />
                    <input type="password" name="password" placeholder="Password" />
                    <input type="submit" value="Register"/>
                </form>
        </div>
        </br></br></br></br></br></br></br></br></br></br></br></br></br></br>
        <div id="contacts">
            <footer id="footer">
                <ul class="icons">
                    <li>
                        <a href="https://twitter.com/voteproject"> <img src="images/twitter.svg"></a>
                        <a href="https://facebook.com/voteproject"> <img src="images/facebook.svg"></a>
                        <a href="https://github.com/CS320VoteProject/VoteProject"> <img src="images/github.svg"></a>
                    </li>
                </ul>
            </footer>
        </div>
</body>
</html>

2 个答案:

答案 0 :(得分:4)

添加if($_SERVER['REQUEST_METHOD'] == 'POST')以检查表单是否已提交。

e.g。

<?php
  if($_SERVER['REQUEST_METHOD'] == 'POST') {
    ob_start();
    include "dbconnect.php";

    $email=$_POST["email"];
    $password=$_POST["password"];
    $username=$_POST["username"];
    $submit = isset($_POST['submit']) ? $_POST['submit'] : null;
    $sql="select * from users where email = '".$email."' and password = '".$password."' and username = '".$username."'";
    $query=mysql_query($sql);
    $counter=mysql_num_rows($query);
    if ($counter!=0){ 
        echo "<font size= 3 >This user has been registered before. Please check information.</font>";
    }else{
        $sql2 = "INSERT INTO users(email, password, username) VALUES ('".$email."','".$password."','".$username."'";
        $add = mysql_query($sql2);
         if($submit){ 
            echo "<br>You have registered successfully!
            Please click. ";
            header("Location:regedIndex.html");
         }else{ 
            echo "Info could not write on database!"; 

         } 
        ob_end_flush();
    }
  }
?>

答案 1 :(得分:1)

错误告诉您索引(通常是数组键)email不存在。这是一行:

$email=$_POST["email"];

它抛出此错误的原因是因为$_POST超全局仅在提交表单后填充,而不是第一次加载时。但是,你不会检查是否是这种情况,因此错误。您可以通过简单地检查代码周围的请求方法来避免它:

<?php
ob_start();
include "dbconnect.php";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email=$_POST["email"];
    $password=$_POST["password"];
    // etc.
}

在(非常重要的)旁注:请停止使用mysql_功能。它们自2013年以来一直被弃用,完全放弃了本月早些时候发布的PHP7。这意味着不再支持代码。 如果您尝试在最新的服务器上运行此脚本,它将无法运行。另请参阅Why shouldn't I use mysql_* functions in PHP?