2013-06-29 160 views
0

我在下面的php代碼中,一旦表單被提交,它會一直拋出一個未定義的變量。我不知道我做錯了什麼,有誰能告訴我是什麼原因導致了錯誤?函數中未定義的變量

誤差線:

說明:未定義變量:ERROR_MESSAGE在/ home/roberkl103 /域/ * * /的public_html的/ dev/edit_account.php上線64

<?php 
include "game_header.php"; 

$user = new User($_SESSION['id']); 

$_GET['type'] = isset($_GET['type']) && ctype_alpha($_GET['type']) ? trim($_GET['type']) : '0'; 

switch ($_GET['type']) { 
    case 'header' : ui_header(); break; 
    case 'options' : ui_options(); break; 
    default: ui_header(); 
} 

function ui_header() { 
    global $user, $game; 
    $whichsection = "Edit Account"; 
    $header = " 
     <div class='dsh_mid_content_lft'> 
      <div class='dsh_content_txt'> 
       ".$whichsection." 
      </div> 
      <div style='margin-top: 5px;'>&nbsp;</div> 
      <p align='center'> 
       <a class='button unactive' href='/edit_account/options.php'>Profile Options</a> 
       <a class='button unactive' href='/edit_account/password.php'>New Password</a> 
       <a class='button unactive' href='/edit_account/colouredname.php'>Coloured Name</a> 
       <a class='button unactive' href='/edit_account/profileflags.php'>Profile Flags</a> 
       <a class='button unactive' href='/edit_account/gameoptions.php'>Game Options</a> 
      </p> 
      <br /> 
    "; 
    echo $header; 
} 

function ui_options() { 
    global $user, $game; 
    if (isset($_POST['profileoptions'])) { 
     $username = mysql_real_escape_string(htmlspecialchars($_POST['username'])); 
     $email = mysql_real_escape_string(htmlspecialchars($_POST['email'])); 
     $gender = mysql_real_escape_string($_POST['gender']); 
     $quote = mysql_real_escape_string(htmlspecialchars($_POST['quote'])); 
     $signature = mysql_real_escape_string(htmlspecialchars($_POST['signature'])); 


     $name_query = mysql_query("SELECT COUNT(*) AS `c` FROM `mx_users` WHERE `username` = '{$username}' and `id` != '{$user->id}'"); 
     $name_fetch = mysql_fetch_assoc($name_query); 
     // Name already exists 
     if ($name_fetch['c'] > 0) { 
      $error_message = "The username you chose is already in use by someone else."; 
     } 
     // New name is too short 
     if (strlen($username) < 3) { 
      $error_message = "Your name is too short. Atleast 3 characters."; 
     } 
     // New name is too long 
     if (strlen($username) > 16) { 
      $error_message = "Your name is too long. Max 16 characters."; 
     } 
     // New email is invalid 
     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
      $error_message = "The e-mail address you entered was invalid."; 
     } 
     if ($error_message != "") { 
      echo $error_message; 
     } else { 
      $update = mysql_query("UPDATE `mx_users` SET `username` = '$username', `email` = '$email', `gender` = '$gender', `quote` = `$quote', `signature` = '$signature'"); 
      $success_message = "Your preferences have been saved."; 
     } 
    } 
    ui_header(); 
    $options_content = " 
     <form method='post' action='/edit_account/options.php'> 
      <table width='100%' border='0' cellpadding='5'> 
       <tr> 
        <td><b>Name</b>:</td> 
        <td> 
         <input type='text' name='username' value='".$user->username."' maxlength='16' size='20' /> 
         <em>Will not change your login name.</em> 
        </td> 
       </tr> 
       <tr> 
        <td><b>Email</b>:</td> 
        <td> 
         <input type='text' name='email' value='".$user->email."' size='40' /> 
        </td> 
       </tr> 
       <tr> 
        <td><b>Avatar</b>:</td> 
        <td> 
         <input type='text' name='avatar' value='".$user->avatar."' size='40' /> 
        </td> 
       </tr> 
       <tr> 
        <td><b>Quote:</b></td> 
        <td> 
         <input type='text' name='quote' value='".htmlspecialchars($user->quote)."' size='40' /> 
        </td> 
       </tr> 
       <tr> 
        <td><b>Gender</b>:</td> 
        <td> 
         <select name='gender'> 
          <option value='1'>Male</option> 
          <option value='2'>Female</option> 
         </select> 
        </td> 
       </tr> 
       <tr> 
        <td><b>Signature</b>:</td> 
        <td> 
         <textarea type='text' name='signature' cols='50' rows='6'> 

         ".$user->signature." 

         </textarea> 
        </td> 
       </tr> 
       <tr> 
        <td>&nbsp;</td> 
        <td> 
         <input type='submit' name='profileoptions' /> 
        </td> 
       </tr> 
      </table> 
     </form> 
    "; 
    echo $options_content; 
} 
?> 

回答

3

此行有一個問題:

if ($error_message != "") 

它假定一個變量$error_message存在,但如果沒有的錯誤條件爲真ñ你沒有在任何地方定義$error_message。因此PHP抱怨。

有幾種方法可以解決這個問題。我建議簡單地改變線

if (isset($error_message)) 

功能isset是在PHP「特殊」,如果一個變量已經被定義並沒有觸發任何警告給定的非null值可以被用於檢查。

0

該行上,您可以訪問$error_message

if($error_message != '') { 

您可以設置它,如果有一個錯誤:

if(...) { 
    $error_message = /* ... */; 
} 

然而,在沒有錯誤的情況下,$error_message從未設置,並且您正在使用未定義的變量。您應該在開始時初始化它:

$error_message = '';