为什么它说出了问题并且无法在mysql中更新到数据库?

时间:2019-03-06 09:39:51

标签: php database

我是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 = "Upload/".basename($_FILES["Image"]["name"]);
$PostText = $_POST["PostDescription"];
$Admin = "Sharon";
date_default_timezone_set("Asia/Singapore");
$CurrentTime=time();
$DateTime=strftime("%B-%d-%Y %H:%M:%S",$CurrentTime);


if(empty($PostTitle))
{
$_SESSION["ErrorMessage"] = "Title Can't be empty";
Redirect_to("AddNewPost.php");
} elseif (strlen($PostTitle)<5) {
    $_SESSION["ErrorMessage"] = "Post Title should be greater     
    than 5 characters";
    Redirect_to("AddNewPost.php");
} elseif (strlen($PostText)>999) {
$_SESSION["ErrorMessage"] = "Post Description should be less than 1000 
    characters";
    Redirect_to("AddNewPost.php");
    } else {
 // Query to insert Post 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); // - > means PDO object rotation
 $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 Added Successfully";
        Redirect_to("AddNewPost.php");

    } else {
        $_SESSION["ErrorMessage"]="Something went wrong, please 
           try again";
        Redirect_to("AddNewPost.php");
    }
   }
   } //Ending of Submit Button If- Condition
   ?>

  <!DOCTYPE>

   <html lang="en">

<head>
<link rel="stylesheet"   
  href="https://use.fontawesome.com/releases/v5.7.2/css/all.css"   
  integrity="sha384-
  fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr"  
 crossorigin="anonymous">
    <link rel="stylesheet" 



 href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/
  bootstrap.min.css"       integrity="sha384-  
  ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" 
 crossorigin="anonymous">
<link rel="stylesheet" href="css/Styles.css">

    <title>Categories</title>
</head>

<body>

    <div style="height:10px; Background:#27aae1;"></div>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container" ">
            <a href="#" class="navbar-brand"> Application   
 Department </a>
            <button class="navbar-toggler" data-
 toggle="collapse" data-target="#navbarcollapseCMS">
                <span class="navbar-toggler-
  icon"></span>
            </button>
            <div class="collapse navbar-collapse" 
  id="navbarcollapseCMS">

            <ul class="navbar-nav mr-auto">

                <li class="nav-item">
                    <a href="Main.php" class="nav-
  link"><i class="fas fa-home text-success"></i> Main page </a>
                </li>
                <li class="nav-item">
                    <a href="Post.php" class="nav-
  link"> Posts </a>
                </li>
                <li class="nav-item">
                    <a href="Categories.php" 

  class="nav-link"> Categories</a>
                </li>
                <li class="nav-item">
                    <a href="Admins.php" class="nav-       link"> Admin</a>
                </li>
                <li class="nav-item">
                    <a href="Comment.php"   
    class="nav-link"> Comments</a>
                </li>
            </ul>
                <ui class="navbar-nav ml-auto">
                    <li class="nav-item"><a  
  href="Logout.php" class="nav-link text-warning"><i class="fas fa-user-   
  times"></i> Logout</a></li>
            </ul>
        </div>
    </div>
</nav>
        <div style="height:10px; Background:#27aae1;"></div>
        <!--NAVBAR END-->
  <!--header-->
  <header class="bg-dark text-white py-3">
<div class="container">
    <div class="row">
     <div class="col-md-12">
        <p style="font-size:30px;"> <i class="fas fa-edit"   
 style="color:#27aae1;"></i> Add New Post </p>
    </div>
</div>
  </div>
 </header>
 <!--header end-->
  <!--Main Area -->
   <section class="container py-2 mb-4">
<div class="row">
    <div class="offset-lg-1 col-lg-10" style="min-height:420px;">
        <?php
        echo ErrorMessage();
        echo SuccessMessage();
        ?>
        <form class="" action="AddNewPost.php" method="post" 
  enctype="multipart/form-data">
                <div class="card bg-secondary text-   
  light mb-2">
                    <div class="card-body bg-dark">
                        <div class="form-group">

                                    <label for="title"> <span class="FieldInfo"> Post Title: </span></label>
                                    <input class="form-control" type="text" name="PostTitle" id="title" 
 placeholder="Type title here" value="">
                            </div>
                            <div 
class="form-group">

 <label   
 for="CategoryTitle"> <span class="FieldInfo"> Choose Category:  
 </span></label>

  <select  
   class="form-control" id="CategoryTitle" name="Category">

 <?php
                                //Fetching all the categories from category mysql_list_tables

  global $ConnectingDB;

 $sql = "SELECT id,title FROM category";

 $stmt = $ConnectingDB->query($sql);

  while ($DateRows = $stmt->fetch()) {
                                    $Id = $DateRows["id"];
                                    $CategoryName = $DateRows["title"];
                                 ?>

  <option> <?php echo $CategoryName; ?> </option>
                             <?php } ?>

</select>
</div>

0 个答案:

没有答案