0

I have the following php code below, it keeps throwing out a undefined variable once the form gets submitted. I'm not sure what im doing wrong, could anyone tell me what is causing the error?

the error line:

Notice: Undefined variable: error_message in /home/roberkl103/domains/**/public_html/dev/edit_account.php on line 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;
}
?>

2 Answers 2

3

This line is the problem:

if ($error_message != "")

It assumes that a variable $error_message exists, but if none of the error conditions are true then you are not defining $error_message anywhere. Therefore PHP complains.

There are several ways to solve the problem. I recommend simply changing the line to

if (isset($error_message))

The function isset is "special" in PHP and can be used to check if a variable has been defined and given a non-null value without triggering any warnings.

Sign up to request clarification or add additional context in comments.

Comments

0

On that line, you access $error_message:

if($error_message != '') {

You set it if there's an error:

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

However, in the case where there are no errors, $error_message is never set, and you're using an undefined variable. You should initialize it at the start:

$error_message = '';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.