首页 文章

注意:未定义索引:ZZZZZZWTF? [重复]

提问于
浏览
1

可能重复:PHP:“注意:未定义的变量”和“通知:未定义的索引”

我的PHP代码突然提出:

注意:未定义的索引:在第36行的C:\ xampp \ htdocs \ globalautoparts \ register.php中提交

注意:未定义的索引:第40行的C:\ xampp \ htdocs \ globalautoparts \ register.php中的fullname

注意:未定义的索引:第41行的C:\ xampp \ htdocs \ globalautoparts \ register.php中的用户名

注意:未定义的索引:第42行的C:\ xampp \ htdocs \ globalautoparts \ register.php中的密码

注意:未定义的索引:第43行的C:\ xampp \ htdocs \ globalautoparts \ register.php中的repeatpassword

注意:未定义的索引:第45行的C:\ xampp \ htdocs \ globalautoparts \ register.php中的电子邮件

在注册页面上 .

我如何解决它?

这是我的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta name="author" content="Luka Cvrk (solucija.com)" />
    <meta name="keywords" content="conceptnova, concept, framework, web, content, corporate, business" />
    <meta name="description" content="Conceptnova" />   
    <link rel="stylesheet" href="css/main.css" type="text/css" media="screen, projection" />
    <title>Global Autoparts | Home</title>
</head>
<body>
    <div id="wrap">
        <div id="header_top">
        <h1 id="logo"><a href="#" title="Conceptnova"><img src="images/logo.gif" align="left" /></a></h1>
        <h1 id="logo"></h1>
        <ul>
                <li><a href="index.php">home</a></li>
                <li><a href="aboutus.php">about us</a></li>
                <li><a href="services.php">services</a></li>
                <li><a href="portfolio.php">portfolio</a></li>
                <li><a href="contact.php">contact</a></li>
          </ul>
            <div id="slogan">
                <p></p>
            </div>
        </div>
        <div id="header_bottom">
            <h2>Think outside of the box!</h2>
            <p>Registering with Global Auto Parts means you have access to super-fast online orders and total user customization, so you wont have to wait in a line again!</p>
        </div>  
        <div id="maincontent">
            <div id="left">
                <h2><a href="#">Register to become apart of the global community!</a></h2>
                <p><?php
echo "<h1>Registration Page.</h1>";
$submit = $_POST['submit'];


//form data
$fullname = strip_tags($_POST['fullname']);
$username = strtolower(strip_tags($_POST['username']));
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
$date = date("Y-m-d");
$email = $_POST['email'];

if ($submit)
{

     //open database
     $connect = mysql_connect("localhost","root","");
                mysql_select_db("phplogin"); //select database

     $namecheck = mysql_query("SELECT username FROM users WHERE username='$username'");
     $count = mysql_num_rows($namecheck);

     if  ($count!=0)

     {
       die("Username already taken! Go <a href='register.php'>back</a> to try again?");

     }

     // check for existence
     if($fullname&&$username&&$password&&$repeatpassword)
     {


       if ($password==$repeatpassword)
       {

               //check char length of username and fullname
               if (strlen($username)>25||strlen($fullname)>25)
               {
               echo "Length of username or fullname is too long!";


               }

               else{
               //check password length
               if (strlen($password)>25||strlen($password)<6)
               {
               echo "Password must be between 6 and 25 characters";
               }
               else{
               //register the user!

               // encrypt password
       $password = md5($password);
       $repeatpassword = md5($repeatpassword);


                  //generate random number for activation process
                  $random = rand(23456789,98765432);
                $queryreg = mysql_query("

                INSERT INTO users VALUES ('','$fullname','$username','$password','$email','$date','$random','0')

                ");

                $lastid = mysql_insert_id();


                //send activation email
                ini_set("SMTP",$server);
                $to = $email;
                $subject = "Activate your account!";
                $headers = "From: Global Auto Parts";
                $server = "localhost";





                $body = "

                Hello $fullname,\n\n

                You need to activate your account with the link below:

                http://localhost/globalautoparts/activate.php?=$lastid&code=$random \n\n


                Thanks.

                ";

                //function to send mail
                mail($to, $subject, $body, $headers);

                die("You have been registered! Check your email to activate your account!");
               }


               }
       }
       else
       echo "Your passwords do not match!";

     }
    else
        echo "Please fill in <b>all</a> fields!";




}

?>

<html>
<p>
<form action='register.php' method='POST'>
      <table>
             <tr>
                 <td>
                 Your full name:
                 </td>
                 <td>
                 <input type='text' name='fullname' value='<?php echo $fullname; ?>'>
                 </td>
             </tr>
             <tr>
                 <td>
                 Choose a username:
                 </td>
                 <td>
                 <input type='text' name='username' value='<?php echo $username; ?>'>
                 </td>
             </tr>
             <tr>
                 <td>
                 Choose a password:
                 </td>
                 <td>
                 <input type='password' name='password'>
                 </td>
             </tr>
             <tr>
                 <td>
                 Repeat your password:
                 </td>
                 <td>
                 <input type='password' name='repeatpassword'>
                 </td>
             </tr>
             <tr>
                 <td>
                 Email:
                 </td>
                 <td>
                 <input type='text' name='email'>
                 </td>
             </tr>
      </table>
      
<p> <input type='submit' name='submit' value='Register'> </form> </p> </div> <div id="right"> <div id="searchform"> <form method="post" action="#"> <p><input type="text" name="search" class="search" value="Search Keywords" /><input type="submit" value="GO" class="submit" /></p> </form> </div> <p> <html> <form action='login.php' method='POST'> Username: <input type='text' name='username'>

Password: &nbsp;<input type='password' name='password'>

Click <a href='register.php'>here</a> to register. <input type='Submit' value='Login' id="login"> </form> <p> </p>
</div> </div> <div id="footer"> <p>&copy; Copyright 2011 <a href="#">Global Autoparts</a>&nbsp;&nbsp;Design: Victor Gatto </p> </div> </div> </body> </html> <!--REGBOX-->

4 回答

  • 1

    正如其他人建议的那样,你可以

    Change the Error Reporting Level

    通过在脚本顶部运行此代码: error_reporting(E_ALL ^ E_NOTICE)

    或者,在使用它们之前实际检查这些变量是否已发布可能是更好的主意 .

    Quick and Dirty fix is to do this:

    @$_POST['submit']
    

    @符号将禁止任何错误并返回false代替字符串 . 现在这种做法一般都不受欢迎 .

    If you want a cleaner method, I would do this:

    <?php 
    $post = array('fullname'=>'','username'=>'','password'=>'','repeatpassword'=>'');
    
    if($_POST) $post = array_merge($post, $_POST);
    
    $fullname = strip_tags($post['fullname']);
    $username = strtolower(strip_tags($post['username']));
    $password = strip_tags($post['password']);
    $repeatpassword = strip_tags($post['repeatpassword']);
    

    然后,您永远不会有关于缺少索引的错误,您可以将默认值设置为您喜欢的任何值 .

    祝好运

  • 5

    有人已经提升了您的服务器/应用程序上的错误报告,以包含 E_NOTICE 错误 .

    您可以使用禁用此功能

    error_reporting(E_ALL ^ E_NOTICE);
    

    或者在使用之前检查数组索引来改进应用程序

    $submit = isset($_POST['submit']) ? $_POST['submit'] : null;
    
  • 7

    好吧,这些可能总是已生成,但您的错误报告设置可能已更改为最近包含通知 . PHP只是通知您're setting a variable equal to another variable that isn't已定义 . 当 $_POST['submit'] 尚未填充时,当您尝试使用它时,您将收到来自PHP的通知错误 . 您可以在尝试使用它们之前检查这些变量是否已设置,或者只是告诉PHP不要向您显示通知错误:

    ini_set('error_reporting', E_ALL ^ E_NOTICE);
    
  • 0

    你应该为if($ submit)块之外的表单数据设置默认值,并在if块中获取表单数据,以避免严格php环境中的错误/通知 .

    // default form data
    $fullname = '';
    $username = '';
    $password = '';
    $repeatpassword = '';
    $email = '';
    $date = date("Y-m-d");
    
    if ($submit)
    {
        //form data
        $fullname = strip_tags($_POST['fullname']);
        $username = strtolower(strip_tags($_POST['username']));
        $password = strip_tags($_POST['password']);
        $repeatpassword = strip_tags($_POST['repeatpassword']);
        $email = $_POST['email'];
    
        // your code ...
    }
    

相关问题