首页 文章

未定义的索引PHP问题 . 请帮忙

提问于
浏览
0

我有以下错误:

注意:未定义的索引:在第6行的C:\ wamp \ www \ registration \ register.php中提交

似乎无法解决什么错误???这是代码::

<?php
    //Create registration form (register.php)
    include "../includes/db_connect.php";

    if(!$_POST['submit']) ///Line 6
    {
    ?>
    <html>
    <head><link rel="stylesheet" href="style.css"></head>

    <div class="divider">
    <strong>Register</strong>

<form method="post" action="register.php"> <div class="formElm"> <label for="first">First Name</label> <input id="first" type="text" name="first"> </div> <div class="formElm"> <label for="last">Last Name</label> <input id="last" type="text" name="last"> </div> <div class="formElm"> <label for="username">Desired Username</label> <input id="username" type="text" name="username"> </div> <div class="formElm"> <label for="password">Password</label> <input id="password" type="password" name="password"> </div> <div class="formElm"> <label for="pass_conf">Confirm Password</label> <input id="pass_conf" type="password" name="pass_conf"> </div> <div class="formElm"> <label for="email">Email</label> <input id="email" type="text" name="email"> </div> <div class="formElm"> <label for="about">About</label> <textarea id="about" cols="30" rows="5" name="about">Tell us about yourself</textarea> </div> <input type="submit" name="submit" value="Register"> </form> or <a href="index.php">Login</a> </div> </html> <?php } else { $first = protect($_POST['first']); $last = protect($_POST['last']); $username = protect($_POST['username']); $password = protect($_POST['password']); $pass_conf = protect($_POST['pass_conf']); $email = protect($_POST['email']); $about = protect($_POST['about']); $errors = array(); $regex = "/^[a-z0-9]+([_\.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+\.[a-z]{2,}$/i"; if(!preg_match($regex, $email)) { $errors[] = "E-mail is not in name@domain format!"; } if(!$first || !$last || !$username || !$password || !$pass_conf || !$email || !$about) { $errors[] = "You did not fill out the required fields"; } $sql = "SELECT * FROM `users` WHERE `username`='{$username}'"; $query = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($query) == 1) { $errors[] = "Username already taken, please try another"; } if(count($errors) > 0) { echo "The following errors occured with your registration"; echo "<font color=\"red\">"; foreach($errors AS $error) { echo "<p>" . $error . "\n"; } echo "</font>"; echo "<a href=\"javascript:history.go(-1)\">Try again</a>"; //we use javascript to go back rather than reloading the page // so the user doesn't have to type in all that info again. } else { $sql = "INSERT into `users`(`first`,`last`,`username`,`password`,`email`,`about`) VALUES ('$first','$last','$username','".md5($password)."','$email','$about');"; $query = mysql_query($sql) or die(mysql_error()); echo "Thank You for registering {$first}! Your username is {$username}"; echo "<a href=\"index.php\"> Click here </a> to Login"; } } ?>

4 回答

  • 0

    要摆脱这个错误,它应该是:

    if(!isset($_POST['submit']))
    

    但是,您的代码已经可以了 .

    你得到的不是错误,而是一个警告,这是由严格的警告引起的 . PHP是一种动态语言,通常不需要定义变量和数组键,大多数文档和代码都会跳过这一部分 . 因此,您应该考虑关闭此功能,因为它会使代码混乱并且几乎没有其他好处 . 或者,切换到静态编译的语言(比如asp.net),它将真正受益于已定义的变量和静态类型 .

  • 0

    如果根本没有POST参数或者没有名为 submit 的参数,那么您正在尝试访问不存在的数组索引,因此警告 . 您可以简单地测试_POST数组中是否存在这样的索引/元素 .

    if( isset($_POST['submit']) )
    

    它没有't check the value (like you original script, which tests if the value of _POST[' submit']等于false,请参见type juggling),但在这种情况下,仅仅存在索引/元素就足够了 .

    http://docs.php.net/isset

  • 2

    首次加载页面时,$ _POST不存在 . 将支票更改为:

    if(!isset($_POST["submit"]))
    

    因为您还没有发布任何内容,所以$ _POST数组中没有“submit”键 . 这就是导致警告的原因 .

  • 1

    对于那些发布使用if(isset($ _ POST ['submit'])),你显然没有阅读他的代码 . 他放的是没有提交写HTML表单其他使用字段(向后扶正我!)

    如果他想保持结构原样,它应该是

    if(empty($_POST['submit']))
    

相关问题