i have a little problem with loop with my PHP code.
I selecting data from my SQL database.
The column with name ’email’ is looping correctly but when i click on button – send message to selected email which is locating in variable $to, the email is still only sent to the first record ([email protected]) from database.
<?php
$query = "select * from vodicakyksk_tbl_test";
$result = mysqli_query($link,$query);
if ($result-> num_rows > 0) {
while ($row = $result-> fetch_assoc()) {
echo '<form action="" method="POST">';
$images = $row['image_name'];
$images = explode(',',$images);
if(isset($_POST['buttonyes'])) {
$to = $row['email'];
$subject = 'Test subject';
$message = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Email title</title>
</head>
<body>
<h1></h1>
</body>
</html>
';
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=UTF-8';
$headers[] = 'To: Admin <[email protected]>';
$headers[] = 'From: ADMINISTRATOR <[email protected]>';
mail($to, $subject, $message, implode("rn", $headers));
}
echo '<div class="div">';
echo '<p class="first-item">Meno a priezvisko<br>'.$row['meno'].'</p>';
echo '<p class="first-item">Dátum narodenia<br>'.'<span>'.$row['den'].'.</span> '.'<span>'.$row['mesiac'].'.</span> '.'<span>'.$row['rok'].'</span>'.'</p>';
echo '<p class="first-item">Email<br>'.$row['email'].'</p>';
echo '<p class="skola">'.$row['skola'].'</p>';
echo '<p>'.$row['prva_oblast'].'</p>';
echo '<p>'.$row['druha_oblast'].'</p>';
echo '<p>'.$row['prvy_odkaz'].'</p>';
echo '<p>'.$row['druhy_odkaz'].'</p>';
echo '<p class="odkaz-div">'.$row['gdpr'].'</p>';
echo '<div class="image-holder">';
foreach($images AS $image){
echo '<img src="images/'.$image.'" alt='.$image.'>';
}
echo '</div>';
echo '<input type="submit" name="buttonyes" value="YES">';
echo '<input type="submit" name="buttonno" value="NO>';
}
echo '</form>';
}
?>
Can someone help me what I’m doing wrong?