I've been following a series of videos on Udemy to learn how to code a website from scratch and I got a problem: I tried to add a new post and I got the error "Something went wrong. Try again.", then I figured out that I had to change 'image' on lines 8 and 9 (of AddNwePost.php) to 'Image'. It worked perfectly and I created a first post, but when I tried to do it again on the second post, I got the same error on my screen and when I checked on database, only the first post was registered. Now I can't add any new posts on Database.
Here are the codes:
AddNewPost.php
<?php require_once("includes/DB.php"); ?>
<?php require_once("includes/Functions.php"); ?>
<?php require_once("includes/Sessions.php"); ?>
<?php
if(isset($_POST["Submit"])){
$PostTitle = $_POST["PostTitle"];
$Category = $_POST["Category"];
$Image = $_FILES["Image"]["name"];
$Target = "uploads/".basename($_FILES["Image"]["name"]);
$PostText = $_POST["PostDescription"];
$Admin = "Guilherme";
date_default_timezone_set("America/Los_Angeles");
$CurrentTime=time();
$DateTime=strftime("%B-%d-%Y %H:%M:%S",$CurrentTime);
if(empty($PostTitle)){
$_SESSION["ErrorMessage"]= "The title must not be empty.";
Redirect_to("AddNewPost.php");
}elseif (strlen($PostTitle)<=5) {
$_SESSION["ErrorMessage"]= "The post title must be greater than 5 characters.";
Redirect_to("AddNewPost.php");
}elseif (strlen($PostText)>10000) {
$_SESSION["ErrorMessage"]= "The post description is limited to 10000 characters.";
Redirect_to("AddNewPost.php");
}else{
// Query to insert the posts in DB When everything is fine
global $ConnectingDB;
$sql = "INSERT INTO posts(datetime,title,category,author,image,post)";
$sql .= "VALUES(:dateTime,:postTitle,:categoryName,:adminName,:imageName,:postDescription)";
$stmt = $ConnectingDB->prepare($sql);
$stmt->bindValue(':dateTime',$DateTime);
$stmt->bindValue(':postTitle',$PostTitle);
$stmt->bindValue(':categoryName',$Category);
$stmt->bindValue(':adminName',$Admin);
$stmt->bindValue(':imageName',$Image);
$stmt->bindValue(':postDescription',$PostText);
$Execute=$stmt->execute();
move_uploaded_file($_FILES["Image"]["tmp_name"],$Target);
if($Execute){
$_SESSION["SuccessMessage"]="Post with id:" .$ConnectingDB->lastInsertId()." added successfully!";
Redirect_to("AddNewPost.php");
}else {
$_SESSION["ErrorMessage"]= "Something went wrong. Try again.";
Redirect_to("AddNewPost.php");
}
}
}
?>
DB.php
<?php
$DSN='mysql:host = localhost; dbname=everybody_blog';
$ConnectingDB = new PDO($DSN,'root','');
?>