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

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.
AdamTrying to learn phpAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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);
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AdamTrying to learn phpAuthor 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.
0
Chris StanyonWebDevCommented:
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
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.