<?php
//$dbh = new PDO("mysql:host=localhost;dbname=webd153", "root", "");
$dbh = new PDO("mysql:host=localhost;dbname=webd153_01", "phpstu01", "creP7the");




$delete_button_id = $_GET['delete_button_id'];
$update_button_id = $_GET['update_button_id'];
$userid           = $_POST['userid'];


////////////////////////////////////////////////////////
//               Pagianting
////////////////////////////////////////////////////////

$numberpage = 3;

$countsql = $dbh->prepare("select COUNT(id) from ch09");
$countsql ->execute();

$row = $countsql->fetch();
$numrecords = $row[0];
$numlinks = ceil($numrecords/$numberpage);

//echo "Number of pagi links is ".$numlinks;
$page = $_GET['start'];
if (!$page) $page = 0;
$start = $page * $numberpage;
//echo "Start is ".$start.'<br>';


////////////////////////////////////////////////////////
//             Delete Records From Database
////////////////////////////////////////////////////////

if (isset ($delete_button_id)){
    $delsql = $dbh->prepare("delete from ch09 where id = '$delete_button_id'");
    $delsql->execute();
	echo "Record Deleted <br>";
}


////////////////////////////////////////////////////////
//            Update Records From Database
////////////////////////////////////////////////////////
if (isset($userid)) {
     $upfirstname = $_POST['upfirstname'];
	 $uplastname  = $_POST['uplastname'];
 	 $upemail     = $_POST['upemail'];
 	 $uptelephone = $_POST['uptelephone'];
	 $upcomments  = $_POST['upcomments'];
	 	
     $upsql = $dbh->prepare("update ch09  set FirstName  = '$upfirstname',
	                                          LastName   = '$uplastname', 
											  Email      = '$upemail',
											  Phone      = '$uptelephone',
											  Comments   = '$upcomments'
							where id = '$userid'");
     $upsql->execute(); 
   	 print_r($upsql->errorInfo());
     echo "Record Updated! <br>";
}
////////////////////////////////////////////////////////
//             Display Records From Database
////////////////////////////////////////////////////////

   $sql = $dbh->prepare("select id,FirstName,LastName,Email,Phone,Comments from ch09 limit $start,$numberpage"); 
   $sql->execute();
//	print_r($sql->errorInfo());

while ($row = $sql->fetch()) {
  $id          = $row['id'];
  $firstname   = $row['FirstName']; 
  $lastname    = $row['LastName'];
  $email       = $row['Email'];
  $telephone   = $row['Phone'];
  $comments    = $row['Comments'];
  
  echo '<br> First Name: ' .$firstname . '<br> Last Name: ' .$lastname.'<br> Email: '. $email . '<br> Phone: ' . $telephone .'<br> Comments: ' . $comments . '<br>' .
  '<a href="ch10_hw.php?delete_button_id='.$id.'" 
   onclick="return confirm(\'Are you sure you want to delete First: '.$firstname.' Last Name: '.$lastname.' Id Record: '.$id.' ? \');">Press To Delete Record  </a>  <br>
   <a href="ch10_hw.php?update_button_id='.$id.'">Press To Update Record </a><br>';

 }

//////////////////////////////
// Paginating Counter Section            
/////////////////////////////
echo '<br> <br>';
for ($i=0; $i<$numlinks; $i++){
	$y = $i+1;
	echo '<a href="ch10_hw.php?start='.$i.'">'.$y.'</a> ';
}
	
	
///////////////////////////////////////////////////////////////////
// Display the selected Update Record (pdated Button is Pressed)
//////////////////////////////////////////////////////////////////
if (isset($update_button_id)){   
	$getsql = $dbh->prepare("select * from ch09 where id = '$update_button_id'");
	$getsql->execute();
	$row = $getsql->fetch();
  $id          = $row['id'];
  $firstname   = $row['FirstName']; 
  $lastname    = $row['LastName'];
  $email       = $row['Email'];
  $telephone   = $row['Phone'];
  $comments    = $row['Comments'];

echo '<br> <br> Please Update The Record';

?>
  <form action = "ch10_hw.php" method="post"> 
  <?php
  echo '<input type="text" name="upfirstname"  value="'.$firstname.'"><br>';
  echo '<input type="text" name="uplastname"   value="'.$lastname.'"><br>';
  echo '<input type="text" name="upemail"      value="'.$email.'"><br>';
  echo '<input type="text" name="uptelephone"  value="'.$telephone.'"><br>';
  echo '<input type="text" name="upcomments"   value="'.$comments.'"><br>';
  
  ?>
     <input type="hidden" name="userid" value="<?php echo $id; ?>">   
     <input type="submit" name="submit" value="Update Record ID <?php echo $id;?>">
</form>   
<?php

}
?>
