<?php
error_reporting(0);
$dbh = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$fullname = $_POST['fullname'];
$deleteid = $_GET['deleteid'];
$idtoupdate = $_POST['idtoupdate'];
$editid = $_GET['editid'];


if ($_POST['submit'] == 'Submit'){
    //insert data!!!!
    if (!$idtoupdate){
  $inssql = $dbh->prepare("insert into contacts (fullname) values ('$fullname')");
  $inssql->execute(); 
    }
  // print_r($inssql->errorInfo()); <!-- use this when your stuff doesn't work.-->
  
}

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

if (isset($idtoupdate)){
$upsql = $dbh->prepare("update contacts set fullname = '$fullname' where id = '$idtoupdate'");
$upsql->execute();    
}

//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()){
    echo $row['fullname'].'  <a href="crud.php?deleteid='.$row['id'].'" onclick="return confirm(\'Are you sure you want to delete '.$row['fullname'].'\')" >Delete</a> <a href ="crud.php?editid='.$row['id'].'">Edit</a><br>';
}

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

?>

<form action ="crud.php" method="post">
<input type="text" name="fullname"  required placeholder="Full Name Here" value="<?php echo $upfullname;?>"/>
<input type="hidden" name="idtoupdate" value="<?php echo $editid;?>"/>    
<input type="submit" name="submit" value="Submit"/>    
</form>




