-2

mysql エラーの代わりに html コンテンツを追加したいので、黒い背景と「エラー」というテキストだけで mysql エラーが発生したときに失敗するのではなく。

mysql_query($query) or die ('error');

このhtmlコンテンツを表示したい:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <title></title>

    <link rel="stylesheet" type="text/css" href="css/style.css" media="all" />


</head>
<body>

<div id="container">
    <h1>REGISTRATION WAS UNSUCCESFULL</h1><br/><br/><br/><br/>
    <div class="textarea">
        <h3>Whoop! Good News we've successfully receieved your registration. So now what we've done, is sent you an email. In this email you will be guided through the final stage of account set-up.<br/><br/>Keep an eye on your inbox and follow the instructions. We hope to see you real soon ;-)</h3>
    </div>
        <div class="man_reg"><img src="../assets/img/help_support/man.png" alt="" width="210" height="214" /></div></div>
    <div id="progress_bar">
        <div id="progress"></div>
        <div id="progress_text">Registration Completed</div>
    </div>

    <style>
    .textarea{
        padding-left:55px;
        padding-right:55px;
        text-align:center;
    }
    .man_reg{
        margin-top:54px;
        margin-left:450px;
    }

    </style>    
</body>
</html>

mysql または die エラーの括弧内に html コンテンツを挿入するだけではありませんか?

そのようです:

mysql_query($query) or die ('HTML CONTENT HERE!');
4

5 に答える 5

1

基本的には括弧内にコンテンツを入れるだけですが、より洗練された方法は別のerror.htmlテンプレートを用意してそれをinclude()ing することです。こうすることで、大量のマークアップでコードが汚染されることはありません。

error.html (簡略化):

<html><head></head><body><?php echo $error; ?></body></html>

PHP コード:

$result = mysql_query(....);

if (!$result)
 { 
   $error = mysql_error();
   include "error.html";
   die(); 
 }

本番環境でエラーを表示するのは悪いスタイルであることに注意してください。開発中は正確なエラー メッセージを表示し、プロジェクトが公開されている場合は一般的な「エラーが発生しました」ページを表示することができます。

于 2013-03-06T10:21:40.163 に答える
1

より良い解決策は、そのコンテンツを含むエラー ページを作成し、次のコードを使用してユーザーをそのページにリダイレクトすることです。

$res = mysql_query($query);
if (!$res) {
    header('Location: http://yourdomain.com/error.html');
    die(); // just to end up the execution
}
于 2013-03-06T10:20:26.020 に答える
0

このためにも関数を使用できます

function dieerr() {
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title></title>

<link rel="stylesheet" type="text/css" href="css/style.css" media="all" />


</head>
<body>

<div id="container">
<h1>REGISTRATION WAS UNSUCCESFULL</h1><br/><br/><br/><br/>
<div class="textarea">
    <h3>Whoop! Good News we\'ve successfully receieved your registration. So now what we\'ve done, is sent you an email. In this email you will be guided through the final stage of account set-up.<br/><br/>Keep an eye on your inbox and follow the instructions. We hope to see you real soon ;-)</h3>
</div>
    <div class="man_reg"><img src="../assets/img/help_support/man.png" alt="" width="210" height="214" /></div></div>
<div id="progress_bar">
    <div id="progress"></div>
    <div id="progress_text">Registration Completed</div>
</div>

<style>
.textarea{
    padding-left:55px;
    padding-right:55px;
    text-align:center;
}
.man_reg{
    margin-top:54px;
    margin-left:450px;
}

</style>    
</body>
</html>';
exit;
}

そして、あなたがこれを持っているなら、それをそのように使ってください:

mysql_query($query) or dieerr();
于 2013-03-06T10:22:30.093 に答える