2014-12-29 4 views
-1

나는 PhP를 사용하여 전자 메일 주소를 가져 와서 데이터베이스에 저장하는 동적 웹 페이지를 렌더링합니다. 오류가 있고 페이지가 다시로드되지만 코드를 실행할 때 정의되지 않은 변수라고하면 값을 유지하는 데 이중 달러 기호를 사용합니다. 다음은 코드가 실행될 때 정의되지 않은 변수가 나타나는 이유는 무엇입니까?

내 코드의 관련 하위 절입니다 : 가변 변수의 더 이해 PHP manual를 참조하십시오 ...

<?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

귀하의 오류가 이중 달러 기호의 beacause를하다

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

?> 
관련 문제