简体   繁体   English

将电子邮件发送到从数据库检索的电子邮件地址中,该会话由PHP中的会话发起

[英]Sending email to email address retrieved from database Initiated by session in PHP

I'm attempting to create a system when you initiate a session an email is then sent to the email the user created the account with. 当您启动会话时,我正在尝试创建一个系统,然后将电子邮件发送到用户创建帐户的电子邮件。 I'm having an error which i've tried to solve, but I'm unable to. 我遇到了尝试解决的错误,但无法解决。 Here is 这是

the error: 错误:

Notice: Array to string conversion in C:\\xampp\\htdocs\\LoginSystem\\index.php on line 37 注意:第37行的C:\\ xampp \\ htdocs \\ LoginSystem \\ index.php中的数组到字符串的转换

Fatal error: Call to undefined method PDOStatement::fetch_assoc() in C:\\xampp\\htdocs\\LoginSystem\\index.php on line 42 致命错误:在第42行的C:\\ xampp \\ htdocs \\ LoginSystem \\ index.php中调用未定义的方法PDOStatement :: fetch_assoc()

I'm very new to php and have been using snippets but obviously not applying them correctly. 我对php很陌生,一直在使用代码段,但显然不能正确应用它们。

Any help is appreciated, Thanks in advance. 任何帮助表示赞赏,在此先感谢。

<?php

require 'assests/database.php';

session_start();


?>

<!DOCTYPE html>
<html>
<head>
<title>Welcome to your web app</title>
<link rel="stylesheet" stype="text/css" href="assests/style.css">
<link href="https://fonts.googleapis.com/css?family=Comfortaa" rel="stylesheet">
</head>
</head>



<body>


<div class="header">
  <a href="index.php"> Your App Name</a>
</div>  


<?php if( isset($_SESSION['user_id']) ): ?>



<?php



    $sql = 'select email from noodles_gamification where ID = "'.$_SESSION['user_id'].'"';

    $result = $conn->query($sql);
    $email = "";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $email = $row["email"];
    }


    /*
    $recipients = array();
    while($row = mysql_fetch_array($stmt)) {
        $recipients = $row[0]['email'];
    } */

    $to = $email;
    $subject = "E-mail subject";
    $body = "E-mail body";
    $headers = "From: noreply@prakashsoft.esy.es" ;

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








?>



<br /> Welcome you are succesfully loggin in!

<a href="assests/logout.php">Logout?</a>
<?php else : ?>



<h1>Please login or register</h1>
<a href="assests/login.php">login</a> or 
<a href="assests/register.php">Register</a>


<?php endif; ?>


</body>
</html>

Your first NOTICE message says, that you try to convert an array to string . 您的第一条NOTICE消息说,您尝试将array转换为string It seems that your session includes an array. 您的会话似乎包含一个数组。

The error message says, that PDOStatement::fetch_assoc() does not exist. 错误消息说, PDOStatement::fetch_assoc()不存在。 The method you were looking for is PDOStatement::fetch() , see http://php.net/manual/de/pdostatement.fetch.php 您正在寻找的方法是PDOStatement::fetch() ,请参见http://php.net/manual/de/pdostatement.fetch.php

As you only want to get one email address (I suppose the ID in your table is unique), you would not need any loop. 由于您只想获得一个电子邮件地址(我想表中的ID是唯一的),因此不需要任何循环。 You may therefore replace your while loop through: 因此,您可以通过以下方式替换while循环:

$row = $result->fetch(PDO::FETCH_ASSOC);
$email = $row["email"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM