Hi everyone, am trying to display the next record with next button and previous record with previous button from 2 tables. But when i click next there is no record displayed eventhough there are records in the database. The same thing happens with previous button. Please advise. Thanks a lot.

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

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

    if(isset($_GET['picid']) && (int)$_GET['picid']) $picid = $_GET['picid']; 
    else $picid = 1; 

        $_SESSION['kra'];
        $_SESSION['kpi'];

    $query = "SELECT * from ipd where userid='".$_SESSION['userid']."' and picid= '$picid' LIMIT 1"; 
    $result = mysql_query($query); 
    if($num=mysql_numrows($result)) { 
    $record = mysql_fetch_assoc($result); 
        $userid= $record["userid"]; 
           $kra= $record["kra"];
        $kpi= $record["kpi"];
          $picid = $record["picid"];
    }

    $query = "SELECT * from progress WHERE picid= '$picid' and userid='". $_SESSION['userid']."' LIMIT 1"; 
    $result = mysql_query($query); 
    if($num=mysql_numrows($result)) { 
    $record = mysql_fetch_assoc($result); 
        $progress1= $record["progress1"]; 
         $progress2= $record["progress2"]; 
        $progress3= $record["progress3"]; 
        $progress4= $record["progress4"]; 
        $picid = $record["picid"];
    }
    $picid = $_GET['picid'];
    $next = $picid + 1; 
    $prev = $picid - 1; 
    ?> 

Dani AI

Generated

As @Diafol noted, the immediate fixes are to call session_start() and stop using deprecated mysql_* calls. The deeper logic bug is treating picid as both a primary-key value and a row offset. Incrementing $picid + 1 (or using LIMIT $picid, 10) will fail when IDs have gaps (for example when the next real picid is 40, not 5). Use the database to find the next/previous key instead of arithmetic on the ID.

Use queries that ask for the next/previous picid (filtered by the same user), then fetch the full record (or join the two tables) for that picid. Example patterns:

-- next id
SELECT picid FROM ipd
 WHERE userid = ? AND picid > ?
 ORDER BY picid ASC
 LIMIT 1;

-- previous id
SELECT picid FROM ipd
 WHERE userid = ? AND picid < ?
 ORDER BY picid DESC
 LIMIT 1;

-- fetch combined data
SELECT i.kra, i.kpi, p.progress1, p.progress2, p.progress3, p.progress4, i.picid
 FROM ipd i
 LEFT JOIN progress p ON p.userid = i.userid AND p.picid = i.picid
 WHERE i.userid = ? AND i.picid = ?;

Practical notes: use prepared statements (mysqli or PDO), cast request values to integers, check that $_SESSION['userid'] exists before using it, and escape output with htmlspecialchars() when populating inputs. If no next/previous row is returned, hide or disable the corresponding button. For complex filters, an alternate method is to load an ordered list of picid values for the user and navigate by array index.

Recommended Answers

All 3 Replies

Member Avatar for Member #120589

1) Stop using mysql_* functions - they are deprecated. I'm sure we've mentioned this a million times. DW Tutorial: Common Issues with MySQL and PHP
2) You're trying to access SESSION vars, but do you have session_start() anywhere?
3) $_SESSION['userid']; // it will print the userid value - no it doesn't you need to explicitly echo it.
4) These:

$_SESSION['kra'];
$_SESSION['kpi'];

are doing nothing
5) This code makes very little sense - you do not show the markup with the buttons...

..sorry, ran out of energy. Consider re-posting.

Hi Diafol, thanks for your reply. Please find the changes below as per your suggestion. Please advise. Thanks a lot.

<?php

                     session_start();


                 error_reporting(E_ALL ^ E_NOTICE);   

            $host = 'localhost';  
            $user = 'user';  
            $pass = '';  
            $dbname = 'pq';  
            $conn = mysqli_connect($host, $user, $pass,$dbname);  
            if(!$conn){  
              die('Could not connect: '.mysqli_connect_error());  
            }  
            echo 'Connected successfully<br/>';  
               echo $_SESSION['userid']; // it will print the userid value

                   if(isset($_GET['picid']) && (int)$_GET['picid']) $picid = $_GET['picid']; 
                else $picid = 1; 

            $sql = 'SELECT * FROM ipd where userid='".$_SESSION['userid']."' and picid= '$picid' LIMIT 1';  
            $retval=mysqli_query($conn, $sql);  

            if(mysqli_num_rows($retval) > 0){  
             while($row = mysqli_fetch_assoc($retval)){  
                echo "{$row['userid']}  <br> ".  
                     "{$row['kra']} <br> ".  
                     "{$row['kpi']} <br> ".  
                     "{$row['picid']} <br> ". ;  
             } //end of while  
            }else{  
            echo "0 results";  
            }  

            $sql = 'SELECT * FROM tprogress where userid='".$_SESSION['userid']."' and picid= '$picid' LIMIT 1';  
            $retval=mysqli_query($conn, $sql);  

            if(mysqli_num_rows($retval) > 0){  
             while($row = mysqli_fetch_assoc($retval)){  
                echo "{$row['progress1']}  <br> ".  
                     "{$row['progress2']} <br> ".  
                     "{$row['progress3']} <br> ".  
                     "{$row['progress4']} <br> ". 
                      "{$row['picid']} <br> ".;  
             } //end of while  
            }else{  
            echo "0 results";  
            }  

                $next = $picid + 1; 
                $prev = $picid - 1; 
            mysqli_close($conn);  
            ?>  
            <html>
            <body>
            <form name="progress" id="progress" method="post" action="">    
            <Table> 
                            <tr>
                                <tr>
                                <td><font size=2>KRA: </font></td>
                                <td><input type="text" name="kra" id="kra" value="<?php echo $row['kra']; ?>" readonly></td>
                            </tr>
                            <tr>
                                <td><font size=2>KPI:</font></td>
                                <td><input type="text" name="kpi" id="kpi" value="<?php echo $row['kpi']; ?>" readonly></td>
                                </tr>
                            </tr>
                        </table>
            <p><b>Progress</b></p>
                <label for="progress1">Progress Quarter 1:</label> 
                <p><input type="text" name="progress1" id="progress1" value="<?php echo $row['progress1'];?>"></p>
             <label for="progress2">Progress Quarter 2:</label> 
              <p><input type="text" name="progress2" id="progress2" value="<?php echo $row['progress2'];?>"></p>
                  <label for="progress3">Progress Quarter 3:</label> 
                <p><input type="text" name="progress3" id="progress3" value="<?php echo $row['progress3'];?>"></p>
             <label for="progress4">Progress Quarter 4:</label> 
              <p><input type="text" name="progress4" id="progress4" value="<?php echo $row['progress4'];?>"></p>
            <br>

            <ul>
            <table align="center">
            <tr><td><a href="progress.php?picid=<?php echo $prev;?>">
            <input type="button" value="Previous"></a></td> 
            <td><a href="progress.php?picid=<?php echo $next;?>">
            <input type="button" value="Next"></a></td> 
            </tr></table>
            </ul>
            </form>
            </body>
            </html>
            Hi everyone, have change the above coding to the coding below to get the next and previous records. However, still unable to get the next record as it shows the next record picid=14, where actually the next record supposed to be picid=40.The current record is picid=4. Please advise. Thanks.

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

                $_SESSION['userid']; 
                $_SESSION['username']; 
                $_SESSION['usersecurity']; 

            //check if the starting row variable was passed in the URL or not
            if (!isset($_GET['picid']) or !is_numeric($_GET['picid'])) {
              //we give the value of the starting row to 0 because nothing was found in URL
               $_SESSION['year'];
                $_SESSION['kra'];
                $_SESSION['kpi'];
                $_SESSION['measure'];
                $_SESSION['target']; 

             $picid = 0;
            //otherwise we take the value from the URL
            } else {
              $picid = (int)$_GET['picid'];
            }

            //this part goes after the checking of the $_GET var
            $fetch = mysql_query("SELECT * FROM general,progress LIMIT $picid, 10")or
            die(mysql_error());

            //now this is the link..
            echo '<a href="'.$_SERVER['PHP_SELF'].'?picid='.($picid+10).'">Next</a>';

            $prev = $picid - 10;

            //only print a "Previous" link if a "Next" was clicked
            if ($prev >= 0)
                echo '<a href="'.$_SERVER['PHP_SELF'].'?picid='.$prev.'">Previous</a>';
            ?>
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.