0

TYA , can someone help me , I want to show the id(PRIMARy/ AI) one at a time everytime I click the Next button. But in my code it just increment only and still show the previous number .

Here is the code:

<?php
    include 'dbh.php';

    $commentNewCount = $_POST['commentNewCount'];

    $sql = "SELECT * FROM comments LIMIT $commentNewCount";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            echo $row['id'];
        }
    } else {
        echo "There are number!";
    }
?>

Code in Js

$(document).ready(function() {
  var commentCount = 0;
  $("button").click(function() {
    commentCount = commentCount + 1;
    $("#comments").load("load-comments.php", {
      commentNewCount: commentCount
    });
  });
});
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Noel_Ed
  • 19
  • 6
  • If the button is in a form it needs `type="button"` too – mplungjan Feb 14 '18 at 10:36
  • 1
    **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Feb 14 '18 at 10:40

2 Answers2

0
$sql = "SELECT * FROM comments LIMIT 1 OFFSET $commentNewCount";

** SQL injection attacks

JOE LEE
  • 1,058
  • 1
  • 6
  • 6
0

Follow me with this :

If $commentNewCount = 5, your current query SELECT * FROM comments LIMIT 5 will return 5 rows.

But, what you want is only 1 row where the id = 5!

So, your query should be :

$sql = "SELECT * FROM comments WHERE id = ".$commentNewCount;
Hamza Abdaoui
  • 2,029
  • 4
  • 23
  • 36