当未插入记录(准备好的语句)时,为什么没有在PHP中出现错误?

时间:2019-03-30 20:21:52

标签: php

好的,这就是我要做的。我正在向数据库中插入一些数据。这是一个博客,我有诸如作者,标签,隐藏的元标签等字段。我已经尝试了好几个星期了。本质上,我正在使用CMS,似乎没有插入任何内容,但没有收到任何错误(即使在使用error_reporting( E_ALL );强制执行时也是如此)。据我所知,一切都以正确的顺序和正确的变量提交了。这是下面的代码,感谢您的帮助!

HTML:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Admin Panel</title>
    <link rel="stylesheet" href="../css/master.css">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tag-editor/1.0.20/jquery.tag-editor.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tag-editor/1.0.20/jquery.tag-editor.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js" type="text/javascript"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jodit/3.2.34/jodit.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/izitoast/1.4.0/css/iziToast.css">
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tag-editor/1.0.20/jquery.tag-editor.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jodit/3.2.34/jodit.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/izitoast/1.4.0/js/iziToast.min.js"></script>
  </head>
  <body>
    <div class="admin">
      <div class="a-padding"><li class="isni"><a href="#"><i class="fas fa-pen"></i> Write Posts</a></li></div>
      <div class="a-padding"><li class="isni"><a href="#"><i class="fas fa-file-alt"></i> View Posts</a></li></div>
      <div class="a-padding"><li class="isni"><a href="#"><i class="fas fa-bookmark"></i> Viewers</a></li></div>
      <div class="a-padding"><li class="isni"><a href="#"><i class="fas fa-plus"></i> Widget</a></li></div>
    </div>
    <div class="main-body">
      <h1>Start Writing</h1>
      <form method="post">
        <input type="text" name="title" placeholder="Title" class="form" required>
        <div class="form-padding"><input type="text" name="author" placeholder="Author" class="form" required></div>
        <div class="form-padding"><input type="text" name="imgurl" placeholder="IMG URL..." class="form"></div>
        <div class="form-padding"><input type="text" name="tags" required></div>
        <div class="form-padding"><input type="text" name="htags" required></div>
        <div class="form-padding"><textarea id="body" name="bodydata" required></textarea></div>
        <div class="spacer"><input type="checkbox" name="hpbox"> Make Highlight</div>
        <input type="submit" name="post" class="form">
      </form>
      <!-- This is where the PHP lies -->
    </div>

    <script>
      $(document).ready(function() {
        var editor = new Jodit("#body", {
          "uploader": {
            "insertImageAsBase64URI": true
          }
        });

      });

      $('input[name="tags"]').tagEditor({
        placeholder: "Meta Tags",
        animateDelete: 100
      });

      $('input[name="htags"]').tagEditor({
        placeholder: "Hidden Meta Tags",
        animateDelete: 100
      });
    </script>
  </body>
</html>

PHP:

// This is before the HTML
require '../imports/database.php'; 
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago'); 
// ------ This is the rest of it, placed where the comment is in the HTML section above

 if (isset($_POST["post"])) {
          if (isset($_POST["hpbox"])) {
            $title = $_POST["title"];
            $author = $_POST["author"];
            $imgurl = $_POST["imgurl"];
            $tags = $_POST["tags"];
            $htags = $_POST["htags"];
            $bd = $_POST["bodydata"];
            $date = date("D M d, Y");
            $time = date("h:i A");
            $p = "true";
            $harch_date = date("M Y");

            $pinsql = "UPDATE `posts` SET `hp`='false' WHERE `hp`='true'";

            if ($con->query($pinsql) === TRUE) {
              echo 
              '
              <script type="text/javascript">
                iziToast.show({
                  title: "Success!",
                  message: "Queried highlight",
                  backgroundColor: "#37c2dd"
                });
              </script>
              ';
            } else {
                echo "Error updating record: " . $con->error;
            }

            $stmt = $con->prepare("INSERT INTO `posts` (`title`, `author`, `image`, `bodydata`, `tags`, `htags`, `date`, `time`, `hp`, `arch_id`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            $stmt->bind_param("ssssssssss", $title, $author, $imgurl, $bd, $tags, $htags, $date, $time, $p, $harch_dat);

            $stmt->execute();
            $stmt->close();


            echo 
            '
            <script type="text/javascript">
            iziToast.show({
              title: "Success!",
              message: "Post inserted",
              backgroundColor: "#37c2dd"
            });
            </script>
            ';
          }else {

          $title = $_POST["title"];
          $author = $_POST["author"];
          $imgurl = $_POST["imgurl"];
          $tags = $_POST["tags"];
          $htags = $_POST["htags"];
          $bd = $_POST["bodydata"];
          $date = date("D M d, Y");
          $time = date("h:i A");
          $arch_date = date("M Y");
          $p = "false";

          $stmt = $con->prepare("INSERT INTO `posts` (`title`, `author`, `image`, `bodydata`, `tags`, `htags`, `date`, `time`, `hp`, `arch_id`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
          $stmt->bind_param("ssssssssss", $title, $author, $imgurl, $bd, $tags, $htags, $date, $time, $p, $arch_date);

          $stmt->execute();
          $stmt->close();
          $con->close();

          echo 
          '
          <script type="text/javascript">
          iziToast.show({
            title: "Success!",
            message: "Post inserted",
            backgroundColor: "#37c2dd"
          });
          </script>
          ';
        }
      }

注意:查看整个文件https://pastebin.com/xtmSGJRA

imports / database.php文件:

<?php
$con = new mysqli('localhost', 'root', '', 'WWDB');
?>

1 个答案:

答案 0 :(得分:0)

我将首先尝试隔离该错误。

我要求提供database.php文件,以检查您是否正在检查连接是否成功建立。你不是。您可以在database.php文件中使用以下顺序同时启用mysqli报告模式:

mysqli_report(MYSQLI_REPORT_STRICT);
try {
    $con = new mysqli('localhost', 'root', '', 'WWDB');

    if ($con->connect_error) {
        die('Connect Error (' . $con->connect_errno . ') ' . $con->connect_error);
    }
} catch (Exception $e) {
    echo 'ERROR:'.$e->getMessage();
} 

在此之后,如果它没有显示任何有价值的错误,请尝试将execute命令放入类似的try-catch块中。我猜您的数据库表结构中可能存在一个定义为整数或布尔值的字段,并且您正在向其传递字符串值,因此请尝试捕获该错误。

编辑:您没有提供更新查询是否有效的信息。如果您仅对准备好的语句有疑问,并且正常的查询有效,那么您需要尝试其他方法,那么这项工作实际上有效吗?

"UPDATE `posts` SET `hp`='false' WHERE `hp`='true'"