dongqiuxu2270 2014-12-29 16:53
浏览 153

为什么我的代码运行时会得到一个未定义的变量?

I am using PhP to render a dynamic webpage that takes an email address and stores it in a database. I use the double dollar sign to maintain the value if there is an error and the page is reloaded but it says it is an undefined variable when I run the code.

Below is the relevant subsections of my code:

<?php
$email = isset($_POST["email"]) ? $_POST[ "email" ] : "";
$iserror = false;
$formerror = false;
if ( isset( $_POST["submit"] ) )
{
if($email == ""){
    $iserror = true;
    $formerror = true;
}

if(!$iserror)
{
    $query = "INSERT INTO email (Address) values ('$email')";

if ( !( $database = mysql_connect( "localhost", 
              "iw3htp", "password" ) ) )
              die( "<p>Could not connect to database</p>" );

           // open Mailer database
           if ( !mysql_select_db( "Mailer", $database ) )
              die( "<p>Could not open Mailer database</p>" );

           // execute query in Mailer database
           if ( !( $result = mysql_query( $query, $database ) ) ) 
           {
              print( "<p>Could not execute query!</p>" );
              die( mysql_error() );
           } // end if

           mysql_close( $database );

           print( "<p>Hi! Your e-mail $email has been added to our mailing list.</p>
              </body></html>" );
           die();
}
}

if ( $iserror )                                              
 {                                                            
        print( "<p class = 'error'>Fields with * need to be filled 
           in properly.</p>" );
  }

print("<form method='post' action='mail.php'><label>Join our mailing list</label>     <br>");
print("<input type='text' name='$email' value='" . $$email ."'>");
if($formerror == true)
{
print( "<span class = 'error'>*</span>" );
}

print("<input type='submit' name='submit' value='Join list' /></form></body></html>");

?>
  • 写回答

2条回答 默认 最新

  • dtef9322 2014-12-29 17:00
    关注

    Your error is beacause of double dollar sign... Refer PHP manual for more understanding of variable variables

    <?php
    $a = 'hello';
    $$a = 'world';
    
    echo "$a ${$a}"; // outputs hello world
    echo "$a $hello"; // outputs hello world But see the (dynamic) variable variable $hello
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败