<?php
error_reporting(0);
$dbh = new PDO("mysql:host=localhost;dbname=test", "root", "");

$fullname = $_POST["fullname"];
$newupdateid = $_POST['updateid'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$comments = addslashes($_POST['comments']);  
$function = 'Enter';
//update
if ($_POST['submit'] == 'Update'){
    $updatesql = $dbh->prepare("update contacts set fullname = '$fullname', phone ='$phone', email = '$email', comments = '$comments' where id = $newupdateid");
    $updatesql->execute();
    print_r($updatesql->errorInfo());
    //print_r($updatesql->errorInfo());
} 


//insert
if ($_POST['submit'] == 'Enter'){
    $fullname = $_POST["fullname"];
    $phone = $_POST["phone"];
    $email = $_POST["email"];
    $comments = $_POST["comments"];
    $inssql = $dbh->prepare("insert into contacts (fullname, phone, email, lastmodified, comments) values ('$fullname', '$phone', '$email', now(), '$comments')");
    $inssql->execute();
    print_r($inssql->errorInfo());
} 
//delete
if ($_GET['deleteid']){
    $delid = $_GET['deleteid'];
    $delsql = $dbh->prepare("delete from contacts where id = '$delid'");
    $delsql->execute();
    echo $_GET['fullname']." Deleted";
}

//pagination needs to go here
$numperpage = 2; //this is how many records per page you want
$start = $numperpage * $_GET['start']; //we need to use the url window to see what link the user clicked on. That will populate our limit tag. 
//in order to find the number of links in our pagination, we need to find the total number of records and divide by the numperpage
$getrecords= $dbh->prepare("select count(id) from contacts"); //count is super handy for getting the number of records
$getrecords->execute(); //enacts the query
$getrow = $getrecords->fetch(); //this grabs the data
$num = $getrow[0]; //this extracts the number
$links = ceil($num/$numperpage); //this tells me the number of links i need

$prev = $_GET['start'] - 1;
$next = $_GET['start'] + 1;

$y=1;
//show prev (only if there is a prev ($start > 0))

//output links:
for ($i=0;$i<$links;$i++){
    if ($_GET['start'] == $i) $style= ' style="color:#ff0000;text-decoration:none"';
    else { $style = '';}
    echo '<a href="contactscms.php?start='.$i.'"'.$style.'">'.$y.'</a> ';
    $y++;
    //url will be ?start=$i
}


echo '<br>';
//echo "select * from contacts limit $start,$numperpage <br>";
//show contacts, give links for delete and update
$sql = $dbh->prepare("select * from contacts limit $start,$numperpage");
echo "select * from contacts limit $start,$numperpage";
echo '<br>';
$sql->execute();
while ($row = $sql->fetch()){
  $id = $row['id'];    
    $fullname = $row['fullname'];
 // echo $id.':'.$row['fullname'].' <a href="contactscms.php?deleteid='.$id.'&start='.$start.'" onclick= "return confirm(\'are you sure you want to delete '.$row['fullname'].'? \')">Delete</a> <a href="contactscms.php?updateid='.$id.'&start='.$start.'">Update</a><br>';
    
    //easier option
  echo "$id : $fullname 
  <a href='contactscms.php?deleteid=$id&start=$start'>Delete</a> 
  <a href='contactscms.php?updateid=$id&start=$start'>Update</a><br>";      
    
    
}

//populate value
if ($_GET['updateid']){
   $upid = $_GET['updateid'];
   $function = 'Update';    
   $upsql = $dbh->prepare("select fullname,phone from contacts where id = '$upid'");
   $upsql->execute();
   $uprow = $upsql->fetch();
   $upname = $uprow['fullname'];
   $upphone = $upphone['phone'];
   $email = $upemail['email'];
   $comments = $upcomments['comments'];
echo '<a href="contactscms.php">&lt;&lt;Return to add mode</a>';    
}


?>


<form action = "contactscms.php?start=<?php echo $start;?>" method="post">
Name: <input type="text" name="fullname" value="<?php echo $upname;?>" required ><br>
Phone: <input type="tel" name="phone" value="<?php echo $upphone;?>" required ><br>
Email: <input type="email" name="email" value="<?php echo $upemail;?>" required ><br>
Comments: <textarea name="comments"><?php echo $upcomments;?></textarea> <br>
<input type="hidden" name="updateid" value ='<?php echo $upid;?>'>   
<input type="submit" name="submit" value = "<?php echo $function;?>"> 
</form>
    