
2020
Members-
Posts
96 -
Joined
-
Last visited
Everything posted by 2020
-
Thank You. But you forgot to explain why you like option 4 over other 3. And, you forgot to echo the connection error. let me see how you echo your error. What format you use.
-
Hi, 3 different tutorials teach 3 different ways on how to get php mysqli echo connection errors. Which one should I memorise out of these 3 you advise ? I'd like your rankings on these 3 atleast .... 1. https://www.php.net/manual/en/function.mysqli-connect.php $link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db"); if (!$link) { echo "Error: Unable to connect to MySQL." . PHP_EOL; echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; exit; } mysqli_close($link); 2. https://www.tutorialrepublic.com/php-tutorial/php-mysql-connect.php $link = mysqli_connect("localhost", "root", ""); // Check connection if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); } // Print host information echo "Connect Successfully. Host info: " . mysqli_get_host_info($link); // Close connection mysqli_close($link); 3. https://www.geeksforgeeks.org/php-mysqli_connect-function/ mysqli_connect("localhost", "root", "", "GFG"); if(mysqli_connect_error()) echo "Connection Error."; else echo "Database Connection Successfully."; Q2. On the connection point check, you give your rankings on these following 3 based on your preference. I ranked like this: A. if(mysqli_connect_error()) B. if($link === false){ C. if (!$link) { Q3. On the connection error reporting point (error message echoing point), you give your rankings on these following 3 based on your preference. I ranked like this: A. die("ERROR: Could not connect. " . mysqli_connect_error()); B. echo "Connection Error."; C. echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; Briefly, let me know: A). Which connection error checking point to use and which error message echoing point to use. B). If you got your own that is better and more helpful then explain why it is better over these 3 and any others.
-
Hi, I built this reg-login file. Note, login.php asks for your login details. The webform (so to speak) uses SELECT sql query to check your login credentials. The reg.php asks for your new acc details. The webform (so to speak) uses INSERT sql query to add your details to db. I got my webform not displayed to you either as registration form or login form. It is a neutral form. It justs asks you for your email. Then checks against db. If it exists, it assumes you existing member and login() function takes over and logs you in. Else, registration() functions takes over and registers you. Note: On the login(), at the end when user is logged into his member account, his personal details get displayed on screen. I need you to check if this following line (especially) is correct or not: if($row = mysqli_fetch_array($result_3,MYSQLI_ASSOC)) 1. I want you to see if there any errors in my code that will result in malfunction or hacker sql injecting or hacking. 2. I need you to show me how to VALIDATE user input. VALIDATE email using 1). html5 & 2). php 7 email validation function plus 3.) with REGEX so nothing but email is inputted. Show me these 3 ways to check for email. I need you to show me how to VALIDATE user password. VALIDATE password using 1). html5 & 2). php 7 & 3.) with REGEX so nothing but password (A-Z, 0-9 ONLY) is inputted. And no other chars. Show me these 3 ways to check for password. From there, I should pick on fast from you and manage to VALIDATE username input. I don't know how to do these above 2 so kindly teach me by showing snippet with comments so i understand your snippet. NOTE: I did not complete the password prompt because I have forgotten how to do it with SHA256. Can someone show me a typical example how to query for password with SHA256 or whatever the latest strong algorithm is ? Show me code with comments so I understand what you doing with your code. I will the modify your snippet a little to suit my purpose and add it on the login(). I know I need to add the password prompt on the registration() too but you don't have to deal with that as I will complete it once I finish learning from you. You just teach me how to do it in the login() and from there I will take it on to add it on the registration(). Thank You! <?php session_start(); if($_SERVER['REQUEST_METHOD'] == 'POST') { if(!isset($_POST['email_account']) || !isset($_POST['email_service'])) { $email_error = "<font color='red'>Input Email Address!</color>"; } else { //Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME). $conn = mysqli_connect("localhost","root","","powerpage"); $conn->set_charset('utf8mb4'); //Always set Charset. if($conn === false) { die("ERROR: Connection Error!. " . mysqli_connect_error()); } else { //Set Parameters. $email = trim($_POST["email_account"]) . '@' . trim($_POST["email_service"]); $_SESSION['email'] = trim($_POST["email_account"]) . '@' . trim($_POST["email_service"]);//If this fails on test then replace it with above line echo "line 25 triggered: $email<br>"; $sql_query = "SELECT COUNT(personal_email) FROM users WHERE personal_email = ?"; $stmt = mysqli_prepare($conn,$sql_query); if($stmt == False) { //Close Connection. mysqli_close($conn); echo "Line 33<br>";//DELETE THIS die("<pre>Mysqli Prepare Failed!\n".mysqli_stmt_error($stmt)."\n$sql_query</pre>"); } else { mysqli_stmt_bind_param($stmt,'s',$email); if(!mysqli_stmt_execute($stmt)) { //Close Connection. mysqli_close($conn); die("Could not mysqli_stmt_execute! Please try again later!"); } $result = mysqli_stmt_get_result($stmt); if(mysqli_fetch_array($result, MYSQLI_NUM)[0])//WHY THIS NOT WORK UNLESS NUM ARRAY GIVEN ? { echo "Line 57 triggered: Function login() will trigger!<br>"; //DELETE THIS $_SESSION['session_type'] = 'login'; login(); } else { echo "Line 61 triggered: Function register() will trigger!<br>"; //DELETE THIS $_SESSION['session_type'] = 'register'; register(); } } } } } function register() { //if(!isset($_SESSION['session_type'] or $_SESSION['session_type'] != 'registration')//Nog Dog's copied & pasted line if(!isset($_SESSION['session_type']) || $_SESSION['session_type'] != 'register') { //Close Statement. mysqli_stmt_close($stmt); //Close Connection. mysqli_close($conn); die("Line 86: Could not check email! Please try again later!"); } //$email = trim($_POST["email_account"]) . '@' . trim($_POST["email_service"]); $email = $_SESSION['email'];//If this fails on test then replace it with above line //Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME). $conn = mysqli_connect("localhost","root","","powerpage"); //Prepare an INSERT Statement. $sql_query_2 = "INSERT INTO users (personal_email) VALUES (?)"; if(!$stmt_2 = mysqli_prepare($conn,$sql_query_2)) { //Close Connection. mysqli_close($conn); die("Could not register! Please try again later!"); } else { //Bind Variables to the Prepared Statement as parameters. mysqli_stmt_bind_param($stmt_2,'s',$email); //Attempt to execute the Prepared Statement. if(!mysqli_stmt_execute($stmt_2)) { //Close Statement. mysqli_stmt_close($stmt_2); //Close Connection. mysqli_close($conn); die("Could not register! Please try again later!"); } mail(); } } function login() { if(!isset($_SESSION['session_type']) || $_SESSION['session_type'] != 'login') { //Close Statement. mysqli_stmt_close($stmt); //Close Connection. mysqli_close($conn); die("Could not check email! Please try again later!"); } //$email = trim($_POST["email_account"]) . '@' . trim($_POST["email_service"]); $email = $_SESSION['email'];//If this fails on test then replace it with above line //Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME). $conn = mysqli_connect("localhost","root","","powerpage"); //Prepare a Select Statement. $sql_query_3 = "SELECT id,username,first_name,middle_name,surname,gender,age_range FROM users WHERE personal_email = ?"; if(!$stmt_3 = mysqli_prepare($conn,$sql_query_3)) { //Close Statement. mysqli_stmt_close($stmt_3); //Close Connection. mysqli_close($conn); die("Could not check email! Please try again later!"); } else { //Bind Variables to the Prepared Statement as parameters. mysqli_stmt_bind_param($stmt_3,'s',$email); //Attempt to execute the Prepared Statement. if(!mysqli_stmt_execute($stmt_3)) { //Close Statement. mysqli_stmt_close($stmt_3); //Close Connection. mysqli_close($conn); die("Could not check email! Please try again later!"); } //mysqli_stmt_bind_result($stmt,$email); $result_3 = mysqli_stmt_get_result($stmt_3); //if(mysqli_fetch_array($result_3, MYSQLI_NUM)) //Fetch result row as an associative array. Since the result set contains only one row, we don't need to use the 'While loop'. //mysqli_stmt_fetch($stmt);//use this if you use 'mysqli_stmt_bind_result($stmt,$email). if($row = mysqli_fetch_array($result_3,MYSQLI_ASSOC)) //Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of 'mysqli_stmt_bind_result($stmt,$email)'. { //Retrieve Values. $id = $row["id"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)'; $username = $row["username"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)'; $first_name = $row["first_name"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)'; $middle_name = $row["middle_name"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)'; $surname = $row["surname"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)'; $gender = $row["gender"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)'; $age_range = $row["age_range"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)'; echo "Id: $id<br>"; echo "Username: $username<br>"; echo "First Name: $first_name<br>"; echo "Middle Name: $middle_name<br>"; echo "Surname: $surname<br>"; echo "Gender: $gender<br>"; echo "Age Range: $age_range<br>"; //Close Statement. mysqli_stmt_close($stmt_3); //Close Connection. mysqli_close($conn); } } } //DO NOT NEED TO REDO THE HTML CODE BELOW AS WAS NOT COPY & PASTE FROM ELESEWHERE .... ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta name="viewport" content="width=device=width, initial-scale=1"> </head> <body> <form action="" method="post"> <label for="email_account">Email:</label> <input type="text" name="email_account" id="email_first_part" placeholder="Email Address before '@'"> <label for="email_service"><b>@</b></label> <input type="text" name="email_service" id="email_last_part" placeholder="Email Address after '@'"> <?php if(!empty($email_error)){echo $email_error;}?> <br> <button type="submit" class="login_register" name="login_register">Register/Login</button> </body> <html> <?php ?>
-
I did say I will do the validations at the end once the current issue is resolved. It now is and so I will get to the validation. And if I get stuck on it then I will pester you. ;) As for the error messages in an array. Like I said before, I can't get my head round it and so do you mind showing a very simple example ? I will try picking up from that. Remember, I am not even an intermediate yet. And so, everything I get from you people is a learning for me. Thanks
-
Hello Requinix, One day, if I remember you, I will explain why I did it. Ok ? I know you want things done in simplified mode but I have my reasons for doing it this way. Ok ?
-
@macguyver, After replying to my last few posts, do close this thread.
-
@macguyver, 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 elses! I give them 2 places programmers honour badges! 2 wks of frustration has now come to an end! $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). <?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 "<pre>SESSION:\n".print_r($_SESSION, 1)."</pre>"; echo "<pre>POST:\n".print_r($_POST, 1)."</pre>"; ?> <!DOCTYPE HTML"> <html> <head> <meta name="viewport" content="width-device=width, initial-scale=1"> </head> <body> <?php if(!isset($_GET['query_type']) && 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 "<br>";//DELETE } echo __LINE__; echo "<br>";//DELETE if(!isset($_GET['form_type']) && 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 "<br>";//DELETE if(!function_exists($_SESSION['form_type'])) { die("Invalid Form!"); } else {echo __LINE__; echo "<br>";//DELETE if(!isset($_SESSION['form_step']))// || $_SESSION['form_step'] != 'end') { $_SESSION['form_step'] = 'start'; echo $_SESSION['form_step']; echo __LINE__; echo "<br>";//DELETE $_SESSION['form_type'](); } else { $_SESSION['form_step'] = $_GET['form_step']; echo __LINE__; echo "<br>"; echo $_SESSION['form_step'];//DELETE $_SESSION['form_type'](); } } } function search() { echo __LINE__; echo "<br>";//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 "<br>";//DELETE //Do following if "Search" button clicked. if(isset($_POST['search'])) {echo __LINE__; echo "<br>";//DELETE $_SESSION["first_name"] = $_POST["first_name"];//ADDED THIS NEW LINE. $_SESSION["marital_status"] = $_POST["marital_status"];//ADDED THIS NEW LINE. rows_count(); fetch_rows(); echo __LINE__; echo "<br>"; die; } } else //Doing following means "Search" button not clicked but pagination numbered links are clicked. Eg page 2, 3, etc. { echo __LINE__; echo "<br>";//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 "<br>";//DELETE } } function rows_count() { 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 = ?"; //$_SESSION["first_name"] = $_POST["first_name"];//ADDED THIS NEW LINE. //$_SESSION["marital_status"] = $_POST["marital_status"];//ADDED THIS NEW LINE. $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"]);//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. 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; echo __LINE__; echo "<br>";//DELETE $_SESSION['form_step'] = 'end'; //fetch_rows(); } //Close Statement. mysqli_stmt_close($stmt_1); //Close Connection. mysqli_close($conn); } function fetch_rows() { echo __LINE__; echo "<br>";//DELETE $form_step = $_GET['form_step']; $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). $previous_page = $page_number-1; $next_page = $page_number+1; echo "Row Start: $offset";echo "<br>"; echo "Row End: $last_row_on_page";echo "<br>"; //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_2 = "SELECT * FROM users WHERE first_name = ? AND marital_status = ? ORDER BY id LIMIT $offset,$last_row_on_page"; $stmt_2 = mysqli_stmt_init($conn); if(mysqli_stmt_prepare($stmt_2,$query_2)) {echo __LINE__; echo "<br>"; //mysqli_stmt_bind_param($stmt_2,"ss",$_POST["first_name"],$_POST["marital_status"]);//SUBSTITUTED THIS LINE FOR THE LINE BELOW. mysqli_stmt_bind_param($stmt_2,"ss",$_SESSION["first_name"],$_SESSION["marital_status"]);//SUBSTITUTED TO THIS LINE FROM THE LINE ABOVE. mysqli_stmt_execute($stmt_2); $result_2 = mysqli_stmt_get_result($stmt_2); echo __LINE__; echo "<br>"; //Grab total number of pages to paginate. $row_count = $_SESSION['row_count']; //$total_pages = ceil($result_1/$result_per_page); $total_pages = ceil($row_count/$result_per_page); echo "TOTAL PAGES: $total_pages<br><br>"; while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC)) {echo __LINE__; echo "<br>"; //Retrieve Values. $id = $row["id"]; $first_name = $row["first_name"]; $middle_name = $row["middle_name"]; $surname = $row["surname"]; $gender = $row["gender"]; $marital_status = $row["marital_status"]; $working_status = $row["working_status"]; echo "Id: $id<br>"; echo "First Name: $first_name<br>"; echo "Middle Name: $middle_name<br>"; echo "Surname: $surname<br>"; echo "Gender: $gender<br>"; echo "Marital Status: $marital_status<br>"; echo "Working Status: $working_status<br>"; echo "<br>"; echo "<br>"; } $i = 1; while($i<=$total_pages) { if($i<$total_pages) { echo "<a href='http://localhost/power.page/stack.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $i;?>'><?php echo " $i ";?></a><?php } elseif($i==$page_number) { echo "<a href='http://localhost/power.page/stack.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $i;?>'><?php echo "<b> $i </b>";?></a><?php } $i++; } if($page_number>$total_pages) { echo "<a href='http://localhost/power.page/stack.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $previous_page;?>'><?php echo "<b> Previous </b>";?></a><?php } } //Close Statement. mysqli_stmt_close($stmt_2); //Close Connection. mysqli_close($conn); $_SESSION['form_step'] = 'end'; } ?> <form action="<?php echo $_SERVER['PHP_SELF'];?>?form_type=<?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=1" method='POST'> <?php //Added '*' (asterisk) to indicate the 'Text Field' is a 'required' one. echo "<label for=\"first_name\">First Name *:</label> <input type=\"text\" name=\"first_name\" placeholder=\"First Name\" value = \"\">";?> <br> <?php echo "<label for=\"marital_status\">Marital Status *:</label>"; echo "<select name=\"marital_status\">"; echo "<option value=\"single\">Single</option>"; echo "<option value=\"married\">Married</option>"; echo "</select>"; echo "<br>"; ?> <input type="submit" name="search" value="Search"> </form> $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). $last_row_on_page = ($page_number * $result_per_page); //Max Result (Row Number that 'Ends' on page). 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: $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: $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 ? $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 "; 7th Version: <?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 "<pre>SESSION:\n".print_r($_SESSION, 1)."</pre>"; echo "<pre>POST:\n".print_r($_POST, 1)."</pre>"; ?> <!DOCTYPE HTML"> <html> <head> <meta name="viewport" content="width-device=width, initial-scale=1"> </head> <body> <?php if(!isset($_GET['query_type']) && 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 "<br>";//DELETE } echo __LINE__; echo "<br>";//DELETE if(!isset($_GET['form_type']) && 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 "<br>";//DELETE if(!function_exists($_SESSION['form_type'])) { die("Invalid Form!"); } else {echo __LINE__; echo "<br>";//DELETE if(!isset($_SESSION['form_step']))// || $_SESSION['form_step'] != 'end') { $_SESSION['form_step'] = 'start'; echo $_SESSION['form_step']; echo __LINE__; echo "<br>";//DELETE $_SESSION['form_type'](); } else { $_SESSION['form_step'] = $_GET['form_step']; echo __LINE__; echo "<br>"; echo $_SESSION['form_step'];//DELETE $_SESSION['form_type'](); } } } function search() { echo __LINE__; echo "<br>";//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 "<br>";//DELETE //Do following if "Search" button clicked. if(isset($_POST['search'])) {echo __LINE__; echo "<br>";//DELETE $_SESSION["first_name"] = $_POST["first_name"];//ADDED THIS NEW LINE. $_SESSION["marital_status"] = $_POST["marital_status"];//ADDED THIS NEW LINE. rows_count(); fetch_rows(); echo __LINE__; echo "<br>"; die; } } else //Doing following means "Search" button not clicked but pagination numbered links are clicked. Eg page 2, 3, etc. { echo __LINE__; echo "<br>";//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 "<br>";//DELETE } } function rows_count() { 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 = ?"; //$_SESSION["first_name"] = $_POST["first_name"];//ADDED THIS NEW LINE. //$_SESSION["marital_status"] = $_POST["marital_status"];//ADDED THIS NEW LINE. $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"]);//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. 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; echo __LINE__; echo "<br>";//DELETE $_SESSION['form_step'] = 'end'; //fetch_rows(); } //Close Statement. mysqli_stmt_close($stmt_1); //Close Connection. mysqli_close($conn); } function fetch_rows() { echo __LINE__; echo "<br>";//DELETE $form_step = $_GET['form_step']; $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). $max_on_page = ($page_number * $result_per_page); //(Row Number that 'Ends' on page). $previous_page = $page_number-1; $next_page = $page_number+1; echo "Row Start: $offset";echo "<br>"; echo "Row End: $last_row_on_page";echo "<br>"; //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_2 = "SELECT * FROM users WHERE first_name = ? AND marital_status = ? ORDER BY id LIMIT $offset,$last_row_on_page"; $stmt_2 = mysqli_stmt_init($conn); if(mysqli_stmt_prepare($stmt_2,$query_2)) {echo __LINE__; echo "<br>"; //mysqli_stmt_bind_param($stmt_2,"ss",$_POST["first_name"],$_POST["marital_status"]);//SUBSTITUTED THIS LINE FOR THE LINE BELOW. mysqli_stmt_bind_param($stmt_2,"ss",$_SESSION["first_name"],$_SESSION["marital_status"]);//SUBSTITUTED TO THIS LINE FROM THE LINE ABOVE. mysqli_stmt_execute($stmt_2); $result_2 = mysqli_stmt_get_result($stmt_2); echo __LINE__; echo "<br>"; //Grab total number of pages to paginate. $row_count = $_SESSION['row_count']; //$total_pages = ceil($result_1/$result_per_page); $total_pages = ceil($row_count/$result_per_page); echo "TOTAL PAGES: $total_pages<br><br>"; while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC)) {echo __LINE__; echo "<br>"; //Retrieve Values. $id = $row["id"]; $first_name = $row["first_name"]; $middle_name = $row["middle_name"]; $surname = $row["surname"]; $gender = $row["gender"]; $marital_status = $row["marital_status"]; $working_status = $row["working_status"]; echo "Id: $id<br>"; echo "First Name: $first_name<br>"; echo "Middle Name: $middle_name<br>"; echo "Surname: $surname<br>"; echo "Gender: $gender<br>"; echo "Marital Status: $marital_status<br>"; echo "Working Status: $working_status<br>"; echo "<br>"; echo "<br>"; } $i = 1; while($i<=$total_pages) { if($i<$total_pages) { echo "<a href='http://localhost/power.page/stack.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $i;?>'><?php echo " $i ";?></a><?php } elseif($i==$page_number) { echo "<a href='http://localhost/power.page/stack.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $i;?>'><?php echo "<b> $i </b>";?></a><?php } $i++; } if($page_number>$total_pages) { echo "<a href='http://localhost/power.page/stack.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $previous_page;?>'><?php echo "<b> Previous </b>";?></a><?php } } //Close Statement. mysqli_stmt_close($stmt_2); //Close Connection. mysqli_close($conn); $_SESSION['form_step'] = 'end'; } ?> <form action="<?php echo $_SERVER['PHP_SELF'];?>?form_type=<?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=1" method='POST'> <?php //Added '*' (asterisk) to indicate the 'Text Field' is a 'required' one. echo "<label for=\"first_name\">First Name *:</label> <input type=\"text\" name=\"first_name\" placeholder=\"First Name\" value = \"\">";?> <br> <?php echo "<label for=\"marital_status\">Marital Status *:</label>"; echo "<select name=\"marital_status\">"; echo "<option value=\"single\">Single</option>"; echo "<option value=\"married\">Married</option>"; echo "</select>"; echo "<br>"; ?> <input type="submit" name="search" value="Search"> </form>
-
Mac, On the other hand, I am not switching these to $_POST from $_GETs: Folks, My form method is not GET but POST. Concentrate on my code on my op. It is this: [code] Since I am switching form method back to POST, then I have to switch these $_GETs to $_POSTs. Right ? [code] if($_SESSION['form_step'] == 'start'); { if(!isset($_GET['form_type']) && empty($_GET['form_type'])) { die("Invalid Form!"); } else { $_SESSION['form_type'] = $_GET['form_type']; if(!isset($_GET['query_type']) && empty($_GET['query_type'])) { die("Invalid Query!"); } else { $_SESSION['query_type'] = $_GET['query_type']; if(!function_exists($_SESSION['form_type'])) { die("Invalid Form!"); } else { $_SESSION['form_type'](); } } } Simply because these params in the url were not user inputs. These params are just part of pagination params. Should I leave them as they are ? Macguyver ? They are easily being $_GET without any hiccups.
-
Mac, Since I am switching form method back to POST, then I have to switch these $_GETs to $_POSTs. Right ? if($_SESSION['form_step'] == 'start'); { if(!isset($_GET['form_type']) && empty($_GET['form_type'])) { die("Invalid Form!"); } else { $_SESSION['form_type'] = $_GET['form_type']; if(!isset($_GET['query_type']) && empty($_GET['query_type'])) { die("Invalid Query!"); } else { $_SESSION['query_type'] = $_GET['query_type']; if(!function_exists($_SESSION['form_type'])) { die("Invalid Form!"); } else { $_SESSION['form_type'](); } } }
-
Mac, You told me to switch form method to GET from POST. I did. When i submit form, I am forwarded to: http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW_1.php?first_name=first_name4&marital_status=single&search=Search I do not want the user's inputs in the url as params. Imagine the form asked 20 questions, if all user inputs get chucked in the url as params then the url would become way over 255. url will be truncated or whatever. I see future issues arising here. I understand why you suggested the $_GET because my pagination links had params in the url: $i = 1; while($i<=$total_pages) { if($i<$total_pages) { echo "<a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW_1.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $i;?>'><?php echo " $i ";?></a><?php } elseif($i==$page_number) { echo "<a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW_1.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $i;?>'><?php echo "<b> $i </b>";?></a><?php } $i++; } if($page_number>$total_pages) { echo "<a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW_1.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $previous_page;?>'><?php echo "<b> Previous </b>";?></a><?php } Stuck now. Don't know how to proceed from here. I have no choice but to switch back to form method $_POST.
-
NOTE: $_SESSION['form_type'] = search. And so, this following line triggers the search(): $_SESSION['form_type']();
-
I wanted to solve the current big issue FIRST by preventing $_SESSION['row_count'] = 5 auto switching to $_SESSION['row_count'] = 0 when I click over to page 2,3,4,5 on pagination. After that, do as you advised. Why later ? Because, it will take time to do all that you suggest since I am new in php. Have to learn a lot to do all that. For example, the error array thing, I could never really get my head round that one even though I have seen others coding it that way. How-about you show me an example how to do it ? As for validating inputs, you suspect my url params and/or $_POSTs contains a string instead of an INT and so best validate the $_GETs & $_POSTs. Yes, I can add filters later-on but the inputs I can assure you is not faulty. Let's rid the cancer first then put make-up on later. Atleast tell me this. When I do a search for a keyword and 5 matching rows are found and $_SESSION['row_count'] = 5 and page 1 manages to show matching row. Then, when I clickover to PAGE 2/3/4/5 then why does $_SESSION['row_count'] = 5 auto switch to $_SESSION['row_count'] = 0. Due to this auto switching, my script assumes there are 0 row matches for pages 2/3/4/5 and so displays no result. Even though there are matching rows to be displayed on pages 2/3/4 & 5. Tell me atleast that, if you managed to figure-out what is causing the switch. If I can stop this auto switching of values then my PAGINATION is complete, even without all those VALIDATORs you suggested. But, for my security purpose, I will add those VALIDATORS as you suggested and so don't think I am ignoring your advice. It's just I need to fix the cancer first. Else, I can't concentrate on all those other side stuffs which are not really creating any problem for my current issue of this dratted $SESSION value auto switching. How do you prevent the auto switching ?
-
Bump!
-
@Macguyver, I have created membership (login/register, logout, account homepage, pagination, etc.) pages before with helps of course. The membership script had about 10 different pages. My new project is that, the membership script should be all one page. So, login, logout, register, search 9pagination) all in one page. What I just gave in this thread is the pagination part of the page that i am stuck on. The reg & login parts already work. I did not bother mentioning them parts in this thread to keep the code to the point where I am having issue. So, there you go. Now you know the full picture. Anyway, I was suggested to add the session start at the top of the page right under the error reporting code and i did but no luck. I even switched the form method from post to get, like you suggested, but no luck. As for writing errors in a different file or whatever you are advising, I have never done it and so don't know how to proceed, unless ofcourse you can point me in the right direction with a code snippet or a link to a url. I have searched all over google for a tutorial with keywords like these but no luck for over a wk now: pagination tutorial AND mysqli AND prepared statements AND php Most tutorials are in oop or pdo or mysql extension. Or, they show tutorials on how to build pagination that displays whole table records (all rows). They don't show tutorials (mysqli, prepared statement) how to show records based on keyword search. That is why I am doing things all by myself reading manual and getting ideas from tutorials here and there that are based on oop or pdo or mysql extension or based on "show all table records". That issue of $_SESSION['row_count'] = 5 switching to $_SESSION['row_count'] = 0 on every page load (same page, remember!), still remains. Remember, this membership script is a one page script. If I can get over this hurdle of why the God Forsaken $_SESSION['row_count'] = 5 switches to $_SESSION['row_count'] = 0 on every same page load, then this 2-3 months project is complete!
-
$query_2 = "SELECT id,first_name,middle_name,surname,gender,marital_status,working_status FROM users WHERE first_name = ? AND marital_status = ? ORDER by id LIMIT $offset,$last_row_on_page"; Mac, I am told the above query is faulty. I should have an $offset and a $number that tells how many rows should be displayed on the page, be it PAGE 1, PAGE 2 or PAGE 100. Remember, we are talking about PAGINATION here. Let us say, I want 10 results per pagination page. So, what should I switch my followings to now (I confused) ? $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). $previous_page = $page_number-1; $next_page = $page_number+1; echo "Row Start: $offset";echo "<br>"; echo "Row End: $last_row_on_page";echo "<br>";
-
Mac, I just realized that, $_SESSION['row_count'] = 5, when I click the SEARCH button. 5 matching rows found. Then when I click PAGE 2 on pagination, the $_SESSION['row_count'] = 0, why is that ? It should stay 5. i am not overwriting the variable value either. This is the reason why, when I click PAGE 2 or PAGE 3 on the PAGINATION section, I see zero results or rows shown. No rows get shown beyond page 1. If I can findout why the '$_SESSION['row_count'] = 5' auto becomes $_SESSION['row_count'] = 0, then mystery solved. Look, after clicking the SEARCH button, this part of code yields $_SESSION['row_count'] = 5. So far so good. $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; I get shown matching rows on PAGE 1. Since I set it to display 1 row per page, I am shown 1 matching row. So far, so good. Now, when I click PAGE 2 on the PAGINATION section, I expect to see the 2nd matching row, but "$_SESSION['row_count'] = 5" switches to "$_SESSION['row_count'] = 0" and so no matching rows get shown. Why the switching of values from '5' to '0' when I click PAGE 2 ? This illegal switching ruins this following query that runs when I click PAGE 2 or any PAGE (eg PAGE 3) beyond PAGE 1: $row_count = $_SESSION['row_count']; //$total_pages = ceil($result_1/$result_per_page); $total_pages = ceil($row_count/$result_per_page); Context: $query_2 = "SELECT id,first_name,middle_name,surname,gender,marital_status,working_status FROM users WHERE first_name = ? AND marital_status = ? LIMIT $offset,$last_row_on_page"; echo "$query_2<br>"; $stmt_2 = mysqli_stmt_init($conn); if(mysqli_stmt_prepare($stmt_2,$query_2)) {echo __LINE__; echo "<br>";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 111. mysqli_stmt_bind_param($stmt_2,"ss",$_POST["first_name"],$_POST["marital_status"]); mysqli_stmt_execute($stmt_2); $result_2 = mysqli_stmt_get_result($stmt_2); echo __LINE__; echo "<br>";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 114. //Grab total number of pages to paginate. $row_count = $_SESSION['row_count']; //$total_pages = ceil($result_1/$result_per_page); $total_pages = ceil($row_count/$result_per_page); Do check my original post's code to see if you can figure-out why the "$_SESSION['row_count']" switches from "5" to "0". Thanks
-
Mac, Don't know much about .ini file stuff. Still a beginner. Anyway, I googled and found this: https://www.tutorialspoint.com/how-to-display-errors-in-php-file And so, I added: <?php error_reporting(E_ALL); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); ?> I get no errors. Also, if you check my url and params I grab from the url via $_GEt and $_SESSIONs, you will see I have not missed anything out.
-
I get no errors. Error Reporting is on.
-
Mac, I do provide parameters via $_GET and $_SESSION. Like this: $_SESSION['form_step'] = 'end'; $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). Only these above are required for the mysql queries. Here is an example of a paginated link: <a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $i;?>'><?php echo " $i ";?></a> Look at all these $_GETs: <?php session_start(); if(!isset($_GET['query_type']) && empty($_GET['query_type'])) { die("Invalid Query!"); } else { $_SESSION['query_type'] = $_GET['query_type']; echo __LINE__; echo "<br>";//DELETE } echo __LINE__; echo "<br>";//DELETE if(!isset($_GET['form_type']) && empty($_GET['form_type'])) { die("Invalid Form!"); } else { $_SESSION['form_type'] = $_GET['form_type']; echo __LINE__; echo "<br>";//DELETE if(!function_exists($_SESSION['form_type'])) { die("Invalid Form!"); } else {echo __LINE__; echo "<br>";//DELETE if(!session_id() || !isset($_SESSION['form_step']) || $_SESSION['form_step'] != 'end') { $_SESSION['form_step'] = 'start'; echo __LINE__; echo "<br>";//DELETE $_SESSION['form_type'](); } } } Which param am I missing ? I will look into the http_build_query(). I once looked into it but have forgotten what it is. As for my unorthodox links on the pagination section, they are rough for now.
-
Good Evening Php Programmers, I am trying to build a pagination script with mysqli using procedural style programming. Am a beginner. Not into oop yet or pdo. Bear that in mind if you show code samples. Else, i won't understand your snippets. Anyway, here's how my script works ... When you click SUBMIT button, initially the rows_count() uses following query to get the matching rows number: $query_2 = "SELECT * FROM users WHERE first_name = ? AND marital_status = ? LIMIT $offset,$last_row_on_page"; Then it forwards you to the fetch_rows() that fetches the rows and displays them in a pagination style. When you click any page numbers on the pagination section, like page 2, then the fetch_rows() is supposed to fetch the relevant rows again for page 2. Note, rows_count() doesn't play here as I deem not necessary to re-count all matched rows. Rows fetching is done with this query: $query_2 = "SELECT * FROM users WHERE first_name = ? AND marital_status = ? LIMIT $offset,$last_row_on_page"; It displays the matching rows using this WHILE loop: while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC)) BIG ISSUE: The fetch_rows() or $query_2 fails to fetch any matching rows beyond page 1 no matter what page you click. Be it page 2, 3, 4, etc. However, it does manage to fetch all matching rows for page 1. Just not beyond page 1. That is the main problem. Struggling for about 4 nights now. Can't get over this hurdle! Code is set to display 1 row per page for testing purpose. Since there are 5 matching rows in my mysql db, rows are supposed to be spread across many pages via pagination. You can easily see which lines I am having trouble with if you notice the CAPITALISED comments in my code. //Do following if "Search" button clicked. if($_SERVER['REQUEST_METHOD'] === 'POST') {echo __LINE__; echo "<br>";//DELETE //Do following if "Search" button clicked. if(isset($_POST['search'])) {echo __LINE__; echo "<br>";//DELETE rows_count(); //This function will forward script flow to fetch_rows() before halting the script. die(); } } echo __LINE__; echo "<br>";//On PAGINATION PAGE 2, THIS FAILS TO ECHO. IT IS LINE: 24. //Do following if "Search" button not clicked but pagination numbered links are clicked. Eg Page 1, 2, 3, etc.. fetch_rows(); //On PAGINATION PAGE 2, THIS FUNCTION IS NOT GETTING TRIGGERED! WHY ? echo __LINE__; echo "<br>";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 205. If you really want to help then best fire-up your Xampp/Wampp and feed my code and test it. Here is the full code for your convenience. You won't understand the code without the context. Hence the full code ... DEVMODE CONTEXT: <?php error_reporting(E_ALL); ?> <!DOCTYPE HTML"> <html> <head> <meta name="viewport" content="width-device=width, initial-scale=1"> </head> <body> <?php session_start(); if(!isset($_GET['query_type']) && empty($_GET['query_type'])) { die("Invalid Query!"); } else { $_SESSION['query_type'] = $_GET['query_type']; echo __LINE__; echo "<br>";//DELETE } echo __LINE__; echo "<br>";//DELETE if(!isset($_GET['form_type']) && empty($_GET['form_type'])) { die("Invalid Form!"); } else { $_SESSION['form_type'] = $_GET['form_type']; echo __LINE__; echo "<br>";//DELETE if(!function_exists($_SESSION['form_type'])) { die("Invalid Form!"); } else {echo __LINE__; echo "<br>";//DELETE if(!session_id() || !isset($_SESSION['form_step']) || $_SESSION['form_step'] != 'end') { $_SESSION['form_step'] = 'start'; echo __LINE__; echo "<br>";//DELETE $_SESSION['form_type'](); } } } //FUNCTIONS START FROM HERE function search() {echo __LINE__; echo "<br>";//DELETE function rows_count() { //Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME). $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; echo __LINE__; echo "<br>";//DELETE $_SESSION['form_step'] = 'end'; fetch_rows(); } } function fetch_rows() { echo __LINE__; echo "<br>";//DELETE $form_step = $_GET['form_step']; $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). $previous_page = $page_number-1; $next_page = $page_number+1; echo "Row Start: $offset";echo "<br>"; echo "Row End: $last_row_on_page";echo "<br>"; //Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME). $conn = mysqli_connect("localhost","root","","powerpage"); $conn->set_charset('utf8mb4'); //Always set Charset. if($conn === false) { die("ERROR: Connection Error!. " . mysqli_connect_error()); } $query_2 = "SELECT * FROM users WHERE first_name = ? AND marital_status = ? LIMIT $offset,$last_row_on_page"; $stmt_2 = mysqli_stmt_init($conn); if(mysqli_stmt_prepare($stmt_2,$query_2)) {echo __LINE__; echo "<br>";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 103. mysqli_stmt_bind_param($stmt_2,"ss",$_POST["first_name"],$_POST["marital_status"]); mysqli_stmt_execute($stmt_2); $result_2 = mysqli_stmt_get_result($stmt_2); if(!$result_2) { //Close Connection. mysqli_close($conn); die("<pre>2c. Statement Fetching failed!</pre>"); } else {echo __LINE__; echo "<br>";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 114. //Grab total number of pages to paginate. $row_count = $_SESSION['row_count']; //$total_pages = ceil($result_1/$result_per_page); $total_pages = ceil($row_count/$result_per_page); echo "TOTAL PAGES: $total_pages<br><br>"; while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))//On PAGE 2, PHP IGNORING THIS AND BYPASSING THIS WHOLE WHILE LOOP ON PAGE 2. IT IS LINE: 122. {echo __LINE__; echo "<br>";//On PAGE 2, THIS FAILS TO ECHO. IT IS LINE: 123. PHP IGNORING IT BYPASSING IT ON PAGE 2. //Retrieve Values. $id = $row["id"]; $first_name = $row["first_name"]; $middle_name = $row["middle_name"]; $surname = $row["surname"]; $gender = $row["gender"]; $marital_status = $row["marital_status"]; $working_status = $row["working_status"]; echo "Id: $id<br>"; echo "First Name: $first_name<br>"; echo "Middle Name: $middle_name<br>"; echo "Surname: $surname<br>"; echo "Gender: $gender<br>"; echo "Marital Status: $marital_status<br>"; echo "Working Status: $working_status<br>"; echo "<br>"; echo "<br>"; $i = 1; while($i<=$total_pages) { if($i<$total_pages) { echo "<a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=1&page=<?php echo $i;?>'><?php echo " $i ";?></a><?php } elseif($i==$page_number) { echo "<a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=1&page=<?php echo $i;?>'><?php echo "<b> $i </b>";?></a><?php } $i++; } if($page_number>$total_pages) { echo "<a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=1&page=<?php echo $previous_page;?>'><?php echo "<b> Previous </b>";?></a><?php } } } } $_SESSION['form_step'] = 'end'; } ?> <form action="<?php echo $_SERVER['PHP_SELF'];?>?form_type=<?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=1&page=1" method='post' enctype='plain/text'> <?php //Added '*' (asterisk) to indicate the 'Text Field' is a 'required' one. echo "<label for=\"first_name\">First Name *:</label> <input type=\"text\" name=\"first_name\" placeholder=\"First Name\" value = \"\">";?> <br> <?php echo "<label for=\"marital_status\">Marital Status *:</label>"; echo "<select name=\"marital_status\">"; echo "<option value=\"single\">Single</option>"; echo "<option value=\"married\">Married</option>"; echo "</select>"; echo "<br>"; ?> <input type="submit" name="search" value="Search"> <?php //$current_function = __FUNCTION__; //echo $current_function; //Do following if "Search" button clicked. if($_SERVER['REQUEST_METHOD'] === 'POST') {echo __LINE__; echo "<br>";//DELETE //Do following if "Search" button clicked. if(isset($_POST['search'])) {echo __LINE__; echo "<br>";//DELETE rows_count(); //This function will forward script flow to fetch_rows() before halting the script. die(); } } echo __LINE__; echo "<br>";//On PAGINATION PAGE 2, THIS FAILS TO ECHO. IT IS LINE: 24. //Do following if "Search" button not clicked but pagination numbered links are clicked. Eg Page 1, 2, 3, etc.. fetch_rows(); //On PAGINATION PAGE 2, THIS FUNCTION IS NOT GETTING TRIGGERED! WHY ? echo __LINE__; echo "<br>";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 205. } ?> What is wrong ? Why is fetch_rows() or $query_2 failing to fetch the matching rows for pages beyond page 1 ? ECHOES Before clicking the SUBMIT button, I get echoed these line numbers as expected: 22 24 32 39 42 50 After clicking the SUBMIT button I get these echoed as expected: 193 71 78 Row Start: 0 Row End: 1 103 114 TOTAL PAGES: 5 123 After clicking the link for 'page 2' on pagination section, I get echoed the same line numbers I get echoed before clicking the SUBMIT button as if everything is starting all over with a new query (when not). That is not supposed to happen. I reckon line 200 is not taking action: fetch_rows(); //On PAGINATION PAGE 2, THIS FUNCTION IS NOT GETTING TRIGGERED! WHY ? IT IS LINE: 200. MAIN ISSUE HERE, I SUSPECT. NOTE: I am a beginner still on OOP and mysqli. Not on pdo either. So, kindly show samples to that level, if you must.