PHP - 数据的插入查询未到达数据库,但没有显示错误消息;似乎代码根本没有被读取/执行

标签 php mysql insert

更新:感谢大家迄今为止的帮助。我取得了一点进步。我从下面的帖子页面代码中删除了页脚,现在评论插入到表格中就可以了。我对其进行了多次测试,因此页脚中肯定有一些代码会干扰将记录插入评论表中。有谁知道它可能是什么?这是页脚代码:

<?php
$currentPage = basename($_SERVER['SCRIPT_FILENAME']);
?>  
<footer>

<?php if($currentPage == "post.php") { ?>
<!-- If the current page is post.php, add the author bio -->
  <section id="about">           
    <div class="container">
      <div class="row">
        <div class="col-sm-2 col-md-3"></div>
        <div class="col-sm-8 col-md-6">
          <div class="author">
            <img src="images/besh-small.jpg" class="img-responsive img-circle" alt="" height="80" width="80">
            <h3 class="authname"><?php echo $post_auth; ?></h3> 
            <p class="bio">
              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat
            </p>
          </div>
        </div>
        <div class="col-sm-2 col-md-3"></div>
      </div>
    </div>
  </section>

<?php } ?>

  <nav class="footer-nav">
    <?php if($currentPage == "post.php" || $currentPage == "category.php") { ?>

        <ul class="list-inline">
            <li><a href="index.php#bloglist">Back to Posts</a></li>
        </ul>

    <?php } else { ?>

        <ul class="list-inline">
          <li><a href="index.php" <?php if($currentPage == "index.php") {echo "id='active'";} ?>>Home</a></li>
          <li><a href="categories.php#categories" <?php if($currentPage == "categories.php") {echo "id='active'";} ?>>Categories</a></li>
          <li><a href="contact.php#contact" <?php if($currentPage == "contact.php") {echo "id='active'";} ?>>Contact</a></li>        
        </ul>

    <?php } ?>     
  </nav>
  <div class="copyright col-xs-12 text-center">
    <div class="container">
      &copy; 
        <?php
          $startYear = 2016;
          $thisYear = date('Y');
          if ($startYear == $thisYear) {
            echo $startYear;
          } else {
            echo "{$startYear} &#8211; {$thisYear}";
          }
        ?> 
        Beshara Saleh
    </div>
  </div>
</footer>

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->    
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script> <!-- scroll easing -->
<script src='http://npmcdn.com/isotope-layout@3/dist/isotope.pkgd.js'></script>
<script src="bower_components/isotope/dist/isotope.pkgd.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.2/js/bootstrapValidator.min.js">
</script>
<script src="js/index.js"></script>
<script src="js/custom.js"></script>


<script>
    // JS
</script>

</body>

</html>

第一次在这里发帖提问;预先感谢您对此的帮助。我搜索了类似的问题但找不到解决方案。我正在使用 PHP 和 MySQL(使用 phpMyAdmin)创建一个基本的博客引擎,以进行一些 PHP 练习。目前,还只是处于初步阶段。我没有优化查询,没有表连接,没有使用准备好的语句,也没有转义数据或设置任何形式的数据保护。我只是测试查询以确保它们到达数据库表。

问题:我在帖子页面添加了一个评论表单,该表单应将数据插入评论表中:

comments.php

当我从表单提交数据时,它不会将数据插入评论表中。但是,没有显示任何错误来帮助我找出解决方案。错误检查由我的confirmQuery函数处理:

function confirmQuery($result) {

    global $dbconnect;

    if(!$result) {        
        die("QUERY FAILED: " . mysqli_error($dbconnect));        
    }
}

此页面的设置方式是,当用户单击主页 (index.php) 中的帖子时,它会发送一个 GET 请求,将特定帖子的 post_id 传递到 post.php。 post_id 用于显示特定的帖子(这很好用)。我还使用 post_id 插入特定帖子的评论。

到目前为止,我创建的用于插入和检索数据的所有查询都运行良好。我的其他查询在访问数据库时都没有问题。

实际上看起来代码根本没有被读取/执行,例如如果我提交包含空字段的表单,则 else block 中应该显示错误消息的代码将不起作用。

这是代码:

<?php

include("includes/db.inc.php");
include("includes/util_funcs.inc.php");

// Check if the post_id parameter was received from the URL query string AND if it is numeric
if (isset($_GET["post_id"]) && is_numeric($_GET["post_id"])) {

// If so, we retrieve post_id value
$post_id = (int) $_GET["post_id"];

} else { 

// Otherwise, post_id field is set to 0
$post_id = 0;

}

// HEADER
include "includes/header.inc.php";

include "includes/breadcrumb.inc.php"; 

?>

<!-- PAGE CONTENT -->
<main class="page-content container">
<!-- POST -->
<section id="post" class="post">
<article>
  <div class="row">
    <div class="col-xs-12">
      <header class="post-header">
      <?php 
      // Define query for displaying the single post.
      // In insert_post.php, a numeric value is automatically inserted in the post_id field when we insert a post;
      // in index.php we make a GET request with the value of post_id, passing it through to post.php so that the value
      // that comes in through the URL is the same value that is in the post_id field
      $query = "SELECT * FROM posts WHERE post_id = $post_id";

      // Run the query
      $result = mysqli_query($dbconnect, $query);

      // Confirm query ran successfully
      confirmQuery($result);  

      while($row = mysqli_fetch_assoc($result)) {
        // $post_id      = $row['post_id'];
        $post_title   = $row['post_title'];
        $post_auth    = $row['post_auth'];
        $post_date    = $row['post_date'];
        $post_image   = $row['post_image'];
        $post_content = $row['post_content'];
        $post_tags    = $row['post_tags']; 
        $post_status  = $row['post_status'];                   

        // Break out of PHP to add the post template markup
      ?>
        <h2 class="post-title"><?php echo $post_title; ?></h2>
        <hr>
        <ul class="post-details list-inline">
          <li class="post-details-item">
            <span class="post-date"><?php echo $post_date; ?></span>
          </li>
          <li class="post-details-item">
            <i class="fa fa-heart-o"></i> 28              
            <i class="fa fa-comments-o"></i> <a href="#">15</a>
          </li>
          <li class="post-details-item">
            <span class="tag"><a href="categories.php#food">Food</a>, <a href="categories.php#art">Art</a>, <a href="#">Another Tag</a></span>
          </li>
        </ul><!-- /.post details -->
      </header>

      <img class="img-responsive" src="images/<?php echo $post_image; ?>" alt="Post Image">      
      <p class="lead">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>            
      <h2 class="post-heading">This is a Heading</h2>
      <p>

        <?php echo $post_content; ?>

      </p>

      <?php } ?> <!-- /while --> 

      <footer class="post-footer">
        <ul class="share list-inline">
          <li>
            <a class="btn btn-block btn-social btn-facebook">
              <span class="fa fa-facebook"></span> SHARE
            </a>
          </li>
          <li>
            <a class="btn btn-block btn-social btn-twitter">
              <span class="fa fa-twitter"></span> SHARE
            </a>
          </li>
          <li>
            <a class="btn btn-block btn-social btn-google">
              <span class="fa fa-google"></span> SHARE
            </a>
          </li>       
        </ul>
      </footer>           
    </div>
  </div>
</article>

<!-- POST COMMENTS -->    

<?php

// COMMENT FORM AND LIST
// include "includes/comments.inc.php";

// ********** TEST **********

// If the comment form is submitted
if(isset($_POST["insert_comment"])) {

    // Should already have the post_id from above, but accessing it here anyway
    $post_id = $_GET["post_id"];

    $comment_auth    = $_POST["comment_auth"];
    $comment_email   = $_POST["comment_email"];
    $comment_content = $_POST["comment_content"];

    // Validate the comment fields; otherwise when the form is submitted, it would
    // run the query even if the fields are empty.
    if(!empty($comment_auth) && !empty($comment_email) && !empty($comment_content) ) {

        // INSERT the field data into the comments table columns
        // (Note: Inserting $post_id into the comment_post_id field)
        $query = "INSERT INTO comments (comment_post_id, comment_auth, comment_email, comment_content, comment_status, comment_date) 
                 VALUES({$post_id}, '{$comment_auth}', '{$comment_email}', '{$comment_content}', 'unapproved', now())";

        $insert_comment = mysqli_query($dbconnect, $query);

        confirmQuery($insert_comment);

    } else {
        // If fields are empty, alert the user.
        // echo "<script>alert('Fields cannot be empty')</script>";
        // DOES NOT DISPLAY
        echo "<h1>Fields cannot be empty</h1>";
    }

}

?>      

<!-- ********** TEST ********** -->
<form action="" method="post" role="form">
    <div class="form-group">
        <label for="author">Name</label>
        <input name="comment_auth" type="text" class="form-control">
    </div>
    <div class="form-group">
        <label for="email">Email</label>
        <input name="comment_email" type="email" class="form-control">
    </div>
    <div class="form-group">
        <label for="comment">Your Comment</label>
        <textarea name="comment_content" class="form-control" rows="3"></textarea>
    </div>     

    <!-- Test -->
<!--        <div class="form-group"> -->
        <input name="insert_comment" class="btn standard-btn" type="submit" value="Leave a comment"> 
        <!-- <button type="submit" name="insert_comment" class="btn btn-primary">Submit</button> -->
<!--        </div> -->
</form>

</section>
<hr>

<!-- RELATED POSTS -->
<?php include "includes/related_posts.inc.php"; ?>

</main>    
<hr>

<!-- FOOTER -->
<?php include "includes/footer.inc.php"; ?>

我希望我已经清楚地解释了这个问题。 再次感谢您的帮助。

最佳答案

$query = "INSERT INTO comments (comment_post_id, comment_auth, comment_email, comment_content, comment_status, comment_date) 
             VALUES({$post_id}, '{$comment_auth}', '{$comment_email}', '{$comment_content}', 'unapproved', now())";

您忘记了这里的 '':{$post_id} 试试这个:

 $query = "INSERT INTO comments (comment_post_id, comment_auth, comment_email, comment_content, comment_status, comment_date) 
             VALUES('{$post_id}', '{$comment_auth}', '{$comment_email}', '{$comment_content}', 'unapproved', now())";

关于PHP - 数据的插入查询未到达数据库,但没有显示错误消息;似乎代码根本没有被读取/执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43958199/

相关文章:

javascript - 为什么 php 变量不能与 javascript 循环一起使用?

php - PHP 的 Kafka 客户端

php - 如何比较不同列中同一表中的值

Mysql STR_TO_DATE 在不同环境中给出不一致的结果

mysql - 为什么我无法将数据插入数据库

.net - PetaPoco 插入物 - 最快的方法?

php - 如何发布两个不均匀的数组,其中一组是复选框,另一组是文本输入框

php - 从 PHP 中提取的目录列表中的 jQuery Prop 父复选框

python - Django 休息框架从另一个表导入数据?

mysql - 使用 ENUM 字段复制 mysql 表