<?PHP
// addpost.php
// Inserts a post into the database
// include the form_data class
include("Scripts/FormData.class.php");
$formData = new reviews_Scripts_FormData(); // Create a new instance of the form_data class
// Get all inputs as array
$vars = array('title' => $_POST['title'],
'genre' => $_POST['genre'],
'esrb' => $_POST['esrb'],
'score' => $_POST['score'],
'platforms'=>$_POST['platforms'],
'review' => $_POST['review']);
// Create an array to hold user errors
$errors = array();
// Check for empty
$errors = $formData->checkEmpty($vars,$errors);
// Check for valid length
$errors = $formData->checkLength($vars['title'],1,400,$errors,"title"); // Check title length
$errors = $formData->checkLength($vars['genre'],1,400,$errors,"genre"); // Check genre length
$errors = $formData->checkLength($vars['esrb'],1,3,$errors,'esrb'); // Check esrb rating length
$errors = $formData->checkLength($vars['platforms'],1,500,$errors,'platforms');
// Replace enter presses with a \n so the database and showing thread formats it correctly
$pattern = "/ /";
$vars['review'] = preg_replace($pattern,"\n",$vars['review']);
$vars['review'] = nl2br($vars['review']);
$vars['review'] = stripslashes($vars['review']);
// If there were errors, format and display them
if ($errors) {
$formData->userErrors($errors);
} else {
try {
// insert into the database
$insertPost = $dbh->prepare("INSERT INTO reviews(id,title,genre,esrb,score,review,platform,date,user_ID)
VALUES(0,:title,:genre,:esrb,:score,:review,:platforms,:date,:user_ID)");
$insertPost->bindValue(":title",$vars['title']);
$insertPost->bindValue(":genre",$vars['genre']);
$insertPost->bindValue(":esrb",$vars['esrb']);
$insertPost->bindValue(":score",$vars['score']);
$insertPost->bindValue(":review",$vars['review']);
$insertPost->bindValue(":platforms",$vars['platforms']);
$insertPost->bindValue(":date",Date("Y-m-d"));
$insertPost->bindValue(":user_ID",getID($dbh,$userSession));
$insertPost->execute();
echo '<p>Your post was added to the database.</p>';
echo '<p><a href="Index.php">Go Home</a></p>';
} catch(PDOException $error) {
recordErrors("admin/addpost.php",$error->getMessage(),"Unable to add post to database",uniqid());
}
}