PHP, MySQL, - email isn't in loop

Viewed 59

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 (example@gmail.com) 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 <administrator@adm.com>';
            $headers[] = 'From: ADMINISTRATOR <info@adm.com>';

            mail($to, $subject, $message, implode("\r\n", $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>&nbsp;'.'<span>'.$row['mesiac'].'.</span>&nbsp;'.'<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?

0 Answers
Related