我在下面有以下php代码,一旦提交表单,它就会抛出一个未定义的变量。我不确定我做错了什么,谁能告诉我导致错误的原因是什么?
错误行:
注意:未定义的变量:第64行的/home/roberkl103/domains/**/public_html/dev/edit_account.php中的error_message
<?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;'> </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> </td>
<td>
<input type='submit' name='profileoptions' />
</td>
</tr>
</table>
</form>
";
echo $options_content;
}
?>
答案 0 :(得分:3)
这一行是问题所在:
if ($error_message != "")
它假设存在变量$error_message
,但如果没有任何错误条件为真,那么您不会在任何地方定义$error_message
。因此PHP抱怨。
有几种方法可以解决这个问题。我建议只需将行更改为
if (isset($error_message))
函数isset
在PHP中是“特殊的”,可用于检查变量是否已定义并且给定非null
值而不触发任何警告。
答案 1 :(得分:0)
在该行上,您可以访问$error_message
:
if($error_message != '') {
如果出现错误,请设置它:
if(...) {
$error_message = /* ... */;
}
但是,如果没有错误,则永远不会设置$error_message
,并且您正在使用未定义的变量。你应该在开始时初始化它:
$error_message = '';