gpt4 book ai didi

php - 引用 MySQL 元素时 PHP 中 undefined variable

转载 作者:行者123 更新时间:2023-11-29 09:12:37 25 4
gpt4 key购买 nike

编辑:我解决了问题!这是一个与我发布的代码无关的问题 - 我在脚本中有一个退出命令 - 但您的所有建议仍然在其他方面有所帮助。

当用户在体育网站上填写选择时,我尝试自动向他们发送电子邮件。脚本的早期部分有效:他们的选择已正确插入或更新到数据库中。当我尝试从 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);

这个错误对我来说完全是神秘的。我希望至少有某种错误消息可以处理。谁能看出出了什么问题吗?

最佳答案

它应该是一个注释,但为了格式化。
你的错误处理方式很不寻常。
如果你真的想使用异常,应该以不同的方式完成:一个 try block 和多个 throws:

  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;
}
?>

关于php - 引用 MySQL 元素时 PHP 中 undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5137932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com