PHP, MYSQL Creating a blog comment system

887

I am currently working on developing a blogging system. For the most part I have the blog done, just creating the scripts to allow users to post comments to each blog. My PHP select code is getting an error along the lines of

check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC WHERE blogID = 6' at line 1.

The full code of my SQL statement is:

SELECT commentID, blogID FROM blog_comments   
ORDER BY commentID LIMIT 1 DESC WHERE blogID = '.$row['postID'];`

I am aware that this current statement is susceptible to SQL Injections, and have tried using tokens to ensure I am protected from that.

the$row['postiD'] is from a previous SQL statement that was ran to display the actual blog post. This is intended to go on the main page, where I don't need to display the actual comment text, but rather just the number of comments that are on that particular blog. I can post the full code if needed.

Okay, I updated my SQL statement and fixed that issue. However, the page is not displaying thecommentID number, and$e is not getting executed, nor do I get any errors in my apache2 log.

$query = "SELECT commentID, blogID FROM blog_comments WHERE blogID ':postid' ORDER BY commentID DESC LIMIT 1";
$query_params = array(':postid' => $row['postID']);
try {
    $stmt = $db->prepare($query);
    $result = $stmt->execute($query_params);
}
catch(PDOException $e)
{
    // dont echo $e on production site
    die($e->getMessage());
}
$rows = $stmt->fetchAll();
?>
<?php foreach($rows as $row): ?>
    <?php echo $row['commentID']; ?>
<?php endforeach; ?>
comments
926

Answer

Solution:

MoveWHERE case just after select:

'SELECT commentID, blogID FROM blog_comments 
 WHERE blogID = '.$row['postID'].' ORDER BY commentID DESC LIMIT 1'

To prevent SQL-injections usePDO and prepared statements : (http://php.net/manual/en/pdo.prepared-statements.php).

774

Answer

Solution:

You have written wrong query

'SELECT commentID, blogID FROM blog_comments WHERE blogID = '.$row['postID'].' ORDER BY commentID DESC LIMIT 1';
527

Answer

Solution:

You really need to learn how we createselect ,order, where and limit statement in SQL

Your query would be

"SELECT `commentID`, `blogID` FROM `blog_comments`  WHERE `blogID` = '".$row['postID']."' ORDER BY `commentID` DESC LIMIT 1" ;

Read Tutorial

Also read How can I prevent SQL-injection

491

Answer

Solution:

Your query elements sequence seems wrong , Please follow below query sequence :

'SELECT commentID, blogID FROM blog_comments WHERE blogID = '.$row['postID'].' ORDER BY commentID DESC LIMIT 1';

Require to set ORDER BY & LIMIT after WHERE clause.

464

Answer

Solution:

SELECT `commentID`, `blogID` FROM `blog_comments` WHERE blogID = $row['postID'] order by `commentID` DESC limit 1

People are also looking for solutions to the problem: php - Insert in one table and delete from another simultaneously

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.