0

我在下面有以下 php 代码,一旦提交表单,它就会不断抛出一个未定义的变量。我不确定我做错了什么,谁能告诉我是什么导致了错误?

错误行:

注意:未定义的变量:/home/roberkl103/domains/ * */public_html/dev/edit_account.php 中的 error_message 在第 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;
}
?>
4

2 回答 2

3

这条线是问题所在:

if ($error_message != "")

它假定$error_message存在变量,但如果没有任何错误条件为真,那么您就没有$error_message在任何地方定义。因此 PHP 抱怨。

有几种方法可以解决这个问题。我建议只需将线路更改为

if (isset($error_message))

该函数isset在 PHP 中是“特殊的”,可用于检查变量是否已定义并赋予非null值而不触发任何警告。

于 2013-06-29T23:18:26.190 回答
0

在该行上,您可以访问$error_message

if($error_message != '') {

如果有错误,你设置它:

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

但是,在没有错误的情况下,$error_message永远不会设置,并且您使用的是未定义的变量。您应该在开始时对其进行初始化:

$error_message = '';
于 2013-06-29T23:18:50.543 回答