0

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','');
?>
Funk Forty Niner
  • 73,764
  • 15
  • 63
  • 131
  • 1
    Consult https://stackoverflow.com/questions/15275689/error-checking-for-pdo-prepared-statements so you know what error you are getting. – user3783243 Feb 03 '20 at 00:54
  • You also don't need all the `?> – user3783243 Feb 03 '20 at 01:01
  • I figured out that the problem must be on my database, since I deleted the only row and tried adding a new post and it worked, but when I tried doing it again for the second post, I got the same error message. – Guillaume_96 Feb 04 '20 at 00:48
  • Maybe you have an integrity constraint on some columns? Error reporting will tell you the issue. – user3783243 Feb 04 '20 at 02:05

0 Answers0