• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 74
  • Last Modified:

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.
0
Adam
Asked:
Adam
  • 2
1 Solution
 
Chris StanyonCommented:
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
 
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 StanyonCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now