Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
引用MySQL元素时PHP中未定义的变量_Php_Mysql_String_Session Variables_Undefined - Fatal编程技术网

引用MySQL元素时PHP中未定义的变量

引用MySQL元素时PHP中未定义的变量,php,mysql,string,session-variables,undefined,Php,Mysql,String,Session Variables,Undefined,编辑:我解决了这个问题!这是一个与我发布的代码无关的问题——我在脚本中有一个退出命令——但您的所有建议在其他方面仍然有帮助 当用户在体育网站上填写他们的选择时,我试图自动向他们发送电子邮件。脚本的早期部分起作用:它们的选择被正确插入或更新到数据库中。当我试图从MySQL数据库中的表中提取用户的电子邮件地址并使用它向他们发送消息时,脚本中断。但这个bug非常奇怪的是,它不会导致任何错误消息,并且由于某些原因,它会阻止某些echo语句在允许其他语句的同时运行 以下是相关代码: 我以前每次有新用户

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

当用户在体育网站上填写他们的选择时,我试图自动向他们发送电子邮件。脚本的早期部分起作用:它们的选择被正确插入或更新到数据库中。当我试图从MySQL数据库中的表中提取用户的电子邮件地址并使用它向他们发送消息时,脚本中断。但这个bug非常奇怪的是,它不会导致任何错误消息,并且由于某些原因,它会阻止某些echo语句在允许其他语句的同时运行

以下是相关代码:

我以前每次有新用户时都会让注册脚本给我发送一封电子邮件,因此我知道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";


这只虫子让我完全迷惑了。我希望我至少能处理一些错误信息。有人知道怎么回事吗?

你确定数据库变量有内容吗?我使用echo(或print)快速确保变量不是空的。你确定你的电子邮件代码有效吗?请使用设置的值(例如您自己的个人电子邮件)进行尝试,以确保其正常工作。

它应该是一条注释,但出于格式的考虑。
您处理错误的方式很不寻常。
如果您确实想使用异常,则应采用不同的方式:一次尝试块和多次抛出:

  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中使用isset(),如果!isset()引发异常/错误并正确处理

如何确保
$login
$password
不易受到攻击?如果使用SQL,则不可能进行SQL注入攻击。我希望你在过滤这些变量方面做得很好。我使用real_escape_字符串来过滤它们,但我没有在帖子中包含这部分代码。问题中是否有实际的错误消息?不幸的是,我似乎忽略了它,没有错误消息。当我将他们的选择输入数据库时,脚本运行良好。当我试图获取他们的电子邮件地址并向他们发送一份副本时,它似乎崩溃了。尽管如此,它在结尾仍然与$reply相呼应。谢谢你的提示,我对编程相当陌生。当它捕捉到错误时,是打印回显所有错误还是仅回显最近的错误?@Swag-Only-one将抛出。这就是重点。要抛出的第一个异常会导致跳过
try
块中的其余代码。这个想法是,如果块中早期的代码失败了,那么在块中后期尝试代码也没有意义。哇,好吧,所以我用echo检查变量是否为空,这就引发了一些其他神秘的问题。我将编辑代码以包含回音,并说明出现了什么问题。这可能是一个与我最初想象的不同的问题。顺便说一句,我没有投你反对票。您的建议实际上有助于澄清问题所在。在项目的测试阶段,对我来说,回应和记录总是很有用的。
      //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>";
            $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;
        //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
  {
    $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;
  }
?>