编辑:我解决了这个问题!这是一个与我发布的代码无关的问题 - 我在脚本中有一个退出命令 - 但是你的所有建议在其他方面仍有帮助。
我正在尝试在用户在体育网站上填写他们的选择时自动向用户发送电子邮件。脚本的早期部分有效:它们的选择在数据库中正确插入或更新。当我尝试从MySQL数据库中的表中提取用户的电子邮件地址并使用它向其发送消息时,脚本中断。但是这个错误很奇怪的是它不会导致任何错误消息,并且由于某种原因阻止某些echo语句在允许其他情况下运行。
以下是相关代码:
...
//set variable for the userID, grabbed from the session array
$userID = $_SESSION['identifier'];
...
//write query to get user's e-mail from the database
$getEmail = "SELECT `email` FROM `useraccounts` WHERE `userID` = '".$userID."'";
//execute query
$result = $db->query($getEmail);
//check if query failed
try
{
if (!$result)
{
throw new customexception("Some kind of database problem occurred when trying to find your e-mail address.");
}
}
catch (customexception $e)
{
include 'error.html';
echo $e;
$db->close();
include 'footer.php';
exit;
}
//get the info from the row
$row = $result->fetch_assoc();
//check if function ran, catch exception if it failed
try
{
if ($row === false)
{
throw new customexception("Some kind of database problem occurred when trying to get your e-mail address from your user record in the database.");
}
}
catch (customexception $e)
{
include 'error.html';
echo $e;
$db->close();
include 'footer.php';
exit;
}
//set e-mail variable
$email = $row['email'];
//set up e-mail information to send a record of their picks to the user
$toAddress = "$email";
$subject = "Your Picks";
$fromAddress = "From: picks@mysite.com";
//take the info the user submitted, format it for the e-mail, and assign to variable $mailContent
//the $winner1, $spread1, etc. variables are defined earlier in the function, and were successfully submitted into the database
$mailContent = "You picked $winner1 to win by $spread1 points, $winner2 to win by $spread2 points, $winner3 to win by $spread3 points, $winner4 to win by $spread4 points, and $winner5 to win by $spread5 points. \n".
"You can change your picks at any time before 1:00pm EST, February 27, 2011. Just go back to the form on the game page and enter your new picks. Good luck!";
//use wordwrap to limit lines of $mailContent to 70 characters
$mailContent = wordwrap($mailContent, 70);
//send the e-mail
$isMailed = mail($toAddress, $subject, $mailContent, $fromAddress);
//debug: check if mail failed
if (!$isMailed)
{
echo "Mail failed.";
}
//debug: echo $email to see if there's anything in there
echo "<p>E-mail: $email</p>";
//debug: echo $toAddress to see if there's anything in there
echo "<p>To address: $toAddress</p>";
//if everything succeeded, write reply and close database
echo $reply;
$db->close();
?>
为了清楚起见,$ userID设置正确,因为他们的选择进入数据库就像他们应该的那样。代码中列出的所有异常都没有出现,这意味着查询似乎已成功运行。我通过从PHP代码复制并直接在MySQL数据库上运行它来再次检查查询。当它直接运行时,它为我输入的每个userID值找到了正确的电子邮件地址。
但邮件永远不会被传递,当我尝试回显$ email和$ toAddress变量时,看看它们是否为空:
//debug: echo $email to see if there's anything in there
echo "<p>E-mail: $email</p>";
//debug: echo $toAddress to see if there's anything in there
echo "<p>To address: $toAddress</p>";
......没有任何表现。甚至没有错误消息。这并不一定意味着变量是空的:甚至不回显标签。
我还尝试使用我的个人电子邮件硬编码代码而不是$ toAddress,并且没有发送邮件。所以邮件功能无效。
我还应该注意到,脚本仍然成功地回应了$ reply(这是一个很早就定义的字符串)。
真正奇怪的是,我网站的登录脚本使用了几乎完全相同的代码并且运行良好:
$getuserID = "SELECT `userID` FROM `useraccounts` WHERE `u_name` = '".$login."' AND `p_word` = SHA1('".$password."')";
$result = $db->query($getuserID);
//check if query ran, catch exception if it failed
try
{
if ($result === false)
{
throw new customexception("Some kind of database problem occurred when trying to find your user ID.");
}
}
catch (customexception $e)
{
include 'error.html';
echo $e;
$db->close();
include 'footer.php';
exit;
}
//get the info from the row
$row = $result->fetch_assoc();
//check if function ran, catch exception if it failed
try
{
if ($row === false)
{
throw new customexception("Some kind of database problem occurred when trying to get info from your user record in the database.");
}
}
catch (customexception $e)
{
include 'error.html';
echo $e;
$db->close();
include 'footer.php';
exit;
}
//set userID variable
$userID = $row['userID'];
//assign the session identifier and include successfullogin.html if all is well
$_SESSION['identifier'] = $userID;
我以前每次收到新用户时都会向注册脚本发送电子邮件,所以我知道mail()通常与我的托管服务提供商一起工作:
//set up static e-mail information
$toAddress = "myemail@mysite.com";
$subject = "Advance Sign-Up";
$mailContent = "Name: $firstName $lastName \n".
"Username: $username \n".
"Password: $password \n".
"E-mail: $email \n".
"Country: $country \n".
"State: $state \n".
"City: $city \n".
"ZIP: $zip \n";
$fromAddress = "From: $email";
...
mail($toAddress, $subject, $mailContent, $fromAddress);
这个错误对我来说完全不可思议。我希望至少可以使用某种错误信息。任何人都可以看到什么是错的吗?
答案 0 :(得分:2)
它应该是一个评论,但为了格式化。
你的错误处理方式很不寻常
如果你真的想要使用异常,它应该以不同的方式完成:一次尝试阻止和多次抛出:
try
{
$getEmail = "SELECT `email` FROM `useraccounts` WHERE `userID` = '".$userID."'";
$result = $db->query($getEmail);
if (!$result)
{
throw new customexception("Some kind of database problem occurred when trying to find your e-mail address.");
}
$row = $result->fetch_assoc();
if ($row === false)
{
throw new customexception("Some kind of database problem occurred when trying to get your e-mail address from your user record in the database.");
}
$email = $row['email'];
$toAddress = "$email";
$subject = "Your Picks";
$fromAddress = "From: picks@mysite.com";
$mailContent = "yadda yadda yadda";
$mailContent = wordwrap($mailContent, 70);
mail($toAddress, $subject, $mailContent, $fromAddress);
}
catch (customexception $e)
{
include 'error.html';
echo $e;
$db->close();
include 'footer.php';
exit;
}
?>
答案 1 :(得分:0)
你肯定数据库变量有内容吗?我使用echo(或print)来快速确保变量不为空。你肯定你的电子邮件代码有效吗?尝试使用设定值(例如您自己的个人电子邮件)以确保其有效。
答案 2 :(得分:0)
忽略此类通知的最佳方法是确保变量存在或在普通PHP中使用isset(),如果!isset()抛出异常/错误并正确处理它。