2014-12-29 76 views
-1

我正在使用PhP來呈現一個動態網頁,它需要一個電子郵件地址並將其存儲在數據庫中。如果出現錯誤並且頁面被重新加載,我使用雙美元符號來維護該值,但是在運行代碼時它說它是未定義的變量。爲什麼我的代碼運行時會得到未定義的變量?

下面是我的代碼中的相關小節:

<?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>"); 

?> 
+0

哪個變量未定義? – simeg

+2

將'$$ email'更改爲'$ email'。 – haim770

+1

_「如果出現錯誤並且頁面重新加載,我使用雙美元符號來維護該值」_ - 這不是'$$'代表的。要在頁面加載中保留該值,您需要將其存儲在'$ _SESSION'中。 –

回答

1

你的錯誤是東陽雙美元符號的...參見PHP manual可變的變量更多的瞭解

<?php 
$a = 'hello'; 
$$a = 'world'; 

echo "$a ${$a}"; // outputs hello world 
echo "$a $hello"; // outputs hello world But see the (dynamic) variable variable $hello 
?> 
0

試試這個

<?php 
$email = isset($_POST["email"]) ? $_POST[ "email" ] : ""; 
$iserror = false; 
$formerror = false; 
if (isset($_POST["submit"])) { 
    if($email == ""){ 
     $iserror = true; 
     $formerror = true; 
    } 
    $connect = mysql_connect("localhost","iw3htp", "password"); 
    mysql_select_db("Mailer"); 
    if(!$iserror) { 
     $query = "INSERT INTO email(Address) values ('".$email."')"; 
     $result = mysql_query($query); 
     if($result != '') { 
      print("<p>Hi! Your e-mail ".$email." has been added to our mailing list.</p></body></html>"); 
     }else{ 
      print("<p>Could not execute query!</p>"); 
     } 
     mysql_close($conncet); 
    } 
} 

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>"); 

?> 
相關問題