<?
include '/home/mmaaaco/public_html/wdm3304materials/wdm3304week1/dbconnect.php';
/*
CREATE TABLE IF NOT EXISTS `3304gallcats` (
  `catid` int(5) NOT NULL auto_increment,
  `catname` varchar(255) NOT NULL,
  PRIMARY KEY  (`catid`)
);

CREATE TABLE IF NOT EXISTS `3304gallpix` (
  `id` int(10) NOT NULL auto_increment,
  `pictitle` varchar(255) NOT NULL,
  `picdesc` text NOT NULL,
  `catid` int(5) NOT NULL,  <-- how else will we code this?
  PRIMARY KEY  (`id`)
);
*/


?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Admin</title>
<style type="text/css">
#newcategory {display:none;}
</style>
<script type="text/javascript">
function checknew() {
var choice = document.getElementById("choice").value;
if (choice == 'new') {
document.getElementById("newcategory").style.display = 'block';
}
else {
document.getElementById("newcategory").style.display = 'none';
}
}
</script>
</head>
<body>
<?
$submit = $_POST['submit'];
if ($submit == 'Upload Pic'){
	$catid = $_POST['catid'];
	$catname = $_POST['catname'];
	
	//if new category created block: 
	if ($catname){  //new category created
	//insert block
	$sql = 	$dbh->prepare(" insert into 3304gallcats (catname) values (?)");
	$sql->bindValue(1, $catname);
	$sql->execute();
	//get last cat id block to put in pics table
	$getlast = $dbh->prepare("select catid from 3304gallcats order by catid desc limit 1");
    $getlast->execute();
	$getlastrow = $getlast->fetch();
	$catid = $getlastrow['catid'];	//this is the new id for the pics table
	}
	
$pictitle = $_POST['pictitle'];
$picdesc = $_POST['picdesc'];
//insert into pics table block
$eric = $dbh->prepare("insert into 3304gallpix (pictitle,picdesc,catid) values (?,?,?)");
$eric->execute(array($pictitle,$picdesc,$catid));

//get last pic id for picname block
$eric = $dbh->prepare("select id from 3304gallpix order by id desc limit 1");
    $eric->execute();
	$getlastrow = $eric->fetch();
	$picid = $getlastrow['id'];		
	$newpicname = $picid.'.jpg';  //new picname (4.jpg)
	
	//move file with new picname into images folder
$picfile = $_FILES['pic']['tmp_name'];
$success = move_uploaded_file($picfile,'../images/'.$newpicname);
if ($success) echo 'Yeah!';

//file to folder 	
	
	
}

//form with title,desc for pics w upload & drop down to select category>
?>
   <table style="width:400px;margin-left:auto;margin-right:auto">
<form action="index.php" method="post" enctype="multipart/form-data">
<tr><td colspan="2"><h2>Add a Picture</h2></td></tr>

<tr><td>
*Pic Title:</td><td> <input type = "text" name = "pictitle" required /></td></tr>
<tr><td>*Upload:</td><td><input type = "file" name = "pic" required/></td></tr>
<tr><td colspan="2">*Description</td></tr>
<tr><td colspan="2"><textarea name = "picdesc" required rows="10" cols="35"></textarea></td></tr>

<tr><td colspan="2">  
<!-- check to see if new category selected-->
<select name = "catid" id = "choice"  onchange="checknew();">
<option value="">Choose Below</option>';

<?
//populate dropdown with existing categories table
$sql = $dbh->prepare("select * from 3304gallcats");
$sql->execute();
while ($carolyn  = $sql->fetch()){
	 echo '<option value="'.$carolyn['catid'].'">'.$carolyn['catname'].'</option>';
}
?>
<option value="new">Choose new Category</option>
</select>
<br/>
<span id = "newcategory"><br/>Category Name:<input type = "text" name = "catname"/><br/></span>
</td></tr>
<tr><td></td><td><input type = "submit" name = "submit" value = "Upload Pic" /></td></tr>
 </form>
 </table>
</body>
</html>
