UPDATE: Thanks everyone for help so far. I made a little progress. I removed the footer from the post page code below, and now the comments insert into the table just fine. I tested it out multiple times, so there is definitely some code in the footer that is interfering with inserting records into the comments table. Does anyone know what it might be? Here is the footer code:
<?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">
©
<?php
$startYear = 2016;
$thisYear = date('Y');
if ($startYear == $thisYear) {
echo $startYear;
} else {
echo "{$startYear} – {$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>
First time posting a question here; thanks in advance for help with this. I searched for similar questions but couldn’t find a solution. I’m creating a basic blog engine with PHP and MySQL (using phpMyAdmin) to get some practice with PHP. Right now, it is just in a preliminary phase. I haven’t optimized the queries, there is no joining of tables, I’m not using prepared statements, and I haven’t escaped data or set up any form of securing the data. I am just testing the queries to make sure they are reaching the database tables.
Problem: I added a comment form to my post page that should insert data into the comments table:
When I submit the data from the form, it does not insert data into the comments table. However, no error is displayed to help me figure out a solution. Error checking is handled by my confirmQuery function:
function confirmQuery($result) {
global $dbconnect;
if(!$result) {
die("QUERY FAILED: " . mysqli_error($dbconnect));
}
}
The way this page is set up is that when a user clicks on a post from the home page (index.php), it sends a GET request passing the post_id for the specific post to post.php. The post_id is used to display the specific post (this works fine). I am also using post_id for inserting comments for the specific post.
All the queries I have created for inserting and retrieving data have worked fine up until this point. None of my other queries have a problem reaching the database.
It actually seem like the code isn’t even being read/executed at all, e.g. if I submit the form with empty fields, the code in the else block that is supposed to display an error message doesn’t work.
Here is the code:
<?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"; ?>
I hope I have explained the problem clearly.
Thanks again for your help.
2
Answers
You forgot the ” here:
{$post_id}
try this:You Should first of all try to execute your query in the simplest way possible. Try to create another PHP script just to test if that query actually works and list the data that you get in an ugly HTML table for example. Then replace your query with this one :
$query = “SELECT * FROM posts WHERE post_id = 1”;
Note : You have to make sure that there IS actually a post with an id of “1”.
If that works it means that you can get your data just fine from the database, So you should make sure that you are passing the variables from the scource php page correctly (In your case the form.)
You can also try to vardump $post_id to see what is it’s value.
I hope this helps 🙂