<?php
if(isset($_GET['addpoem'])){
	include 'form.html.php';
	exit();
}

if(isset($_POST['poemtext'])){
	include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
	try{
	$sql="insert into poem set 
	poemtext=:poemtext,
	poemdate=CURDATE()";
	$s=$pdo->prepare($sql);
	$s->bindValue(':poemtext',$_POST['poemtext']);
	$s->execute();
	}//try
	catch(PDOException $e){
		$error='Error adding submitted poem: '.$e->getMessage();
		include 'error.html.php';
		exit();
	}
	header('location:.');
	exit();
}

if(isset($_GET['deletepoem'])){
		include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
	try{
		$sql='delete from poem where id=:id';
		$s=$pdo->prepare($sql);
		$s->bindValue(':id',$_POST['id']);
		$s->execute();
	}
	catch(PDOException $e){
		$error='Error deleting poem: '.$e->getMessage();
		include 'error.html.php';
		exit();
	}
	header('location:.');
	exit();
}

	include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
try{
	$sql='select poem.id,poemtext,name,email
	from poem inner join sender
	on senderid=sender.id';
	$result=$pdo->query($sql);
}
catch(PDOException $e){
	$error='Error fetching poems:'.$e->getMessage();
	include 'error.html.php';
	exit();
}
while($row=$result->fetch()){
	$poems[]=array(
	'id'=>$row['id'],
	'text'=>$row['poemtext'],
	'name'=>$row['name'],
	'email'=>$row['email']);
}
include 'poems.html.php';




