/    Sign up×
Community /Pin to ProfileBookmark

Why Session Variable Auto Switches ?

Php Folks,

Why on earth does the following variable’s value “5” auto switch to “0” when I reload the page ?
$_SESSION[‘row_count’] = $row_count;

You see, when we on PAGE 1, the value is 5. Then when I reload the page with different param in url this time from “page_1” to “page_2” (pagination) then the $_SESSION[‘row_count’] = 0. While on PAGE 1, it was $_SESSION[‘row_count’] = 5.
Why the God Forsaken value switch ?

[code]
function search()
{
function rows_count()
{
//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect(“localhost”,”root”,””,”powerpage”);
$conn->set_charset(‘utf8mb4’); //Always set Charset.

if($conn === false)
{
die(“ERROR: Connection Error!. ” . mysqli_connect_error());
}

$query_1 = “SELECT COUNT(id) FROM users WHERE first_name = ? AND marital_status = ?”;
$stmt_1 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_1,$query_1))
{
mysqli_stmt_bind_param($stmt_1,”ss”,$_POST[“first_name”],$_POST[“marital_status”]);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
mysqli_stmt_fetch($stmt_1);
$_SESSION[‘row_count’] = $row_count;
$_SESSION[‘form_step’] = ‘end’;
}
//Close Statement.
mysqli_stmt_close($stmt_1);
//Close Connection.
mysqli_close($conn);
}
[/code]

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 07.2020 — When you reload the page, are you reloading the POST fields from the form submission? (I'm assuming it's a form submission?) If not, then the $_POST values you're using in the query will not exist.

PS: Do you really have the rows_count() function definition inside of the search() function definition? 99.999% of the time you probably don't want to do that.
Copy linkTweet thisAlerts:
@developer_webauthorJul 07.2020 — @NogDog#1620338

No. I am not. Look:
<i>
</i>$query_1 = "SELECT COUNT(id) FROM users WHERE first_name = ? AND marital_status = ?";
$stmt_1 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_1,$query_1))
{
mysqli_stmt_bind_param($stmt_1,"ss",$_POST["first_name"],$_POST["marital_status"]);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
mysqli_stmt_fetch($stmt_1);
$_SESSION['row_count'] = $row_count;


NogDog, do you see this line:
<i>
</i>$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);


Note the $row_count. that is the value of the COUNT in query_1 (Sql Query Result).

I need php to remember this. I need to pass this value from one function to another and so used $_SESSION['row_count'] here so when I reload the page, the variable value is not forgotten.

Problem is, before reloading the page, the value is "5". Meaning row count is "5".

Now, when I relaod the page from PAGE 1 to PAGE 2 (pagination) then $_
SESSION['row_count'] = 5 auto switches to $_SESSION['row_count'] = 0.

Why the value switch ? How to stop this switch ? Do I GLOBAl session values ?

If so, then which line should I global from here ....

<i>
</i>function search()
{echo __LINE__; echo "&lt;br&gt;";//DELETE
function rows_count()
{
//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost","root","","powerpage");
$conn-&gt;set_charset('utf8mb4'); //Always set Charset.

<i> </i> if($conn === false)
<i> </i> {
<i> </i> die("ERROR: Connection Error!. " . mysqli_connect_error());
<i> </i> }
<i> </i>
<i> </i> $query_1 = "SELECT COUNT(id) FROM users WHERE first_name = ? AND marital_status = ?";
<i> </i> $stmt_1 = mysqli_stmt_init($conn);
<i> </i> if(mysqli_stmt_prepare($stmt_1,$query_1))
<i> </i> {
<i> </i> mysqli_stmt_bind_param($stmt_1,"ss",$_POST["first_name"],$_POST["marital_status"]);
<i> </i> mysqli_stmt_execute($stmt_1);
<i> </i> $result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
<i> </i> mysqli_stmt_fetch($stmt_1);
<i> </i> $_SESSION['row_count'] = $row_count;
<i> </i> GLOBAL $row_count;
<i> </i> echo __LINE__; echo "&lt;br&gt;";//DELETE
<i> </i> $_SESSION['form_step'] = 'end'; //$form_step = 'end'; WRONG
<i> </i> //fetch_rows();
<i> </i> }
<i> </i> //Close Statement.
<i> </i> mysqli_stmt_close($stmt_1);
<i> </i> //Close Connection.
<i> </i> mysqli_close($conn);
<i> </i>}

<i> </i>function fetch_rows()
<i> </i>{ echo __LINE__; echo "&lt;br&gt;";//DELETE
<i> </i> $form_step = $_GET['form_step'];
<i> </i>
<i> </i> $page_number = $_GET['page'];
<i> </i> $result_per_page = $_GET['page_limit'];
<i> </i> $offset = (($page_number * $result_per_page) - $result_per_page); //Offset (Row Number that 'Starts' on page).
<i> </i> $last_row_on_page = ($page_number * $result_per_page); //Max Result (Row Number that 'Ends' on page).
<i> </i> $previous_page = $page_number-1;
<i> </i> $next_page = $page_number+1;
<i> </i>
<i> </i> echo "Row Start: $offset";echo "&lt;br&gt;";
<i> </i> echo "Row End: $last_row_on_page";echo "&lt;br&gt;";
<i> </i>
<i> </i> //Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
<i> </i> mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
<i> </i> $conn = mysqli_connect("localhost","root","","powerpage");
<i> </i> $conn-&gt;set_charset('utf8mb4'); //Always set Charset.

<i> </i> if($conn === false)
<i> </i> {
<i> </i> die("ERROR: Connection Error!. " . mysqli_connect_error());
<i> </i> }

<i> </i> $query_2 = "SELECT * FROM users WHERE first_name = ? AND marital_status = ? ORDER BY id LIMIT $offset,$last_row_on_page";
<i> </i> $stmt_2 = mysqli_stmt_init($conn);
<i> </i> if(mysqli_stmt_prepare($stmt_2,$query_2))
<i> </i> {echo __LINE__; echo "&lt;br&gt;";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 111.
<i> </i> mysqli_stmt_bind_param($stmt_2,"ss",$_POST["first_name"],$_POST["marital_status"]);
<i> </i> mysqli_stmt_execute($stmt_2);
<i> </i> $result_2 = mysqli_stmt_get_result($stmt_2);
<i> </i> echo __LINE__; echo "&lt;br&gt;";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 114.
<i> </i> //Grab total number of pages to paginate.
<i> </i> $row_count = $_SESSION['row_count'];
<i> </i> //$total_pages = ceil($result_1/$result_per_page);
<i> </i> $total_pages = ceil($row_count/$result_per_page);
<i> </i>
<i> </i> echo "TOTAL PAGES: $total_pages&lt;br&gt;&lt;br&gt;";
<i> </i>
<i> </i> while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
<i> </i> {echo __LINE__; echo "&lt;br&gt;";
<i> </i> //Retrieve Values.
<i> </i> $id = $row["id"];
<i> </i> $first_name = $row["first_name"];
<i> </i> $middle_name = $row["middle_name"];
<i> </i> $surname = $row["surname"];
<i> </i> $gender = $row["gender"];
<i> </i> $marital_status = $row["marital_status"];
<i> </i> $working_status = $row["working_status"];
<i> </i>
<i> </i> echo "Id: $id&lt;br&gt;";
<i> </i> echo "First Name: $first_name&lt;br&gt;";
<i> </i> echo "Middle Name: $middle_name&lt;br&gt;";
<i> </i> echo "Surname: $surname&lt;br&gt;";
<i> </i> echo "Gender: $gender&lt;br&gt;";
<i> </i> echo "Marital Status: $marital_status&lt;br&gt;";
<i> </i> echo "Working Status: $working_status&lt;br&gt;";
<i> </i> echo "&lt;br&gt;";
<i> </i> echo "&lt;br&gt;";
<i> </i> }
<i> </i> $i = 1;
<i> </i> while($i&lt;=$total_pages)
<i> </i> {
<i> </i> if($i&lt;$total_pages)
<i> </i> {
<i> </i> echo "&lt;a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW_1.php?form_type=";?&gt;&lt;?php echo $_SESSION['form_type'];?&gt;&amp;query_type=&lt;?php echo $_SESSION['query_type'];?&gt;&amp;form_step=end&amp;page_limit=2&amp;page=&lt;?php echo $i;?&gt;'&gt;&lt;?php echo " $i ";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i> elseif($i==$page_number)
<i> </i> {
<i> </i> echo "&lt;a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW_1.php?form_type=";?&gt;&lt;?php echo $_SESSION['form_type'];?&gt;&amp;query_type=&lt;?php echo $_SESSION['query_type'];?&gt;&amp;form_step=end&amp;page_limit=2&amp;page=&lt;?php echo $i;?&gt;'&gt;&lt;?php echo "&lt;b&gt; $i &lt;/b&gt;";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i>
<i> </i> $i++;
<i> </i> }
<i> </i> if($page_number&gt;$total_pages)
<i> </i> {
<i> </i> echo "&lt;a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW_1.php?form_type=";?&gt;&lt;?php echo $_SESSION['form_type'];?&gt;&amp;query_type=&lt;?php echo $_SESSION['query_type'];?&gt;&amp;form_step=end&amp;page_limit=2&amp;page=&lt;?php echo $previous_page;?&gt;'&gt;&lt;?php echo "&lt;b&gt; Previous &lt;/b&gt;";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i> }
<i> </i> //Close Statement.
<i> </i> mysqli_stmt_close($stmt_2);
<i> </i> //Close Connection.
<i> </i> mysqli_close($conn);
<i> </i>
<i> </i> $_SESSION['form_step'] = 'end';
<i> </i>}
Copy linkTweet thisAlerts:
@NogDogJul 07.2020 — Does the search() function get called on the reloaded/second/whatever page? If so, it's probably going to set $_SESSION['row_count'] to 0, since it will run the query again but the $_POST values it uses will be empty.

That's speculation on my part since I cannot see the program flow and know the difference between the 2 different views. If you want to verify the the new page is picking it up initially, stick something like this to see what is set before anything else runs:
<i>
</i>&lt;?php
session_start();
echo "&lt;pre&gt;SESSION:n".print_r($_SESSION, 1)."&lt;/pre&gt;";
echo "&lt;pre&gt;POST:n".print_r($_POST, 1)."&lt;/pre&gt;";
Copy linkTweet thisAlerts:
@developer_webauthorJul 08.2020 — @NogDog#1620345

Do I just copy & paste your code as is or amend ?

I was told that the $_POST will be empty on page 2 etc. And so your assumptions seem to be true.

Now got to fix this. Shall I make the $_
POST user inputs into $_SESSIONs or what so they are not lost ?
Copy linkTweet thisAlerts:
@developer_webauthorJul 08.2020 — NogDog,

And yes, search() gets called on all pagination pages. page 1,2,3,4,5 etc.

I see this getting echoed:

**SESSION:

Array

(

[query_type] => select

[form_type] => search

[form_step] => end

[row_count] => 0

)

POST:

Array

(

)**
Copy linkTweet thisAlerts:
@developer_webauthorJul 08.2020 — NogDog,

Ok, let's assume the search() loads on page 2,3,4,5 etc. and all the form inputs are disappearing. That means, values of $_post['first_name'] & $_post['marital_status'] are vanishing as soon as I load page 2.

To prevent this vanishing act, I added these:

<i>
</i>$_SESSION["first_name"] = $_POST["first_name"];//ADDED THIS NEW LINE.
$_SESSION["marital_status"] = $_POST["marital_status"];//ADDED THIS NEW LINE.

Added the $_POSTs to $_SESSIONs.

Look here again I did it ...
<i>
</i>//mysqli_stmt_bind_param($stmt_1,"ss",$_POST["first_name"],$_POST["marital_status"]);//SUBSTITUTED THIS LINE FOR THE LINE BELOW.

mysqli_stmt_bind_param($stmt_1,"ss",$_SESSION["first_name"],$_SESSION["marital_status"]);//SUBSTITUTED TO THIS LINE FROM THE LINE ABOVE.


Now update looks like the following....

Note that, I have dragged out the rows_count() and fetch_rows() out of the search() function as was advised to separate them.

But guess what ? This time I get displayed no results even on PAGE 1!!!!

Last time the issue was PAGE 1 was showing the search results but not any page after PAGE 1. Not page 2/3/4/5.

This time, it's worst. Even PAGE 1 doesn't show any results! I am lost now. Totally!

CODE
<i>
</i>&lt;?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
session_start();

//I WAS TOLD $_POST VALUES ARE LIKELY TO BE EMPTY ON PAGINATION PAGE 1,2,3,4,5. WAS ADVISED TO ADD THIS FOLLOWING CODE TO CHECK.
echo "&lt;pre&gt;SESSION:n".print_r($_SESSION, 1)."&lt;/pre&gt;";
echo "&lt;pre&gt;POST:n".print_r($_POST, 1)."&lt;/pre&gt;";
?&gt;

&lt;!DOCTYPE HTML"&gt;
&lt;html&gt;

&lt;head&gt;
&lt;meta name="viewport" content="width-device=width, initial-scale=1"&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php

if(!isset($_GET['query_type']) &amp;&amp; empty($_GET['query_type']))//'query_type' tells the script whether it is an INSERT or SELECT or UPDATE sql query. From this the script determines which type of form to display and what prepared statements lines to use.
{
die("Invalid Query!");
}
else
{
$_SESSION['query_type'] = $_GET['query_type'];
echo __LINE__; echo "&lt;br&gt;";//DELETE
}
echo __LINE__; echo "&lt;br&gt;";//DELETE

if(!isset($_GET['form_type']) &amp;&amp; empty($_GET['form_type']))//'form_type' tells the script which form to display. (Login, Reg, Submit Personal Details, Submit Link).
{
die("Invalid Form!");
}
else
{
$_SESSION['form_type'] = $_GET['form_type'];
echo __LINE__; echo "&lt;br&gt;";//DELETE
<br/>
<i> </i>if(!function_exists($_SESSION['form_type']))
<i> </i>{
<i> </i> die("Invalid Form!");
<i> </i>}
<i> </i>else
<i> </i>{echo __LINE__; echo "&lt;br&gt;";//DELETE
<i> </i> if(!isset($_SESSION['form_step']))// || $_SESSION['form_step'] != 'end')
<i> </i> {
<i> </i> $_SESSION['form_step'] = 'start'; echo $_SESSION['form_step'];
<i> </i> echo __LINE__; echo "&lt;br&gt;";//DELETE
<i> </i> $_SESSION['form_type']();
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> $_SESSION['form_step'] = $_GET['form_step'];
<i> </i> echo __LINE__; echo "&lt;br&gt;"; echo $_SESSION['form_step'];//DELETE
<i> </i> $_SESSION['form_type']();
<i> </i> }
<i> </i>}
}

function search()
{
echo __LINE__; echo "&lt;br&gt;";//DELETE
//Do following if "Search" button clicked.
if($_SERVER['REQUEST_METHOD'] === 'POST') //Doing following means "Search" button clicked AND pagination numbered links are NOT clicked. Eg page 2, 3, etc.
{echo __LINE__; echo "&lt;br&gt;";//DELETE
//Do following if "Search" button clicked.
if(isset($_POST['search']))
{echo __LINE__; echo "&lt;br&gt;";//DELETE
rows_count();
fetch_rows();
echo __LINE__; echo "&lt;br&gt;";
die;
}
}
else //Doing following means "Search" button not clicked but pagination numbered links are clicked. Eg page 2, 3, etc.
{
echo __LINE__; echo "&lt;br&gt;";//DELETE
//Do following if "Search" button not clicked but pagination numbered links are clicked. Eg Page 2, 3, etc..
//rows_count(); //SHOULD I RUN THIS FUNCTION AGAIN FROM PAGE 2,3,4, ETC OR NOT ?
fetch_rows();
echo __LINE__; echo "&lt;br&gt;";//DELETE
}
}


function rows_count()
{
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost","root","","powerpage");
$conn-&gt;set_charset('utf8mb4'); //Always set Charset.

<i> </i>if($conn === false)
<i> </i>{
<i> </i> die("ERROR: Connection Error!. " . mysqli_connect_error());
<i> </i>}

<i> </i>$query_1 = "SELECT COUNT(id) FROM users WHERE first_name = ? AND marital_status = ?";
<i> </i>$_SESSION["first_name"] = $_POST["first_name"];//ADDED THIS NEW LINE.
<i> </i>$_SESSION["marital_status"] = $_POST["marital_status"];//ADDED THIS NEW LINE.
<i> </i>$stmt_1 = mysqli_stmt_init($conn);
<i> </i>if(mysqli_stmt_prepare($stmt_1,$query_1))
<i> </i>{
<i> </i> //mysqli_stmt_bind_param($stmt_1,"ss",$_POST["first_name"],$_POST["marital_status"]);//SUBSTITUTED THIS LINE FOR THE LINE BELOW.
<i> </i> mysqli_stmt_bind_param($stmt_1,"ss",$_SESSION["first_name"],$_SESSION["marital_status"]);//SUBSTITUTED TO THIS LINE FROM THE LINE ABOVE.
<i> </i> mysqli_stmt_execute($stmt_1);
<i> </i> $result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
<i> </i> mysqli_stmt_fetch($stmt_1);
<i> </i> $_SESSION['row_count'] = $row_count;
<i> </i> echo __LINE__; echo "&lt;br&gt;";//DELETE
<i> </i> $_SESSION['form_step'] = 'end';
<i> </i> //fetch_rows();
<i> </i>}
<i> </i>//Close Statement.
<i> </i>mysqli_stmt_close($stmt_1);
<i> </i>//Close Connection.
<i> </i>mysqli_close($conn);
}

function fetch_rows()
{ echo __LINE__; echo "&lt;br&gt;";//DELETE
$form_step = $_GET['form_step'];
<br/>
<i> </i>$page_number = $_GET['page'];
<i> </i>$result_per_page = $_GET['page_limit'];
<i> </i>$offset = (($page_number * $result_per_page) - $result_per_page); //Offset (Row Number that 'Starts' on page).
<i> </i>$last_row_on_page = ($page_number * $result_per_page); //Max Result (Row Number that 'Ends' on page).
<i> </i>$previous_page = $page_number-1;
<i> </i>$next_page = $page_number+1;
<i> </i>
<i> </i>echo "Row Start: $offset";echo "&lt;br&gt;";
<i> </i>echo "Row End: $last_row_on_page";echo "&lt;br&gt;";
<i> </i>
<i> </i>//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
<i> </i>mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
<i> </i>$conn = mysqli_connect("localhost","root","","powerpage");
<i> </i>$conn-&gt;set_charset('utf8mb4'); //Always set Charset.

<i> </i>if($conn === false)
<i> </i>{
<i> </i> die("ERROR: Connection Error!. " . mysqli_connect_error());
<i> </i>}

<i> </i>$query_2 = "SELECT * FROM users WHERE first_name = ? AND marital_status = ? ORDER BY id LIMIT $offset,$last_row_on_page";
<i> </i>$stmt_2 = mysqli_stmt_init($conn);
<i> </i>if(mysqli_stmt_prepare($stmt_2,$query_2))
<i> </i>{echo __LINE__; echo "&lt;br&gt;";
<i> </i> //mysqli_stmt_bind_param($stmt_2,"ss",$_POST["first_name"],$_POST["marital_status"]);//SUBSTITUTED THIS LINE FOR THE LINE BELOW.
<i> </i> mysqli_stmt_bind_param($stmt_2,"ss",$_SESSION["first_name"],$_SESSION["marital_status"]);//SUBSTITUTED TO THIS LINE FROM THE LINE ABOVE.
<i> </i> mysqli_stmt_execute($stmt_2);
<i> </i> $result_2 = mysqli_stmt_get_result($stmt_2);
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i> //Grab total number of pages to paginate.
<i> </i> $row_count = $_SESSION['row_count'];
<i> </i> //$total_pages = ceil($result_1/$result_per_page);
<i> </i> $total_pages = ceil($row_count/$result_per_page);
<i> </i>
<i> </i> echo "TOTAL PAGES: $total_pages&lt;br&gt;&lt;br&gt;";
<i> </i>
<i> </i> while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
<i> </i> {echo __LINE__; echo "&lt;br&gt;";
<i> </i> //Retrieve Values.
<i> </i> $id = $row["id"];
<i> </i> $first_name = $row["first_name"];
<i> </i> $middle_name = $row["middle_name"];
<i> </i> $surname = $row["surname"];
<i> </i> $gender = $row["gender"];
<i> </i> $marital_status = $row["marital_status"];
<i> </i> $working_status = $row["working_status"];
<i> </i>
<i> </i> echo "Id: $id&lt;br&gt;";
<i> </i> echo "First Name: $first_name&lt;br&gt;";
<i> </i> echo "Middle Name: $middle_name&lt;br&gt;";
<i> </i> echo "Surname: $surname&lt;br&gt;";
<i> </i> echo "Gender: $gender&lt;br&gt;";
<i> </i> echo "Marital Status: $marital_status&lt;br&gt;";
<i> </i> echo "Working Status: $working_status&lt;br&gt;";
<i> </i> echo "&lt;br&gt;";
<i> </i> echo "&lt;br&gt;";
<i> </i> }
<i> </i> $i = 1;
<i> </i> while($i&lt;=$total_pages)
<i> </i> {
<i> </i> if($i&lt;$total_pages)
<i> </i> {
<i> </i> echo "&lt;a href='http://localhost/power.page/stack.php?form_type=";?&gt;&lt;?php echo $_SESSION['form_type'];?&gt;&amp;query_type=&lt;?php echo $_SESSION['query_type'];?&gt;&amp;form_step=end&amp;page_limit=2&amp;page=&lt;?php echo $i;?&gt;'&gt;&lt;?php echo " $i ";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i> elseif($i==$page_number)
<i> </i> {
<i> </i> echo "&lt;a href='http://localhost/power.page/stack.php?form_type=";?&gt;&lt;?php echo $_SESSION['form_type'];?&gt;&amp;query_type=&lt;?php echo $_SESSION['query_type'];?&gt;&amp;form_step=end&amp;page_limit=2&amp;page=&lt;?php echo $i;?&gt;'&gt;&lt;?php echo "&lt;b&gt; $i &lt;/b&gt;";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i>
<i> </i> $i++;
<i> </i> }
<i> </i> if($page_number&gt;$total_pages)
<i> </i> {
<i> </i> echo "&lt;a href='http://localhost/power.page/stack.php?form_type=";?&gt;&lt;?php echo $_SESSION['form_type'];?&gt;&amp;query_type=&lt;?php echo $_SESSION['query_type'];?&gt;&amp;form_step=end&amp;page_limit=2&amp;page=&lt;?php echo $previous_page;?&gt;'&gt;&lt;?php echo "&lt;b&gt; Previous &lt;/b&gt;";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i>}
<i> </i>//Close Statement.
<i> </i>mysqli_stmt_close($stmt_2);
<i> </i>//Close Connection.
<i> </i>mysqli_close($conn);
<i> </i>
<i> </i>$_SESSION['form_step'] = 'end';
<i> </i>echo $_POST["first_name"];// THESE ARE FAILING TO ECHO!
<i> </i>echo $_POST["marital_status"];// THESE ARE FAILING TO ECHO!
<i> </i>//die();
}
?&gt;
&lt;form action="&lt;?php echo $_SERVER['PHP_SELF'];?&gt;?form_type=&lt;?php echo $_SESSION['form_type'];?&gt;&amp;query_type=&lt;?php echo $_SESSION['query_type'];?&gt;&amp;form_step=end&amp;page_limit=2&amp;page=1" method='POST' enctype='text/plain'&gt;
&lt;?php

//Added '*' (asterisk) to indicate the 'Text Field' is a 'required' one.
echo "&lt;label for="first_name"&gt;First Name *:&lt;/label&gt;
&lt;input type="text" name="first_name" placeholder="First Name" value = ""&gt;";?&gt;
&lt;br&gt;
&lt;?php
echo "&lt;label for="marital_status"&gt;Marital Status *:&lt;/label&gt;";
echo "&lt;select name="marital_status"&gt;";
echo "&lt;option value="single"&gt;Single&lt;/option&gt;";
echo "&lt;option value="married"&gt;Married&lt;/option&gt;";
echo "&lt;/select&gt;";
echo "&lt;br&gt;";
?&gt;
&lt;input type="submit" name="search" value="Search"&gt;
&lt;/form&gt;
Copy linkTweet thisAlerts:
@venkysivaJul 08.2020 — Thanks for sharing the solution

Varun Sethupathi

https://www.wisewebtek.com/
Copy linkTweet thisAlerts:
@developer_webauthorJul 08.2020 — @NogDog,

On my 7th version, it is now working.

So it means, the $_POSTs were getting deleted and not passed from page 1 to page 2, 3 etc on paginated pages. After dumping the $

$_
POST values to $_SESSION values they (user inputs) are now getting forwarded from page to page (after also I deleted on form type line "enctype=text/plain" part) and to their SQL queries and so now page 2 also showing matching row data.

So now what even stackoverflow.com guys failed to spot was spotted by someone somewhere else today and was spotted here by you NogDog. Both on same day! I give you 2 places programmers honour badges!

2 wks of frustration has now come to an end! :)


Anyway, one issue. I have 6 total rows out of them 5 match my keyword search. I put 2 rows to be displayed per page. That means all 5 matches should be spread across 3 pages. But they get spread across 2 where PAGE 1 displays 2 rows and PAGE 2 display 3 rows. Note, max only 2 rows were supposed to display!

I am told, that the last line is wrong:

<i>
</i>
$offset = (($page_number * $result_per_page) - $result_per_page); //Offset (Row Number that 'Starts' on page).
$last_row_on_page = ($page_number * $result_per_page); //(Row Number that 'Ends' on page).



I am told that the variable, other than the $offset, must not hold the last row's id (last row that should be displayed on the page, eg PAGE 2) but how many rows I want displayed on the page. Is that so ? I need you to confirm.

Should I change this:

<i>
</i>$offset = (($page_number * $result_per_page) - $result_per_page); //Offset (Row Number that 'Starts' on page).
$last_row_on_page = ($page_number * $result_per_page); //(Row Number that 'Ends' on page).

$query_2 = "SELECT * FROM users WHERE first_name = ? AND marital_status = ? ORDER BY id LIMIT $offset,$last_row_on_page";


To this instead ? Yes or no ?

<i>
</i>$offset = (($page_number * $result_per_page) - $result_per_page); //Offset (Row Number that 'Starts' on page).
$max_rows_on_page = 2;

$query_2 = "SELECT * FROM users WHERE first_name = ? AND marital_status = ? ORDER BY id LIMIT $offset,$max_rows_on_page ";




<i>
</i>&lt;?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
session_start();

//I WAS TOLD $_POST VALUES ARE LIKELY TO BE EMPTY ON PAGINATION PAGE 1,2,3,4,5. WAS ADVISED TO ADD THIS FOLLOWING CODE TO CHECK.
echo "&lt;pre&gt;SESSION:n".print_r($_SESSION, 1)."&lt;/pre&gt;";
echo "&lt;pre&gt;POST:n".print_r($_POST, 1)."&lt;/pre&gt;";
?&gt;

&lt;!DOCTYPE HTML"&gt;
&lt;html&gt;

&lt;head&gt;
&lt;meta name="viewport" content="width-device=width, initial-scale=1"&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php

if(!isset($_GET['query_type']) &amp;&amp; empty($_GET['query_type']))//'query_type' tells the script whether it is an INSERT or SELECT or UPDATE sql query. From this the script determines which type of form to display and what prepared statements lines to use.
{
die("Invalid Query!");
}
else
{
$_SESSION['query_type'] = $_GET['query_type'];
echo __LINE__; echo "&lt;br&gt;";//DELETE
}
echo __LINE__; echo "&lt;br&gt;";//DELETE

if(!isset($_GET['form_type']) &amp;&amp; empty($_GET['form_type']))//'form_type' tells the script which form to display. (Login, Reg, Submit Personal Details, Submit Link).
{
die("Invalid Form!");
}
else
{
$_SESSION['form_type'] = $_GET['form_type'];
echo __LINE__; echo "&lt;br&gt;";//DELETE
<br/>
<i> </i>if(!function_exists($_SESSION['form_type']))
<i> </i>{
<i> </i> die("Invalid Form!");
<i> </i>}
<i> </i>else
<i> </i>{echo __LINE__; echo "&lt;br&gt;";//DELETE
<i> </i> if(!isset($_SESSION['form_step']))// || $_SESSION['form_step'] != 'end')
<i> </i> {
<i> </i> $_SESSION['form_step'] = 'start'; echo $_SESSION['form_step'];
<i> </i> echo __LINE__; echo "&lt;br&gt;";//DELETE
<i> </i> $_SESSION['form_type']();
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> $_SESSION['form_step'] = $_GET['form_step'];
<i> </i> echo __LINE__; echo "&lt;br&gt;"; echo $_SESSION['form_step'];//DELETE
<i> </i> $_SESSION['form_type']();
<i> </i> }
<i> </i>}
}

function search()
{
echo __LINE__; echo "&lt;br&gt;";//DELETE
//Do following if "Search" button clicked.
if($_SERVER['REQUEST_METHOD'] === 'POST') //Doing following means "Search" button clicked AND pagination numbered links are NOT clicked. Eg page 2, 3, etc.
{echo __LINE__; echo "&lt;br&gt;";//DELETE
//Do following if "Search" button clicked.
if(isset($_POST['search']))
{echo __LINE__; echo "&lt;br&gt;";//DELETE
$_SESSION["first_name"] = $_POST["first_name"];//ADDED THIS NEW LINE.
$_SESSION["marital_status"] = $_POST["marital_status"];//ADDED THIS NEW LINE.
<br/>
<i> </i> rows_count();
<i> </i> fetch_rows();
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i> die;
<i> </i> }
<i> </i>}
<i> </i>else //Doing following means "Search" button not clicked but pagination numbered links are clicked. Eg page 2, 3, etc.
<i> </i>{
<i> </i> echo __LINE__; echo "&lt;br&gt;";//DELETE
<i> </i> //Do following if "Search" button not clicked but pagination numbered links are clicked. Eg Page 2, 3, etc..
<i> </i> //rows_count(); //SHOULD I RUN THIS FUNCTION AGAIN FROM PAGE 2,3,4, ETC OR NOT ?
<i> </i> fetch_rows();
<i> </i> echo __LINE__; echo "&lt;br&gt;";//DELETE
<i> </i>}
}


function rows_count()
{
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost","root","","powerpage");
$conn-&gt;set_charset('utf8mb4'); //Always set Charset.
<br/>
<i> </i>if($conn === false)
<i> </i>{
<i> </i> die("ERROR: Connection Error!. " . mysqli_connect_error());
<i> </i>}

<i> </i>$query_1 = "SELECT COUNT(id) FROM users WHERE first_name = ? AND marital_status = ?";
<i> </i>//$_SESSION["first_name"] = $_POST["first_name"];//ADDED THIS NEW LINE.
<i> </i>//$_SESSION["marital_status"] = $_POST["marital_status"];//ADDED THIS NEW LINE.
<i> </i>$stmt_1 = mysqli_stmt_init($conn);
<i> </i>if(mysqli_stmt_prepare($stmt_1,$query_1))
<i> </i>{
<i> </i> //mysqli_stmt_bind_param($stmt_1,"ss",$_POST["first_name"],$_POST["marital_status"]);//SUBSTITUTED THIS LINE FOR THE LINE BELOW.
<i> </i> mysqli_stmt_bind_param($stmt_1,"ss",$_SESSION["first_name"],$_SESSION["marital_status"]);//SUBSTITUTED TO THIS LINE FROM THE LINE ABOVE.
<i> </i> mysqli_stmt_execute($stmt_1);
<i> </i> $result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
<i> </i> mysqli_stmt_fetch($stmt_1);
<i> </i> $_SESSION['row_count'] = $row_count;
<i> </i> echo __LINE__; echo "&lt;br&gt;";//DELETE
<i> </i> $_SESSION['form_step'] = 'end';
<i> </i> //fetch_rows();
<i> </i>}
<i> </i>//Close Statement.
<i> </i>mysqli_stmt_close($stmt_1);
<i> </i>//Close Connection.
<i> </i>mysqli_close($conn);
}

function fetch_rows()
{ echo __LINE__; echo "&lt;br&gt;";//DELETE
$form_step = $_GET['form_step'];
<br/>
<i> </i>$page_number = $_GET['page'];
<i> </i>$result_per_page = $_GET['page_limit'];
<i> </i>$offset = (($page_number * $result_per_page) - $result_per_page); //Offset (Row Number that 'Starts' on page).
<i> </i>$last_row_on_page = ($page_number * $result_per_page); //Max Result (Row Number that 'Ends' on page).
<i> </i>$previous_page = $page_number-1;
<i> </i>$next_page = $page_number+1;
<i> </i>
<i> </i>echo "Row Start: $offset";echo "&lt;br&gt;";
<i> </i>echo "Row End: $last_row_on_page";echo "&lt;br&gt;";
<i> </i>
<i> </i>//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
<i> </i>mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
<i> </i>$conn = mysqli_connect("localhost","root","","powerpage");
<i> </i>$conn-&gt;set_charset('utf8mb4'); //Always set Charset.

<i> </i>if($conn === false)
<i> </i>{
<i> </i> die("ERROR: Connection Error!. " . mysqli_connect_error());
<i> </i>}

<i> </i>$query_2 = "SELECT * FROM users WHERE first_name = ? AND marital_status = ? ORDER BY id LIMIT $offset,$last_row_on_page";
<i> </i>$stmt_2 = mysqli_stmt_init($conn);
<i> </i>if(mysqli_stmt_prepare($stmt_2,$query_2))
<i> </i>{echo __LINE__; echo "&lt;br&gt;";
<i> </i> //mysqli_stmt_bind_param($stmt_2,"ss",$_POST["first_name"],$_POST["marital_status"]);//SUBSTITUTED THIS LINE FOR THE LINE BELOW.
<i> </i> mysqli_stmt_bind_param($stmt_2,"ss",$_SESSION["first_name"],$_SESSION["marital_status"]);//SUBSTITUTED TO THIS LINE FROM THE LINE ABOVE.
<i> </i> mysqli_stmt_execute($stmt_2);
<i> </i> $result_2 = mysqli_stmt_get_result($stmt_2);
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i> //Grab total number of pages to paginate.
<i> </i> $row_count = $_SESSION['row_count'];
<i> </i> //$total_pages = ceil($result_1/$result_per_page);
<i> </i> $total_pages = ceil($row_count/$result_per_page);
<i> </i>
<i> </i> echo "TOTAL PAGES: $total_pages&lt;br&gt;&lt;br&gt;";
<i> </i>
<i> </i> while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
<i> </i> {echo __LINE__; echo "&lt;br&gt;";
<i> </i> //Retrieve Values.
<i> </i> $id = $row["id"];
<i> </i> $first_name = $row["first_name"];
<i> </i> $middle_name = $row["middle_name"];
<i> </i> $surname = $row["surname"];
<i> </i> $gender = $row["gender"];
<i> </i> $marital_status = $row["marital_status"];
<i> </i> $working_status = $row["working_status"];
<i> </i>
<i> </i> echo "Id: $id&lt;br&gt;";
<i> </i> echo "First Name: $first_name&lt;br&gt;";
<i> </i> echo "Middle Name: $middle_name&lt;br&gt;";
<i> </i> echo "Surname: $surname&lt;br&gt;";
<i> </i> echo "Gender: $gender&lt;br&gt;";
<i> </i> echo "Marital Status: $marital_status&lt;br&gt;";
<i> </i> echo "Working Status: $working_status&lt;br&gt;";
<i> </i> echo "&lt;br&gt;";
<i> </i> echo "&lt;br&gt;";
<i> </i> }
<i> </i> $i = 1;
<i> </i> while($i&lt;=$total_pages)
<i> </i> {
<i> </i> if($i&lt;$total_pages)
<i> </i> {
<i> </i> echo "&lt;a href='http://localhost/power.page/stack.php?form_type=";?&gt;&lt;?php echo $_SESSION['form_type'];?&gt;&amp;query_type=&lt;?php echo $_SESSION['query_type'];?&gt;&amp;form_step=end&amp;page_limit=2&amp;page=&lt;?php echo $i;?&gt;'&gt;&lt;?php echo " $i ";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i> elseif($i==$page_number)
<i> </i> {
<i> </i> echo "&lt;a href='http://localhost/power.page/stack.php?form_type=";?&gt;&lt;?php echo $_SESSION['form_type'];?&gt;&amp;query_type=&lt;?php echo $_SESSION['query_type'];?&gt;&amp;form_step=end&amp;page_limit=2&amp;page=&lt;?php echo $i;?&gt;'&gt;&lt;?php echo "&lt;b&gt; $i &lt;/b&gt;";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i>
<i> </i> $i++;
<i> </i> }
<i> </i> if($page_number&gt;$total_pages)
<i> </i> {
<i> </i> echo "&lt;a href='http://localhost/power.page/stack.php?form_type=";?&gt;&lt;?php echo $_SESSION['form_type'];?&gt;&amp;query_type=&lt;?php echo $_SESSION['query_type'];?&gt;&amp;form_step=end&amp;page_limit=2&amp;page=&lt;?php echo $previous_page;?&gt;'&gt;&lt;?php echo "&lt;b&gt; Previous &lt;/b&gt;";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i>}
<i> </i>//Close Statement.
<i> </i>mysqli_stmt_close($stmt_2);
<i> </i>//Close Connection.
<i> </i>mysqli_close($conn);
<i> </i>
<i> </i>$_SESSION['form_step'] = 'end';
}
?&gt;
&lt;form action="&lt;?php echo $_SERVER['PHP_SELF'];?&gt;?form_type=&lt;?php echo $_SESSION['form_type'];?&gt;&amp;query_type=&lt;?php echo $_SESSION['query_type'];?&gt;&amp;form_step=end&amp;page_limit=2&amp;page=1" method='POST'&gt;
&lt;?php

//Added '*' (asterisk) to indicate the 'Text Field' is a 'required' one.
echo "&lt;label for="first_name"&gt;First Name *:&lt;/label&gt;
&lt;input type="text" name="first_name" placeholder="First Name" value = ""&gt;";?&gt;
&lt;br&gt;
&lt;?php
echo "&lt;label for="marital_status"&gt;Marital Status *:&lt;/label&gt;";
echo "&lt;select name="marital_status"&gt;";
echo "&lt;option value="single"&gt;Single&lt;/option&gt;";
echo "&lt;option value="married"&gt;Married&lt;/option&gt;";
echo "&lt;/select&gt;";
echo "&lt;br&gt;";
?&gt;
&lt;input type="submit" name="search" value="Search"&gt;
&lt;/form&gt;


<i>
</i>$page_number = $_GET['page'];
$result_per_page = $_GET['page_limit'];
$offset = (($page_number * $result_per_page) - $result_per_page); //Offset (Row Number that 'Starts' on page).
$last_row_on_page = ($page_number * $result_per_page); //Max Result (Row Number that 'Ends' on page).


I'd like to see how droopsnoot fixed this:
<i>
</i>$last_row_on_page = ($page_number * $result_per_page); //Max Result (Row Number that 'Ends' on page).


droopsnoot, show us your EDIT of me code that is working.

Thank You
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...