0

I have a checkbox form which is sent to mail.php to be mailed. The selected items should be sent via mail. I have a query to output selected items. In mail body; to echo all selected items which pulled from database, i need to equal the while loop to a variable. But i can not manage it to equal.

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

include 'nedmin/netting/database.php';

if (isset($_POST['sendmail'])) {

$city=array_keys($_POST['cities']);

$cities=implode(",",$city);


$query = $db->prepare("SELECT * FROM semt WHERE semt_id IN('".$cities."')");

$query->execute();

while ($statement = $query->fetch(PDO::FETCH_ASSOC)) {

?>
   <td><?php echo $statement["city_name"]?></td>

<?php }


$mail = new PHPMailer;

try {
   /* SMTP parameters. */
   $mail->isSMTP();
   $mail->SMTPKeepAlive = TRUE;   
   $mail->SMTPAuth = TRUE;
   $mail->SMTPSecure = 'tls';

   ...
   ...
   ...
   $mail->isHTML(TRUE);
   $mail->Subject = "Subject";
   $mail->Body = "Selected cities: $......";

   $mail->send();
}
catch (Exception $e)
{
   echo $e->errorMessage();
}
catch (\Exception $e)
{
   echo $e->getMessage();
}
}
?>
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Don't replace your original code with the code from an answer. Readers need to see what the answers are fixing. – Barmar Jul 07 '19 at 11:07
  • `WHERE semt_id IN('".$cities."')` is not going to work as expected. See: [can-i-bind-an-array-to-an-in-condition](https://stackoverflow.com/questions/920353/can-i-bind-an-array-to-an-in-condition) – Paul Spiegel Jul 07 '19 at 12:16

2 Answers2

0

Concatenate to a string instead of printing output.

$city_names = "";
while ($statement = $query->fetch(PDO::FETCH_ASSOC) {
    $city_names .= "<td>{$statement["city_name"]}</td>\n";
}

Then you can use $city_names in the email.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0
$citiesName = [];

while ($statement = $query->fetch(PDO::FETCH_ASSOC)) { ?>
    <td><?php echo $statement["city_name"]?></td>
    <?php $citiesName[] = $statement["city_name"]?>
<?php }

//code ...
$mail->Body = "Selected cities: " . implode(',', $citiesName);
Mike Foxtech
  • 1,633
  • 1
  • 6
  • 7