Jump to content

PHP Pagination Error while using "Where" clause


uppalakishore

Recommended Posts

I want to display 4 records per page by using pagination on clicking "showdetails2.php"...Everything will be working fine but when i click on next page...it shows error.....

 

 

Simply what i want is i want to display 4 records per page(menas 4 students of each department)

 

///////////////////////////////////////////////

 

 

 

<?php

                        include('db_connect.php');

                        $rowsPerPage =4;

 

                        // by default we show first page

                        $pageNum = 1;

 

                        // if $_GET['page'] defined, use it as page number

                        if(isset($_GET['page']))

                        {

                            $pageNum = $_GET['page'];

                        }

 

                        // counting the offset

                        $offset = ($pageNum - 1) * $rowsPerPage;

                        @session_start();

                        $_SESSION['dis']=$_POST['disciplines'];

                        $result=mysql_query("select * from student_master where Disciplines='".$_SESSION['dis']."' LIMIT $offset,$rowsPerPage ");

                        if(mysql_num_rows($result)>0)

                        {

                            echo "

                                    <h2 align='center' class='spl_heading'><font color=green>".$_SESSION['dis']."</font> Students Details : </h2>

                                    <table width='98%' cellpadding='2' cellspacing='2' border='1' align='center'>

                                    <tr class='fade_bg' align='center'>

                                    <td class='spl_bgcolor'>Enrollment No</td>

                                    <td>Name</td>

                                    <td>Father's Nname</td>

                                    <td>Address</td>

                                    <td>Discipline</td>

                                    <td>Semester</td>

                                    <td>Year</td>

                                    <td>Email</td>

                                    <td>Mobile Number</td>

                                    <td>Photo</td></tr>";

                            while($row=mysql_fetch_row($result))

                            {

                                echo "  <tr class='sp_table_row'>

                                        <td class='spl_bgcolor'style='color:fcc;' width='16%' align='center'>$row[0]</td>

                                        <td width='30%'>$row[1]</td>

                                        <td width='25%'>$row[2]</td>

                                        <td width='25%'>$row[3]</td>

                                        <td width='10%'>$row[4]</td>

                                        <td width='15%'>$row[5]</td>

                                        <td width='10%'>$row[7]</td>

                                        <td width='35%'>$row[6]</td>

                                        <td width='20%'>$row[8]</td>

                                        <td><img src='upload/$row[9]' alt='Upload Image' align='center' style='-moz-border-radius:4px;' class='magnify' data-magnifyduration='800' width=80 height=110></td>

                                    </tr>";

 

                            }

                                echo "<tfoot><tr><td  colspan=10><marquee behavior='alternate' scrollamount='3' class='yellow'>Click on Image to Enlarge & Resize</marquee></td></tr></tfoot>

 

                                    </table><br>

                                    ";

                                            // how many rows we have in database

                        $query  = "SELECT COUNT(En_no) AS numrows FROM student_master where                                                                                    Disciplines='".$_SESSION['dis']."'";

                        $result1  = mysql_query($query) or die('Error, query failed');

                        $row    = mysql_fetch_array($result1, MYSQL_ASSOC);

                        $numrows = $row['numrows'];

 

                        // how many pages we have when using paging?

                        $maxPage = ceil($numrows/$rowsPerPage);

 

                        // print the link to access each page

                        $self = $_SERVER['PHP_SELF'];

                        $nav  = '';

 

                        for($page = 1; $page <= $maxPage; $page++)

                        {

                        if ($page == $pageNum)

                        {

                            $nav .= "  <span class='bgc'><b> $page </b></span> "; // no need to create a link to current page

                        }

                        else

                        {

                            $nav .= "  <a href=\"$self?page=$page\">$page</a> ";

                        }

                        }

 

                        // ... still more code coming

                        if ($pageNum > 1)

                        {

                        $page  = $pageNum - 1;

                        $prev  = "  <a href=\"$self?page=$page\">[Prev]</a> ";

 

                        $first = "  <a href=\"$self?page=1\">[First Page]</a>";

                        }

                        else

                        {

                        $prev  = ' '; // we're on page one, don't print previous link

                        $first = ' '; // nor the first page link

                        }

 

                        if ($pageNum < $maxPage)

                        {

                        $page = $pageNum + 1;

                        $next = "  <a href=\"$self?page=$page\">[Next]</a> ";

 

                        $last = "  <a href=\"$self?page=$maxPage\">[Last Page]</a>";

                        }

                        else

                        {

                        $next = ' '; // we're on the last page, don't print next link

                        $last = ' '; // nor the last page link

                        }

 

                        // print the navigation link

                        echo "<div class='pager'><center>".$first  .  $prev  .  $nav  .  $next  .  $last."</center></div>";

                                echo "<center>To search again,Click <a href='search_decipline.php' class='btn'><blink><b>here</b></blink></a> . . .</center>";

                        }

                        else

                        {

                            echo "<br><center>Error Occured. <a href='search_decipline.php' class='btn'><blink>Try Again</blink></a> . . . </center>";

                        }

        ?>

 

 

 

Solve it For me And THANKS IN ADVANCE :o.... :o

19038_.php

19039_.php

Link to comment
Share on other sites

Actually i'm New here...

 

And i think the error might be here(following):

 

////////////// Main Query to get Students belongs to a Particular Department(discipline) /////////

                        $dis=$_POST['disciplines'];

                        @session_start();

                        $_SESSION['dis']=$_POST['disciplines'];

                        $result=mysql_query("select * from student_master where Disciplines='".$_SESSION['dis']."' LIMIT $offset,$rowsPerPage");

                        if(mysql_num_rows($result)>0)

                        {

                                  //print some table headings

                                  while($row=mysql_fetch_row($result))

                                  {

                                      //print some table rows

                                  }

                                    // how many rows we have in database to print for PAGINATION

                                include('db_connect.php');  //to establish database connection

                                $query  = "select COUNT(Disciplines) AS numrows FROM student_master where Disciplines='".$_SESSION['dis']."'";

                                $result2  = mysql_query($query) or die('Error, query failed');

                                $row  = mysql_fetch_array($result2,MYSQL_ASSOC);

                                $numrows = $row['numrows'];

 

                              //place some pagination links

                          }

                          else

                          {

                              echo "Error Occurd"

                            }

 

 

When i run the program first page working fine...when i click on next page it will show msg "Error Occurd"(which is showd in blue color as a else stmt)...i think it may be a session variable problem but i don't know how to rectify it...

THANKS FOR UR REPLY

Link to comment
Share on other sites

1. Use code tags.

2. We aren't here to "solve it for you".

3. Post only the relevant part, not the whole file.

4. Don't suppress errors.

 

1 - When posting source code, use the code tags. That's the button up above the textarea with the hash-mark ("#") on it. Or, if you're like me and don't want to waste the time to move your hand to the mouse and back, you type "[" (left-square bracket), "code", "]" (right-square bracket). And then close the code with "[" "/code" "]" --- Obviously, without all the spaces and quotes

 

Using the code tags will help even more than colorizing your code!

 

4 - Remove the "@" (error suppression construct) in @session_start() and anywhere else that you have it in your code. Fix the errors, warnings, notices, whatever don't hide them. And while we are talking about not suppressing errors, add the code to show them at the beginning of your script:

error_reporting(E_ALL);
ini_set('display.errors', 1);

 

 

Now, on your second and subsequent pages, this line of code: $_SESSION['dis']=$_POST['disciplines']; is causing you problems. Since page "2" is not POSTed, the $_POST array does not exist and you are clearing the value that you assigned on page "1". You need to make this assignment conditionally:

if (isset($_POST['disciplines'])) $_SESSION['dis']=$_POST['disciplines'];

Note: you should be getting an error on that line warning about referencing an index that does not exist. (If you have error reporting turned on).

 

Additional Issues:

You are placing the user supplied value directly into your query, which will leave you open to sql injection or failed queries. You need to escape that value:

"select * from student_master where Disciplines='" . mysql_real_escape_string($_SESSION['dis']) . "' LIMIT $offset,$rowsPerPage"

 

You should avoid $_SERVER[php_SELF] or sanitize it first. This value comes from the user and can create problems.

 

If you clean all that up, and still have problems, post the newest code, along with any error messages in their entirety.

Link to comment
Share on other sites

I'm sory  :shy: to say this...i don't know how to avoid $_SERVER['PHP_SELF'],and it is linked with Overall pages.....

 


// print the link to access each page
                        $self = $_SERVER['PHP_SELF'];
                        $nav  = '';

                        for($page = 1; $page <= $maxPage; $page++)
                        {
                        if ($page == $pageNum)
                        {
                            $nav .= "  <span class='bgc'><b> $page </b></span> "; // no need to create a link to current page
                        }
                        else
                        {
                            $nav .= "  <a href=\"$self?page=$page\">$page</a> ";
                        }
                        }

                        // ... still more code coming
                        if ($pageNum > 1)
                        {
                        $page  = $pageNum - 1;
                        $prev  = "  <a href=\"$self?page=$page\">[Prev]</a> ";

                        $first = "  <a href=\"$self?page=1\">[First Page]</a>";
                        }
                        else
                        {
                        $prev  = ' '; // we're on page one, don't print previous link
                        $first = ' '; // nor the first page link
                        }

                        if ($pageNum < $maxPage)
                        {
                        $page = $pageNum + 1;
                        $next = "  <a href=\"$self?page=$page\">[Next]</a> ";

                        $last = "  <a href=\"$self?page=$maxPage\">[Last Page]</a>";
                        }
                        else
                        {
                        $next = ' '; // we're on the last page, don't print next link
                        $last = ' '; // nor the last page link
                        }

                        // print the navigation link
                        echo "<div class='pager'><center>".$first  .  $prev  .  $nav  .  $next  .  $last."</center></div>";


 

Will Please tell me how :confused: to do it......There by pagination will work fine......////////First page works fine but when i click on next page nothing will come.....

 

when i use session_start(), it shows error headers already sent...

 


Thanks For Your Reply....


 

Link to comment
Share on other sites

For the PHP_SELF security issue, I recommend that you use this snippet. Put it at the very top of your entrance files (index.php, and others the user can visit directly), and you'll ensure that it's safe to use.

 

Then for your "headers already sent" problem, there is a sticky detailing this. I strongly recommend reading it.

Link to comment
Share on other sites

I tried the following code by applying SESSION (Some times working properly) but i got the Error,Means it will executed in one system and it won't work in other system


 

session_start();
if (isset($_POST['disciplines'])) $_SESSION['dis']=$_POST['disciplines'];
include('db_connect.php');
$rowsPerPage =4;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;

$result=mysql_query("select * from student_master where Disciplines='" . mysql_real_escape_string($_SESSION['dis']) . "' LIMIT $offset,$rowsPerPage");
if(mysql_num_rows($result)>0)
{
echo "
".mysql_real_escape_string($_SESSION['dis'])." Students Details :












";
while($row=mysql_fetch_row($result))
{
echo "










";

}
echo "

Enrollment No    Name    Father's Nname    Address    Discipline    Semester    Year    Email    Mobile Number    Photo
$row[0]    $row[1]    $row[2]    $row[3]    $row[4]    $row[5]    $row[7]    $row[6]    $row[8]    Upload Image
Click on Image to Enlarge & Resize



";
// how many rows we have in database
$query = "SELECT COUNT(En_no) AS numrows FROM student_master where Disciplines='".mysql_real_escape_string($_SESSION['dis'])."'";
$result1 = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result1, MYSQL_ASSOC);
$numrows = $row['numrows'];

// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);

// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';

for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= "  $page  "; // no need to create a link to current page
}
else
{
$nav .= " $page ";
}
}

// ... still more code coming
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " [Prev] ";

$first = " [First Page]";
}
else
{
$prev = ' '; // we're on page one, don't print previous link
$first = ' '; // nor the first page link
}

if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " [Next] ";

$last = " [Last Page]";
}
else
{
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}

// print the navigation link
echo "
".$first . $prev . $nav . $next . $last."
";
echo "
To search again,Click here . . .
";
}
else
{
echo "
Error Occured. Try Again . . .

";
}


?>


 

When i Run the above code i got that the error was in the following line....

 


 

"select * from student_master where Disciplines='" . mysql_real_escape_string($_SESSION['dis']) . "' LIMIT $offset,$rowsPerPage"

 

And Thanks In Advance...

Link to comment
Share on other sites

What is the exact error message? Also, why all of the empty newlines?

 

When posting code on this new forum, at least for the time being, please either use the "paste as plain text" button (middle of the three yellow clipboard buttons), or turn off the rich text editor with the button at the top left. Otherwise it'll strip the indenting, making the code harder to read than necessary.

On the other hand, if your code actually looks like that, then you need to start properly indenting it. As it will help you avoid problems, and make it easier for people to help you out.

Edited by Christian F.
Link to comment
Share on other sites

CTRL + Q to Enable/Disable GoPhoto.it

I think, the Problem might be here:


session_start();
if (isset($_POST['disciplines'])) $_SESSION['dis']=$_POST['disciplines'];


 

[code]
<html>
<body>
 <a href="home.php" align="left" style="text-decoration:none;font:verdana;color:red;padding: 5px;margin-left:40px;"><button class="btn" onclick="javascript:location.href='home.php'" class="btn" style="font-weight: bold;">Home Page</button></a>
 <button class="btn" onclick="history.go(-1);return true;">Go Back</button>

 <?php
//					 // Make sure that PATH_INFO is set, and not ORIG_PATH_INFO as some hosts seem to use.
//					 if (isset ($_SERVER['ORIG_PATH_INFO']) && $_SERVER['ORIG_PATH_INFO'] != $_SERVER['PHP_SELF']) {
//					 $_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO'];
//					 }
//
//					 // Security measure, to avoid XSS exploit.
//					 if (!empty ($_SERVER['PATH_INFO']) && strrpos ($_SERVER['PHP_SELF'], $_SERVER['PATH_INFO'])) {
//
//
//
//					 $_SERVER['PHP_SELF'] = substr ($_SERVER['PHP_SELF'], 0, -(strlen ($_SERVER['PATH_INFO'])));
//					 }

session_start();
 if (isset($_POST['disciplines'])) $_SESSION['dis']=$_POST['disciplines'];
				 include('db_connect.php');
				 // how many rows to show per page
				 $rowsPerPage = 4;
				 // by default we show first page
				 $pageNum = 1;
				 // if $_GET['page'] defined, use it as page number
				 if(isset($_GET['page']))
				 {
					 $pageNum = $_GET['page'];
				 }
				 // counting the offset,example we want 5 rows per page,offset=(1-1)*5=0(means 0 to 5 rows on first page,(2-1)*5=5(5 to 5 on second page)
				 $offset = ($pageNum - 1) * $rowsPerPage;
				 $result=mysql_query("select * from student_master where Disciplines='" . mysql_real_escape_string($_SESSION['dis']) . "' LIMIT $offset,$rowsPerPage") or die('Error : '. mysql_error());
				 $result2=mysql_query("select count(*) from student_master where Disciplines='".mysql_real_escape_string($_SESSION['dis'])."'");
					 echo "<table align='right'><tr><td><fieldset border='1' class='fieldmoz'>Total ".mysql_real_escape_string($_SESSION['dis'])." Students = <font color='red' ><b>".mysql_result($result2,0)."</fieldset></b></font></td></tr></table>
							 <center><h2 align='center' class='spl_heading'><font color=green>".mysql_real_escape_string($_SESSION['dis'])."</font> Students Details : </h2></center>
								 <table width='70%' cellpadding='2' cellspacing='2' border='1' align='center'>
							 <tr class='fade_bg' align='center'>
							 <td class='spl_bgcolor'>Enrollment No</td>
							 <td>Name</td>

							 <td>Discipline</td>

							 <td>Year</td>

							 <td>Photo</td>
							 </tr>";
					 while($row=mysql_fetch_row($result))
					 {
						 echo " <tr class='sp_table_row'>
								 <td class='span' width='16%' align='center'><a class='underline' href='all_click_details.php?En_no=$row[0]'>$row[0]</a></td>
								 <td width='20%'>$row[1]</td>

								 <td align='center' width='10%'>$row[4]</td>

								 <td width='10%' align='center'>$row[7]</td>

								 <td width='6%' align='center'><img src='upload/$row[9]' alt='$row[1]' title='$row[1]' align='center' style='-moz-border-radius:4px;' class='magnify' data-magnifyduration='800' width='90' height='110'></td>

						 </tr>";
					 }
						 echo "<tfoot><tr><td colspan=10><marquee behavior='alternate' scrollamount='3' align='center' class='yellow'>Click on Image to Enlarge & Resize</marquee></td></tr></tfoot>
							 </table><br>
							 ";

							 // how many rows we have in database
							 $result1=mysql_query("select count(Disciplines) from student_master where Disciplines='".mysql_real_escape_string($_SESSION['dis'])."'");
							 $row=mysql_fetch_row($result1);
							 $numrows=$row[0];
						 // how many pages we have when using paging?
						 $maxPage = ceil($numrows/$rowsPerPage);
						 // print the link to access each page
						 $self = $_SERVER['PHP_SELF'];
						 $nav = '';
						 for($page = 1; $page <= $maxPage; $page++)
						 {
							 if ($page == $pageNum)
							 {
								 $nav .= " <span class='bgc'><b> $page </span> |</b>"; // no need to create a link to current page
							 }
							 else
							 {
								 $nav .= " <a href=\"$self?page=$page\">$page</a> <b>|</b>";
							 }
						 }
						 // ... still more code coming
						 if ($pageNum > 1)
						 {
							 $page = $pageNum - 1;
							 $prev = " <a href=\"$self?page=$page\">[Prev]</a> <b>|</b>";
							 $first = " <a href=\"$self?page=1\">[First Page]</a>";
						 }
						 else
						 {
							 $prev = ' '; // we're on page one, don't print previous link
							 $first = ' '; // nor the first page link
						 }
						 if ($pageNum < $maxPage)
						 {
							 $page = $pageNum + 1;
							 $next = " <a href=\"$self?page=$page\">[Next]</a> <b>|</b>";
							 $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a>";
						 }
						 else
						 {
							 $next = ' '; // we're on the last page, don't print next link
							 $last = ' '; // nor the last page link
						 }
						 // print the navigation link
						 echo "<div class='pager'><center>".$first . $prev . $nav . $next . $last."</center></div>";
						 echo "<center><br><button onclick='javascript:window.print()' value=''class='btn' style='width:100;height:25;'>Print</button></center>";
						 // and close the database connection
						 mysql_close();

					 ?>
</body>
</html>

 


When i run the above code it will give correct results in one system,when i run it on my laptop it won't produce the same result....i think there might be an error in passing SESSION VARIABLES to the next page.(B'Coz when i click on next page it will display empty page with table headers ony...

I can't understand whether it is coding problem or my Laptop OS Problem(b'coz it will run correctly on other system)....

Edited by uppalakishore
Link to comment
Share on other sites

The problem you're having is that you've already send the HTTP headers by the time you attempt to start the session. To solve it I recommend that you read through the thread "HEADER ERRORS", which is stickied at the top of the "PHP coding help" section.

 

You're clearly suppressing errors on your laptop, which is only serving to mask the actual issue. Which is why we all recommend that you turn on error reporting in your php.ini, and make sure it gets outputted to the browser. Only for development environments, of course.

Once you get the error messages, you can start to deal with the actual problems, instead of having to guess at what they are.

 

Also, it seems as if you might have enabled output buffering on your primary system, which again only serves to hide problems. Turn it off, and make sure error reporting is turned on there too.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.