0

我正在尝试在 error() 中使用 header 函数,我注释了此函数中的所有代码并插入了 header(),但它不起作用。

这是链接http://www.htmlgoodies.com/beyond/php/article.php/3877766/Web-Developer-How-To-Upload-Images-Using-PHP.htm

如果没有错误,则应执行插入语句。如何在 URL 代码中执行此操作?

更新

function error($error, $location, $seconds = 5) 
{ 
   header("Location:test.php");
}

INSERT into table; This query should execute only when there are no errors.How would i do this ?
4

2 回答 2

0

肮脏的修复:

function error($error, $location, $seconds = 5) 
{

    $urlToRedirect = $location . '?error_message=' . urlencode($error) ;
    header('Location: ' . $urlToRedirect);
    exit;

}

更好的解决方案是为错误消息创建一个数组,如下所示:

$errorMessages = array(
    'incorrect_login' => 'Incorrect login credentials.',
    'restricted' => 'Restricted page.'
);

然后重写您的错误函数并删除 seconds 参数,因为您不再使用刷新(就个人而言,我也会删除 location 参数,以便所有错误重定向都指向同一页面):

function error($error, $location) 
{
    $urlToRedirect = $location . '?error_message=' . $error ;
    header('Location: ' . $urlToRedirect);
    exit;
}

然后,像这样调用函数:

error('restricted', 'error.php');

然后,在您要显示错误的页面上:

if(isset($_GET['error_message']) && array_key_exists($_GET['error_message'], $errorMessages)){
    echo $errorMessages[$_GET['error_message']];
}
于 2012-10-19T10:29:05.263 回答
0

尝试这个::

function error($error, $location, $seconds = 5)
    { 
        header('Refresh: '.$seconds.'; url=' . $location);

    $content .= <<< END

            <html><head>
            <title>Upload error</title>
            </head>
            <body> 
                    <div id="Upload">
                    <h1>Upload failure</h1>
                    <p>An error has occurred: 
                    <span class="red">$error</span>
        The upload form is reloading</p>
        </div>
        </html>
END;

        echo $content;

        exit; 
    } // end error handler 
于 2012-10-19T10:51:35.073 回答