2

fail.phpが呼び出されたときに電子メールアラートを停止する条件付きステートメントを作成しようとしています。現在、良い結果と失敗した結果の両方について電子メールアラートを受け取っています。

結果が失敗した場合、私は電子メールを受け取りたくありません。2つのスクリプトを作成する必要がありますか、それともこれを一緒に機能させる方法はありますか?

ありがとう

これが、スクリプト全体とともに参照しているセクションです。

if (mysql_affected_rows($result) > 0) {
mail($to, $subject, $msg, $headers);  
$reg =          $_REQUEST['reg'] ; 
$first_name =   $_REQUEST['first_name']; 
header("location: reg_add_success.php?reg=" . urlencode($reg) . "&first_name=" . urlencode($first_name)); 
} 
else { 
header("location: reg_add_fail.php"); 
exit(); // as sugested by John Conde
}

<?

$to = 'newreg@41q.org';
$subject = 'New Homeless Connection';
$msg = "<html>
<head>
<title>New Homeless Connection</title>
</head>

<body>
<table cellspacing=\"0\" cellpadding=\"10\" border=\"1\" align=\"left\">
<tr>
<td align=\"left\" width=\"150px\">Registery No.:</td>
<td align=\"left\"> $reg</td>
</tr>
<tr>
<td align=\"left\">First Name:</td>
<td align=\"left\">$first_name </td>
</tr>
<tr>
<td align=\"left\">Connection Date:</td>
<td align=\"left\"$>$connect_date</td>
</tr>
 <tr>
<td align=\"left\" colspan=\"2\">http://www.41q.org/admin/</td>
</tr>
</table>
<br>
<br>
</body>
</html>
";

// Make sure to escape quotes

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Homeless' . "\r\n";

mail($to, $subject, $msg, $headers);

date_default_timezone_set('America/Los_Angeles');
$submit_date = date("m/d/y g:i A") ; 

$order = "INSERT INTO reg_add (submit_date, 
connect_date, 
reg, 
first_name, 
)

VALUES

('$submit_date',
'$_POST[connect_date]', 
'{$_POST[reg]}nv', 
'$_POST[first_name]')";

$result = mysql_query($order);

if (mysql_affected_rows($result) > 0) {
mail($to, $subject, $msg, $headers);  
$reg =          $_REQUEST['reg'] ; 
$first_name =   $_REQUEST['first_name']; 
header("location: reg_add_success.php?reg=" . urlencode($reg) . "&first_name=" . urlencode($first_name)); 
} 
else { 
header("location: reg_add_fail.php"); 
exit(); // as sugested by John Conde
}
?>
4

4 に答える 4

2

の最初のインスタンスを削除しますmail($to, $subject, $msg, $headers);

次に、適切な測定のために、true / falseではなく、影響を受ける行の数を確認します(ただし、両方とも機能するはずです)。

if (mysql_affected_rows($result) > 0) {

}
于 2012-04-28T04:56:27.070 に答える
0

コードを確認する場合

// Make sure to escape quotes

$headers  = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: Homeless' . "\r\n";

mail($to, $subject, $msg, $headers);

date_default_timezone_set('America/Los_Angeles');

このコードは、結果に関係なく、すでにメールを送信しています。

トップコードからこの行を削除する必要があります

mail($to, $subject, $msg, $headers);

コードは正常に機能します。

于 2012-04-28T05:04:29.230 に答える
0

最終的なコード、親切にテストしてください

<?

$to = 'newreg@41q.org';
$subject = 'New Homeless Connection';
$msg = "<html>
<head>
<title>New Homeless Connection</title>
</head>

<body>
<table cellspacing=\"0\" cellpadding=\"10\" border=\"1\" align=\"left\">
<tr>
<td align=\"left\" width=\"150px\">Registery No.:</td>
<td align=\"left\"> $reg</td>
</tr>
<tr>
<td align=\"left\">First Name:</td>
<td align=\"left\">$first_name </td>
</tr>
<tr>
<td align=\"left\">Connection Date:</td>
<td align=\"left\"$>$connect_date</td>
</tr>
 <tr>
<td align=\"left\" colspan=\"2\">http://www.41q.org/admin/</td>
</tr>
</table>
<br>
<br>
</body>
</html>
";

// Make sure to escape quotes

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Homeless' . "\r\n";

date_default_timezone_set('America/Los_Angeles');
$submit_date = date("m/d/y g:i A") ; 

$order = "INSERT INTO reg_add (submit_date, 
connect_date, 
reg, 
first_name, 
)

VALUES

('$submit_date',
'$_POST[connect_date]', 
'{$_POST[reg]}nv', 
'$_POST[first_name]')";

$result = mysql_query($order);

if (mysql_affected_rows($result) > 0) {
mail($to, $subject, $msg, $headers);  
$reg =          $_REQUEST['reg'] ; 
$first_name =   $_REQUEST['first_name']; 
header("location: reg_add_success.php?reg=" . urlencode($reg) . "&first_name=" . urlencode($first_name)); 
} 
else { 
header("location: reg_add_fail.php"); 
exit(); // as sugested by John Conde
}
?>
于 2012-04-28T05:13:38.477 に答える
0

現在、いくつかのSQLインジェクションに加えて、最後にfirst_name,余分な無効なクエリ,、posts配列キーで使用される定数、requestとpostの組み合わせ、コードの大きなhtmlブロック、渡された値の有効性のチェックがありません。

有効な値を確認すると、スクリプトがメールを続行してデータベースパーツを更新する必要があるかどうかを判断できます。

これがあなたのコードのクリーンアップであり、それが役立つことを願っています:

<?php 

$to = 'newreg@41q.org';
$subject = 'New Homeless Connection';

if($_SERVER['REQUEST_METHOD']=='POST'){

    if(isset($_POST['first_name']) && strlen($_POST['first_name'])>1){
        $first_name=$_POST['first_name'];
    }

    if(isset($_POST['reg']) && strlen($_POST['reg'])>1){
        $reg=$_POST['reg'];
    }

    if(isset($_POST['connect_date']) && strlen($_POST['connect_date'])>1){
        $connect_date=$_POST['connect_date'];
    }

    if(!isset($first_name) || !isset($reg) || !isset($connect_date)){
        header("location: reg_add_fail.php");
        exit();
    }
}else{
//the page the post from
header("location: reg_form.php");
exit();
}

$msg=<<<EMAIL
<html>
<head>
<title>New Homeless Connection</title>
</head>

<body>
<table cellspacing="0" cellpadding="10" border="1" align="left">
<tr>
<td align="left" width="150px">Registery No.:</td>
<td align="left">$reg</td>
</tr>
<tr>
<td align="left">First Name:</td>
<td align="left">$first_name </td>
</tr>
<tr>
<td align="left">Connection Date:</td>
<td align="left">$connect_date</td>
</tr>
 <tr>
<td align="left" colspan="2">http://www.41q.org/admin/</td>
</tr>
</table>
<br>
<br>
</body>
</html>
EMAIL;

// Make sure to escape quotes
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Homeless' . "\r\n";

mail($to, $subject, $msg, $headers);

date_default_timezone_set('America/Los_Angeles');
$submit_date = date("m/d/y g:i A") ;

$order = "INSERT INTO reg_add (submit_date,connect_date, reg, first_name)
          VALUES ('{$submit_date}',".mysql_real_escape_string($connect_date)."','".mysql_real_escape_string($reg)."nv','".mysql_real_escape_string($first_name)."')";

$result = mysql_query($order);

header("Location: ./reg_add_success.php?reg=".urlencode($reg)."&first_name=".urlencode($first_name));
die;
?>
于 2012-04-28T05:14:08.410 に答える