<?php
//this is outputting errors if it is error_reporting(E_ALL)
error_reporting(0);
$dbh = new PDO('mysql:host=localhost;dbname=test', 'root', '');
//these are the data that comes from the form
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
//this means that the deleteid was clicked
$deleteid = $_GET['deleteid'];
//this gets the id to update from the hidden fields!!!
$idtoupdate = $_POST['idtoupdate'];
//this is if the edit is clicked!!! 
$editid = $_GET['editid'];

if ($_POST['submit'] == 'Submit'){
    //insert data!!!!
    if (!$idtoupdate){
        /*this could be 
        if (empty($idtoupdate))
        if (!isset($idtoupdate))
        if ($idtoupdate == '')
        */
        //if no idtoupdate
      $inssql = $dbh->prepare("insert into contacts (fullname,email,phone) values ('$fullname','$email','$phone')");   
        echo "Your record has been inserted!";
  
  $inssql->execute(); 
        header("location: crud.php?message=insert");
  
    }
  // print_r($inssql->errorInfo()); <!-- use this when your stuff doesn't work.-->
  
}

//delete
if ($deleteid){
    $delsql= $dbh->prepare("delete from contacts where id = '$deleteid'");
    $delsql->execute();
}

if ($idtoupdate){
    //IMPORTANT must have where statement of entire database will update to the new data!!!!
$upsql = $dbh->prepare("update contacts set fullname = '$fullname',email='$email',phone='$phone' where id = '$idtoupdate'");//update fullname
$upsql->execute();  //exit
}

//need to display our results so that we see what's working and what's in our database!!!
$selsql = $dbh->prepare("select id,fullname from contacts");
$selsql->execute(); 
while ($row = $selsql->fetch()){
    //outputs fullname
    echo $row['fullname'];
    //output links to delete with confirmation popup!
    echo '<a href="crud.php?deleteid='.$row['id'].'"';
    //this is the popup
    echo ' onclick="return confirm(\'Are you sure you want to delete '.$row['fullname'].'\')" ';
    //close the delete link
    echo '>Delete</a>';
    //now output a link that lets them edit with editid !!!!
    echo ' <a href ="crud.php?editid='.$row['id'].'">Edit</a><br>'; //delete fullname --------->
   }

if (isset($editid)){
    $getupsql = $dbh->prepare("select fullname,email,phone from contacts where id = '$editid'");
    $getupsql->execute();
    $row = $getupsql->fetch();
    $upfullname = $row['fullname'];
    $upemail = $row['email'];
    $upphone = $row['phone'];
}



?>

<form action ="contactcrud.php" method="post">
<input type="text" name="fullname"  required placeholder="Full Name Here" value="<?php echo $upfullname;?>"/><br>
<input type="text" name="email"  required placeholder="Your Email Here" value="<?php echo $upemail;?>"/><br>
<input type="text" name="phone"  required placeholder="Your Phone Here" value="<?php echo $upphone;?>"/><br>
<input type="hidden" name="idtoupdate" value="<?php echo $editid;?>"/>    
<input type="submit" name="submit" value="Submit"/>    
</form>