<?php
session_start();
include 'connect.php';
/*
CREATE TABLE IF NOT EXISTS `names` (
  `id` int(5) NOT NULL auto_increment,
  `names` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
)
*/?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<password>names CMS</password>
</head>
<body>
<?php
/*
This is our table structure!!!! 

*/
//VARIABLES AT TOP!!!!!
// cleans data (single quotes and the like!
$submit = $_POST['submit'];


$task = $_POST['update'];
$editid = $_GET['editid'];
$id = $_GET['deleteid'];//for deleting
$names = $_POST['names'];
$status = 'Insert Names!';//Default status. Will change with editid

//This is your delete block!!!!
if ($id) { 
$sth = $dbh->prepare("delete from names where id = ?");
$sth->bindValue(1,$id);
$sth->execute();
    }

//Update mode 
if ($task == 'yes')//check the hidden field named update in the form!! 
   {
   }
	   
if ($editid) //sole purpose is to populate the data in the form! 
     {
$upsql = $dbh->prepare("select names from names where id = ?");
$upsql->bindValue(1,$editid);
$upsql->execute();
$uprow = $upsql->fetch();
$upnames = $uprow['names'];

$status = 'Edit '.$upnames;		//changes the submit button value
$up = 'yes'; //for the hidden field that says we're updating!
	 }

//This will be your insert block!  

	   
if ($submit == 'Insert Names!')
  {

$inssql = $dbh->prepare("insert into names (names) values (?)");
$inssql->execute(array($names));
if (!$inssql) print_r($dbh->errorInfo());	

  }

echo 'Welcome, '.$_SESSION['ughgughuoiu'].' <a href = "logout.php">Log out</a><br/>';


$caylie = $dbh->prepare("select id,names from names order by names asc");


$caylie->execute();
if (!$caylie) {
echo  "\nPDO::errorInfo():\n";
print_r($dbh->errorInfo());	
}
while ($row = $caylie->fetch())
{
//this changes on every turn of the loop to a new record! 
$id = $row['id'];
//output below! Note the strong html tags
$names =  $row['names'];


echo $names.' <a href = "namescms.php?deleteid='.$id.'" onClick="return confirm(\'Are you sure?\');">Delete</a> <a href = "namescms.php?editid='.$id.'">Edit</a><br/><br/><br/>';//output
}
?>
<form action = "namescms.php?editid=<?= $editid;?>" method = "post" enctype="multipart/form-data">
names: <input type="text" name = "names" value = "<? echo $upnames;?>" /><br/> 
<input type = "hidden" name = "update" value = "<?= $up;?>"/>
<input type = "submit" name = "submit" value = "<? echo $status;?>"/>
</form>
<br/>
<?
if ($editid) echo '<a href = "namescms.php">Return to Add Mode</a> ';
?>

</body>
</html>
