<?php
error_reporting(0); // this will help with php syntax errors if 0 is E_ALL, notices will be given. 0 means turned off 
include 'dbconnect.php'; //this is literally like pasting the code from the dbconnect. $dbh = new PDO("mysql:host=localhost;dbname=mmaaaco_ksecor", "mmaaaco_ksecor", "hamilton"); THis creates a variable that holds the connection. 

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?
//insert from form
//these get data from the post method
$name = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$zipcode = $_POST['zipcode'];
//this gets the picture uploaded
$profilepic = $_FILES['profilepic']['tmp_name'];
echo 'Willow: '.print_r($_FILES['profilepic']);
//these are from the url
$deleteid = $_GET['deleteid'];
$editid = $_GET['editid'];
//this modifies the button 
$status = 'Add Record';
if (strlen($name) > 2 && !$editid) {
$insertsql = $dbh->prepare("insert into contacts (fullname,email,phone,zipcode) values (?,?,?,?)");
$insertsql->execute(array($name,$email,$phone,$zipcode));
echo '<br/>'.$name.' added!<br/>';
//need last id!
$getlast =  $dbh->prepare("select id from contacts order by id desc limit 1");
$getlast->execute();
$getrow = $getlast->fetch();
$lastid = $getrow[0];
if ($profilepic) {
move_uploaded_file($profilepic, 'profilepics/'.$lastid.'.jpg');
}

}

if ($editid != '' && strlen($name) > 3){
$upsql = $dbh->prepare("update contacts set fullname = ?, email = ?, phone = ? , zipcode =?  where id = ?");
if ($profilepic) {
move_uploaded_file($profilepic, 'profilepics/'.$editid.'.jpg');
}
$upsql->execute(array($name,$email,$phone,$zipcode,$editid));
echo '<br/>Record Updated!</br>';
}


if ($deleteid){
	$deletesql = $dbh->prepare("delete from contacts where id = ?"); 
		$deletesql->bindParam(1,$deleteid, PDO::PARAM_INT,5);
    $deletesql->execute();
	if ($deletesql) echo $deleteid.' has been deleted!<br/>' ;
}

if ($editid){
	$editsql = $dbh->prepare("select fullname,email,phone,zipcode from contacts where id = ?");
	$editsql->bindParam(1,$editid, PDO::PARAM_INT,5);
    $editsql->execute();
	$editrow = $editsql->fetch();
	$nametoedit = $editrow[0];
	$emailtoedit = $editrow[1];
	$phonetoedit = $editrow[2];
	$zipcodetoedit = $editrow[3];
	$status = 'Edit Record';
}





$selectsql = $dbh->prepare("select id,fullname from contacts");
$selectsql->execute();
while ($row = $selectsql->fetch()){
	echo '<a href = "bestpractice.php?id='.$row['id'].'">'.$row['fullname'].'</a>   <a href = "bestpractice.php?deleteid='.$row['id'].'" onclick="return confirm(\'Are you sure you want to delete '.$row['fullname'].'\');">Delete</a>         <a href = "bestpractice.php?editid='.$row['id'].'">Edit</a><br/>';
}

$dbh->null;

?>
<form action = "<?= $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];?>" method="post" enctype="multipart/form-data">
<?
if (file_exists('profilepics/'.$editid.'.jpg')){
	echo '<img src = "profilepics/'.$editid.'.jpg" height = "50" width= "50"/>';
}
?>
<input type = "file" name="profilepic" /><br/>
Name: <input type="text" name = "fullname" value = "<?= $nametoedit; ?>"><br/>
Email: <input type="text" name = "email" value = "<?= $emailtoedit; ?>"><br/>
Phone: <input type="text" name = "phone" value = "<?= $phonetoedit; ?>"><br/>
zipcode: <input type="text" name = "zipcode" value = "<?= $zipcodetoedit; ?>"><br/>
<input type = "submit" name = "submit" value = "<?= $status;?>"/>   <br/>
</form>
<?
if ($editid) echo '<br/><a href = "bestpractice.php">Back to add mode</a><br/>';
 
?>
</body>
</html>
