Hi everyone, tried to display next and previous records using Next and Previous buttons but unable to do so. The record displayed is the current record whenever i click the Next or Previous buttons ie it doesn't move to the next or previous records. Below is the coding. Please advise. Thanks.

<form name="progress" id="progress" method="post" action=""> <td colspan="2"><input type="submit" name="prev" id="prev" value="Previous" class="btnSubmit"></td> <td colspan="2"><input type="submit" name="next" id="next" value="Next" class="btnSubmit"></td> </form>
<?php

  error_reporting(E_ALL ^ E_NOTICE);
                mysql_connect("localhost","user",""); 
                mysql_select_db("pq"); 



                $_SESSION['userid']; // it will print the userid value
                $_SESSION['username']; // it will print the userid value
                $_SESSION['usersecurity']; // it will print the userid value

                $picid = 0;  
                if(isset($_POST['next']))
                {

                   $picid=$picid++;
                   $que = mysql_query("select * from general,progress where picid ='$picid'");
                   $data = mysql_fetch_array();
                   $userid= $data["userid"]; 
                   $year= $data["year"];
                $kra= $data["kra"];
                $kpi= $data["kpi"];
                   $progress1= $data["progress1"]; 
                  $Rating1= $data["Rating1"];   
                $picid = $data["picid"];
                }
                if(isset($_POST['prev']))
                {

                   $picid=$picid--;
                   $que = mysql_query("select * from general,progress where picid ='$picid'");
                   $data = mysql_fetch_array();
                  $userid= $data["userid"]; 
                   $year= $data["year"];
                $kra= $data["kra"];
                $kpi= $data["kpi"];
                   $progress1= $data["progress1"]; 
                  $Rating1= $data["Rating1"];   
                $picid = $data["picid"];
                }
            ?>

Dani AI

Generated

The behavior comes down to three practical problems: the current record id is not preserved between requests, the increment/decrement logic in the posted code is ineffective, and nothing is actually rendered after the fetch (as noted). Switching away from the deprecated mysql_* API and avoiding assignments like $picid = $picid++ will eliminate two common sources of no-op behaviour. As suggested, use LIMIT-based queries (or explicit next/previous-id queries) rather than trying to rely on an in-memory counter that resets on each POST.

A robust pattern:

  • Persist the current picid across requests (hidden form field, query string, or session).
  • When “Next” is requested, query the database for the next picid: WHERE picid > :cur ORDER BY picid ASC LIMIT 1. For “Previous”, use < :cur ORDER BY picid DESC LIMIT 1.
  • Use prepared statements (PDO or mysqli) and cast picid to int to avoid injection.
  • Finally fetch the full record for that resolved picid and render the fields (escape before output).

Example (PDO) implementation:

<?php
$pdo = new PDO('mysql:host=localhost;dbname=pq;charset=utf8mb4','dbuser','dbpass',
    [PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);

$current = isset($_POST['picid']) ? (int)$_POST['picid'] :
           (int)$pdo->query("SELECT picid FROM progress ORDER BY picid ASC LIMIT 1")->fetchColumn();

if (isset($_POST['next'])) {
  $stmt = $pdo->prepare("SELECT picid FROM progress WHERE picid > ? ORDER BY picid ASC LIMIT 1");
  $stmt->execute([$current]); if ($val = $stmt->fetchColumn()) $current = (int)$val;
}
if (isset($_POST['prev'])) {
  $stmt = $pdo->prepare("SELECT picid FROM progress WHERE picid < ? ORDER BY picid DESC LIMIT 1");
  $stmt->execute([$current]); if ($val = $stmt->fetchColumn()) $current = (int)$val;
}

$stmt = $pdo->prepare("SELECT g.*, p.* FROM general g JOIN progress p ON g.userid = p.userid WHERE p.picid = ?");
$stmt->execute([$current]); $row = $stmt->fetch(PDO::FETCH_ASSOC);
// then output fields, e.g. echo htmlspecialchars($row['kra']);
?>

Quick checklist to resolve the original symptom: persist the id, remove = $picid++/= $picid-- patterns (use ++$picid or $picid += 1), use the next/previous-id queries shown above, and actually echo the fetched columns after the final SELECT.

Recommended Answers

All 2 Replies

For starters, I don't see any code where you are actually displaying something that you fetched from your database.

Sophia_1 If you want pagination or some thing else because your code not sutiable for pagination pre and next option only work through data come from data base limit option pass on sql query according to design whole arhitacture .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.