Absolute newbie Q on REQUEST

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
expatriate
Forum Newbie
Posts: 4
Joined: Sat Jul 08, 2017 10:25 am

Absolute newbie Q on REQUEST

Post by expatriate »

Hi Guys,
I never studied or worked much with PHP. VBA/SQL and database design and development is my field. I'm trying to reverse engineer a bit of PHP code for a mobile app my client had commissioned as a front end to a MySQL database.
There's a calling function as follows. I'm trying to figure out how to identify what table or view these fields are coming from. I can post the db_function.php code if necessary. Thanks for your help and patience.
(I hope the php code tags worked ok - I don't see any formatting when previewing

Code: Select all

<?php
include('db_function.php');

$db = new db_function();

$CUST_ID = $_REQUEST['cust_id'];
$PRO_NO = $_REQUEST['pro_no'];

if(!empty($CUST_ID) && !empty($PRO_NO))
{    
	$result = $db->getEquipments($CUST_ID,$PRO_NO);

    if($result != '')
	{
		$response["success"] = 1;
		$response["message"] = "Successfully fetched equipments!";
		$response["equipments"] = $result;
    } 
	else
	{
		$response["success"] = 0;
		$response["message"] = "No Records";
    }
}
else 
{
	$response["success"] = 0;
	$response["message"] = "Required fields are missing!";
}
echo json_encode($response);  
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Absolute newbie Q on REQUEST

Post by requinix »

That code doesn't tell you database stuff. Look in db_function.php for db_function... which is a class :banghead:... and look at its getEquipments() method. Odds are that will have a SQL query in it, or at least specify a table name.


Maybe a stupid question but have you tried just looking for a table or view named "equipment(s)"?
expatriate
Forum Newbie
Posts: 4
Joined: Sat Jul 08, 2017 10:25 am

Re: Absolute newbie Q on REQUEST

Post by expatriate »

Thanks. I wrote the database and also supplied the logic, so I'm familiar with the tables. I know where the data is coming from, but not the parameter values, as they are not entered by the user. However, you got me thinking in the right direction. They must be from the current record on the app, yeah?

However, the query is not returning records when it should with modified parameters. Is there any way to step thru the code, or view return codes, and troubleshoot why not? Note I have modified the parameters from my first post. In my test situation the field FACILITY does exist in both the source for the current record and vwEquipmentonsite and values of it and cust_id do match those of the current record in the app but no records are appearing on the app from the MySQL view. Should it matter, pro_no is a integer, facility a text field.

This is the modified getequipments.php file

Code: Select all

<?php
include('db_function.php');

$db = new db_function();

$CUST_ID = $_REQUEST['cust_id'];
$FACILITY = $_REQUEST['facility'];

if(!empty($CUST_ID) && !empty($FACILITY))
{    
	$result = $db->getEquipments($CUST_ID,$FACILITY);

    if($result != '')
	{
		$response["success"] = 1;
		$response["message"] = "Successfully fetched equipments!";
		$response["equipments"] = $result;
    } 
	else
	{
		$response["success"] = 0;
		$response["message"] = "No Records";
    }
}
else 
{
	$response["success"] = 0;
	$response["message"] = "Required fields are missing!";
}
echo json_encode($response);  
?>

And this the relevant section of the db_function.php file

Code: Select all

	public function getEquipments($CUST_ID,$FACILITY)
	{
		if($CUST_ID!='' && $FACILITY!='') 
		{
			$equipments_count = '';
		//	$query_equipments = "SELECT * from vwequipmentonsite where CUST_ID = '".$CUST_ID."'";
$query_equipments = "SELECT * from vwequipmentonsite where CUST_ID = '".$CUST_ID."' AND FACILITY = '".$FACILITY."'";
			$result_equipments = @mysql_query($query_equipments);
			$equipments_count = mysql_num_rows($result_equipments);
			
			$equipments_data = array();
			
			if($equipments_count)
			{
				for($i=0;$i<$equipments_count;$i++)
				{
					$data_equipments = @mysql_fetch_assoc($result_equipments);
					foreach ($data_equipments as $key => $value) {
						if (is_null($value)) {
							 $data_equipments[$key] = "";
						}
					}
					$equipments_data[] = $data_equipments;
				}				
				return $equipments_data;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Absolute newbie Q on REQUEST

Post by requinix »

Okay, now I'm not clear what you want. Obviously

Code: Select all

$CUST_ID = $_REQUEST['cust_id'];
$FACILITY = $_REQUEST['facility'];
are what's used in the query. Are you asking where those values are coming from?

$_REQUEST is a combination of $_GET and $_POST, being GET parameters (in the URL) and POST parameters (a POSTed form) respectively. The script looks like it's used for AJAX, so you need to look somewhere in your frontend to find out where that is happening. (Search your codebase for "getequipments".)
expatriate wrote:However, the query is not returning records when it should with modified parameters.
"Modified parameters"?
expatriate wrote:Is there any way to step thru the code, or view return codes, and troubleshoot why not?
Generally yes, but it takes some setup to get working. The code is simple enough that you can troubleshoot with two steps:

1. Go to your site, find a place where it's not returning records that it should, and determine the cust_id and facility values it's using. Plug those into the query and try running it manually to make sure it's returning results.
2. The rest of the code looks correct, so if the frontend isn't getting the values then next you troubleshoot the AJAX and Javascript. Use your browser to monitor the AJAX request and you should see the data coming back in the response. Then look at the Javascript to see if you can spot why the data isn't next going... wherever it's supposed to go. The browser's error console could have clues.

I say it's correct but there is one thing that should be adjusted: getequipments.php is returning JSON so it should state that it's returning JSON, because otherwise this will claim to be returning HTML (the default) and that could confuse the AJAX stuff. This could explain the sorts of problems I think you're having. Modify the code to

Code: Select all

header("Content-Type: application/json");
echo json_encode($response);
and give this a shot first.
expatriate
Forum Newbie
Posts: 4
Joined: Sat Jul 08, 2017 10:25 am

Re: Absolute newbie Q on REQUEST

Post by expatriate »

requinix, Thanks. "modified parameters" refers to the difference in the php between my first 1st and 2nd posts. I added one parameter. At first I was unsure of where the parameters are coming from - but that must be the current record in the app.

The front end is an IOS app. I'm not an IOS developer so can't check anything there. I was hoping that just adding the parameter in the php file would be adequate but it looks like that is not going to happen. What do you think? From what you can see, with the 'facility' field existing in both the source of the query and the current record - should simply modifying parameter fields in php files result in records being returned?

I knew that being able to monitor the calls to the php code without access to the front end was a long shot, but had to ask.

Added the line:

Code: Select all

header("Content-Type: application/json");header("Content-Type: application/json");
the 2nd line was already there at the end
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Absolute newbie Q on REQUEST

Post by requinix »

If you're not debugging on production then you can "monitor" the request easily:

Code: Select all

file_put_contents("/tmp/foo", json_encode($_REQUEST));
Use the app and check /tmp/foo to see what the app is sending.
expatriate
Forum Newbie
Posts: 4
Joined: Sat Jul 08, 2017 10:25 am

Re: Absolute newbie Q on REQUEST

Post by expatriate »

Great. I will try that. Thanks.
Post Reply