<?php
if (isset($_POST['submit'])){
    $_POST['submit']();
}

function subscribe(){
$dbh= new PDO("mysql:host=localhost;dbname=test", 'root', '');
    
if ($_POST['submit'] ==  'subscribe'){
    $email = $_POST['email'];
     $checksql = $dbh->prepare("select count(id) from subscribers where email = '$email'");
    $checksql->execute();
    $row = $checksql->fetch();
    if ($row[0] > 0){
        echo "you are already subscribed<br>";
    }
    else {
    $ins = $dbh->prepare("insert into subscribers (email) values ('$email')"); 
     $ins->execute();
     echo "Thank you!";   
    }
    }
}

function unsubscribe(){
   $email = $_POST['email'];
  $dbh= new PDO("mysql:host=localhost;dbname=test", 'root', '');
   $delsql = $dbh->prepare("delete from subscribers where email = '$email' ");
   $delsql->execute();
   print_r($delsql->errorInfo());
    
}

?>


<form action="newsletter.php" method="post">
<input type="email" name="email" required>
<input type="submit" name="submit" value="subscribe">
<input type="submit" name="submit" value="unsubscribe">

</form> 