数据未插入数据库(PHP)[重复]

问题描述 投票:0回答:1

[我一直在Udemy上观看了一系列视频,以学习如何从头开始编写网站代码,但是我遇到了一个问题:我尝试添加新的帖子,但收到错误消息:“出了点问题。请重试。”,然后我发现必须将(AddNwePost.php的)第8行和第9行的'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");
    }
  }
}
?>

DB.php

<?php
$DSN='mysql:host = localhost; dbname=everybody_blog';
$ConnectingDB = new PDO($DSN,'root','');
?>
php mysql pdo
1个回答
-1
投票

前几天,我遇到了和您一样的问题。我要做的是:1.创建一个具有新名称但具有相同的结构和数据的新数据库/表。2.将具有相同名称的Database / table更改为新名称。这个对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.