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

时间:2017-05-13 21:33:11

标签: 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块中的代码不起作用。

以下是代码:

<?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"; ?>

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

2 个答案:

答案 0 :(得分:0)

$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())";

你忘记了&#39;&#39;在这里:{$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())";

答案 1 :(得分:0)

首先,您应该尽可能以最简单的方式执行查询。尝试创建另一个PHP脚本,以测试该查询是否实际工作,并列出您在丑陋的HTML表中获得的数据。然后用这个替换你的查询:  $ query =&#34; SELECT * FROM posts WHERE post_id = 1&#34 ;; 注意:您必须确保实际上有一个ID为&#34; 1&#34;的帖子。 如果这有效,则意味着您可以从数据库中获得数据,所以您应该确保正确地从scource php页面传递变量(在您的情况下是表单。) 您也可以尝试vardump $ post_id来查看它的价值。 我希望这会有所帮助:)