未定义的变量多个错误

时间:2013-11-18 21:51:24

标签: php smarty

我目前正在开发一个网站,并且在我的注册页面中不断收到2个错误。这些是错误:

Notice: Undefined variable: error in C:\xampp\htdocs\olympus\register.php on line 35

Notice: Undefined variable: message in C:\xampp\htdocs\olympus\register.php on line 36

这些是他们所指的代码行:

line 35 - $smarty->assign('error',$error);

line 36 - $smarty->assign('message',$message);

我已经仔细检查了一切,我找不到为什么会有这些错误。 拜托,有人可以帮我解决这个问题吗?

编辑:到目前为止,这是完整的register.php文件。

<?php

require_once 'smarty.php';

if($_POST) {
    $password = $_POST['password'];
    $confirm = $_POST['confirm'];
    if($password != $confirm) {
        $error = 'Passwords do not match!'; 
    } else {
        require_once 'config.php';      // our database settings
        $conn = mysql_connect($dbhost,$dbuser,$dbpass)
            or die('Error connecting to mysql');
        mysql_select_db($dbname);
        $query = sprintf("SELECT COUNT(id) FROM users WHERE UPPER(username) = UPPER('%s')",
            mysql_real_escape_string($_POST['username']));
        $result = mysql_query($query);
        list($count) = mysql_fetch_row($result);
        if($count >= 1) { 
            $error = 'that username is taken.';
        } else {
            $query = sprintf("INSERT INTO users(username,password) VALUES ('%s','%s')",
                mysql_real_escape_string($_POST['username']),
                mysql_real_escape_string(md5($password)));
            mysql_query($query);            
            $userID = mysql_insert_id($conn);
            require_once 'stats.php';
            setStat('atk',$userID,'5');
            setStat('def',$userID,'5');         
            setStat('mag',$userID,'5');
            $message = 'Congratulations, you registered successfully!';
        }
    }   
}
$smarty->assign('error',$error);
$smarty->assign('message',$message);
$smarty->display('register.tpl');

?>

1 个答案:

答案 0 :(得分:0)

您的错误清楚地描述了最新情况:

$error$message未设置。多数民众赞成。

你应该至少在那行之前初始化它们:

$error = $message = '';

$error = $message = array();

而且,顺便说一句:它只是注意,而不是语法错误,因此可以避免在开始时不显示此代码的错误:

error_reporting(E_ALL & ~E_NOTICE);

<强>编辑:

如果未设置$_POST(未发送表单),则不会进入条件而不是条件。那么你的两个变量是未定义的。所以只需在开始时预先定义它们。将您的代码更改为:

<?php

require_once 'smarty.php';

$error = $message = ''; // THIS IS ADDED HERE...

if($_POST) {
    $password = $_POST['password'];
    $confirm = $_POST['confirm'];
    if($password != $confirm) {
        $error = 'Passwords do not match!'; 
    } else {
        require_once 'config.php';      // our database settings
        $conn = mysql_connect($dbhost,$dbuser,$dbpass)
            or die('Error connecting to mysql');
        mysql_select_db($dbname);
        $query = sprintf("SELECT COUNT(id) FROM users WHERE UPPER(username) = UPPER('%s')",
            mysql_real_escape_string($_POST['username']));
        $result = mysql_query($query);
        list($count) = mysql_fetch_row($result);
        if($count >= 1) { 
            $error = 'that username is taken.';
        } else {
            $query = sprintf("INSERT INTO users(username,password) VALUES ('%s','%s')",
                mysql_real_escape_string($_POST['username']),
                mysql_real_escape_string(md5($password)));
            mysql_query($query);            
            $userID = mysql_insert_id($conn);
            require_once 'stats.php';
            setStat('atk',$userID,'5');
            setStat('def',$userID,'5');         
            setStat('mag',$userID,'5');
            $message = 'Congratulations, you registered successfully!';
        }
    }   
}
$smarty->assign('error',$error);
$smarty->assign('message',$message);
$smarty->display('register.tpl');

?>

或者只是移动它:

$smarty->assign('error',$error);
$smarty->assign('message',$message);

在最后的条件if($_POST) {内。