duanduan1993 2019-03-06 09:39
浏览 34

为什么它说错了什么并且无法更新到mysql中的数据库?

I am new to PHP. When I create the below code following tutorial, I am puzzle why I can't update my data to database. As it shows below error "Something went wrong, please try again"

Can anyone help to see through my scripts on where did I go wrong? Or is there anyway that I can do step by step debug to find out which line goes wrong?

<?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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 Revit2020下载问题
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
    • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数