<?php
include '../connection.php';
$submit = $_POST['submit'];
if ($submit == 'Log In'){
	$username = $_POST['username'];
	$password = sha1($_POST['password']);
	//echo "Your username is ".$username.' and your password is  '.$password;
$sql = $dbh->prepare("select username from rec_admin where username = ? and password = ?");
$sql->bindValue(1,$username,PDO::PARAM_STR);
$sql->bindValue(2,$password,PDO::PARAM_STR);
$sql->execute();
//print_r($sql->errorInfo());
$sqlresults = $sql->fetch();
$uname = $sqlresults['username'];
if ($uname == $username){
	$_SESSION['admin'] = 'authorized';
	$_SESSION['username'] = $username;
}


}


if ($_SESSION['admin'] != 'authorized'){
?>
<form action ="index.php" method="post">
Username: <input type="text" name ="username"/><br>
Password: <input type="password" name="password"><br>
<input type="submit" name="submit" value="Log In">
</form>
<?php
}
else {
       if ($submit == 'Add Recipe'){
       	$title = $_POST['title'];
       	$content = $_POST['content'];
        $inssql = $dbh->prepare("insert into rec_recipes (recipetitle,recipedescription) values (?,?)");
        $inssql->bindValue(1,$title,PDO::PARAM_STR);
        $inssql->bindValue(2,$content,PDO::PARAM_STR);
        $inssql->execute();
        $lastid = $dbh->lastInsertId();
        $newpicname = $lastid.'.jpg';

        $bool = move_uploaded_file($_FILES['photo']['tmp_name'],'../images/'.$newpicname);
        if ($bool) echo 'Record inserted and pic uploaded!';

       }





		echo 'Welcome, '.$_SESSION['username'];
		echo '<br><a href="logout.php">Log Out</a>';
        ?>
        <form action="index.php" method="post" enctype="multipart/form-data">
        Title: <input type="text" name="title"><br>
        Content: <textarea name="content"></textarea><br>
        Add a photo (jpeg): <input type="file" name="photo"><br>
        <input type="submit" name="submit" value="Add Recipe">
        </form>
       <?php
}
?>

