Trying to display recently submitted values from a database on my page. Not working.

Adam
Adam used Ask the Experts™
on
Hello.
I have an input form where the user enters details about them-self, which would then be stored in a database. After the form has been submitted, the user would be redirected to a page which confirms the page has been sent and also uses / echoes some of the information the user has just entered in the previous page.  

The set up is only partially working. The form works and the information is stored in the database and redirects. The redirected page is not calling the the information (just submitted into the database) even though the id of the entry entered in the previous form has been passed through.

So I have a signup form and a welcome form and functions on a functions page.

On the Welcome page I have the following function.

function find_student_by_id($id) { //so this function should return an associated array called teacher which has all teacher propertuies associated to that ID
      global $db_connection;
  
      $sql = "SELECT * FROM students_table";
      //$sql .= "WHERE id='" . db_escape($db_connection, $id) . "' ";
      $sql .= "WHERE id='" . $id . "' ";
     
      $result = mysqli_query($db_connection, $sql);
  	//echo $result;
     confirm_result_set($result);
     
 	$student = mysqli_fetch_assoc($result); // find first
     mysqli_free_result($result);
     return $student; // returns an assoc. array called student
   }

Open in new window


In this function, the following function is being called -

function confirm_result_set($result_set) {
     if (!$result_set) {
     	exit("Database query failed fool!.");
     }
   }

Open in new window


The code is failing at this function as I am receiving the error - "Database query failed fool!."

I'm assuming therefore that there is no $result_set value. Looking at this, I thought this was because the string value '$result' was being passed, not '$result_set' so I changed '$result_set' to '$result' in function 'confirm_result_set'. But no, this didn't work (which confuses me more...sigh).

I took these functions from a tutorial I was following and just tried to adapt them to my needs - but clearly I'm messing up somewhere. Any idea what is causing the issue? Many thanks for reading.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
It could be something as simple as your SQL query. You're concatenating 2 strings together to get your query but there is no space between the 2 strings, so what you end up trying to execute is:

SELECT * FROM students_tableWHERE id='xx'

which is invalid SQL (no space between table name and WHERE)

Either add a space, or just build the query differently:

$sql = sprintf("SELECT * FROM students_table WHERE id='%s'", $id);
AdamTrying to learn php

Author

Commented:
Wow - that was the issue. The space. Several hours spent staring at the code this morning. I just added the space and it worked as  intended. Many thanks for your help Chris.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
No worries Adam,

Glad it's working. We've all spent hours staring blankly at code only to miss something really simple.

As a tip for moving forward, you might want to think about adding in some error checking to your DB calls. Not only is it it good practice, but it will make your code more robust and easier to debug.

Good luck with it

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial