数据未插入数据库(PHP)

数据未插入数据库(PHP),php,mysql,pdo,Php,Mysql,Pdo,我一直在关注Udemy上的一系列视频,学习如何从头开始编写网站代码,但我遇到了一个问题:我试图添加一篇新帖子,但出现了错误“出了问题,请再试一次”。然后我发现我必须将第8行和第9行(addnWebPost.php)上的“image”改为“image”。它工作得很好,我创建了第一篇文章,但当我在第二篇文章中再次尝试这样做时,我在屏幕上出现了相同的错误,当我检查数据库时,只注册了第一篇文章。现在我无法在数据库中添加任何新帖子 代码如下: AddNewPost.php <?php require

我一直在关注Udemy上的一系列视频,学习如何从头开始编写网站代码,但我遇到了一个问题:我试图添加一篇新帖子,但出现了错误“出了问题,请再试一次”。然后我发现我必须将第8行和第9行(addnWebPost.php)上的“image”改为“image”。它工作得很好,我创建了第一篇文章,但当我在第二篇文章中再次尝试这样做时,我在屏幕上出现了相同的错误,当我检查数据库时,只注册了第一篇文章。现在我无法在数据库中添加任何新帖子

代码如下:

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");
    }
  }
}
?>
<?php
$DSN='mysql:host = localhost; dbname=everybody_blog';
$ConnectingDB = new PDO($DSN,'root','');
?>

DB.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");
    }
  }
}
?>
<?php
$DSN='mysql:host = localhost; dbname=everybody_blog';
$ConnectingDB = new PDO($DSN,'root','');
?>


咨询,这样您就可以知道您遇到了什么错误。您也不需要所有的
?>我发现问题一定出在我的数据库上,因为我删除了唯一的一行并尝试添加一篇新文章,结果成功了,但当我再次尝试第二篇文章时,我收到了相同的错误消息。也许您对某些列有完整性约束?错误报告将告诉您该问题。