Solved

php function

Posted on 2014-12-12
17
140 Views
Last Modified: 2014-12-12
I have the attached php function. It is included in several programs using
include "parts_dols.php";

In one specific case, I put the include in at the top of the file, like this:

<?php
include "labor_dols.php";
include "parts_dols.php";

When I do this, the program fails, even when I do not execute the function in the program.

I use the function successfully in 4 - 5 other programs.

What can cause this?

Thanks
parts-dols.php
0
Comment
Question by:Richard Korts
  • 7
  • 6
  • 3
  • +1
17 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40497160
The clue would be in the error message
Add to the top of the page

    error_reporting(E_ALL);
0
 

Author Comment

by:Richard Korts
ID: 40497164
Gary,

To the top of the function or to the top of the page in which the function is being included?
0
 
LVL 58

Expert Comment

by:Gary
ID: 40497172
The top of the page right after your very first opening
<?php
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:Richard Korts
ID: 40497175
Gary,

I put it in the referencing program (NOT the function).

I get a LOT of these:

Notice: Undefined index: addr in /home/fwsprinkler/public_html/fwsprinklercdb.info/wo_updt.php on line 67

Notice: Undefined index: invp in /home/fwsprinkler/public_html/fwsprinklercdb.info/wo_updt.php on line 76

Notice: Undefined index: wop in /home/fwsprinkler/public_html/fwsprinklercdb.info/wo_updt.php on line 81

Notice: Undefined index: warr in /home/fwsprinkler/public_html/fwsprinklercdb.info/wo_updt.php on line 86

Notice: Undefined index: lg1 in /home/fwsprinkler/public_html/fwsprinklercdb.info/wo_updt.php on line 92

Notice: Undefined index: lg2 in /home/fwsprinkler/public_html/fwsprinklercdb.info/wo_updt.php on line 95

Notice: Undefined index: fhour in /home/fwsprinkler/public_html/fwsprinklercdb.info/wo_updt.php on line 99

Notice: Undefined index: tax in /home/fwsprinkler/public_html/fwsprinklercdb.info/wo_updt.php on line 104

Notice: Undefined index: emfee in /home/fwsprinkler/public_html/fwsprinklercdb.info/wo_updt.php on line 106

Notice: Undefined index: locf in /home/fwsprinkler/public_html/fwsprinklercdb.info/wo_updt.php on line 110

Notice: Undefined index: ckd2014-12-040rkorts1 in /home/fwsprinkler/public_html/fwsprinklercdb.info/wo_updt.php on line 159
0
 

Author Comment

by:Richard Korts
ID: 40497186
The top looks like this now:

<?php
session_start();
error_reporting(E_ALL);
include "parts_dols.php";
include "labor_dols.php";      

I had session_start() farther down, it complained about "headers already sent" so I moved it to the top.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40497189
Are you using register_globals?
Where are all these variables coming from addr,fhour etc?
0
 

Author Comment

by:Richard Korts
ID: 40497195
Statements like these:

$addr = conv_quotes($_REQUEST['addr']);
$wonotes = conv_quotes($_REQUEST['notes']);

addr, etc. are on the HTML form being submitted with POST method.
0
 

Author Comment

by:Richard Korts
ID: 40497196
I am not using register globals.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40497221
You are referencing the variables in wo_updt.php before they are set then.
0
 

Author Comment

by:Richard Korts
ID: 40497262
Gary,

The program wo_updt.php works PERFECTLY if I don't put that include in.

The include causes it to fail without the included function being referenced.

The SAME include is used in 4 - 5 other programs & works flawlessly.

It makes NO sense.

So I guess I gust have to copy the code in the included function & use it directly in the program; don't like to do that because now it's in two places.
0
 
LVL 58

Accepted Solution

by:
Gary earned 350 total points
ID: 40497266
Just to be clear - it's when you include parts_dols.php that the page fails?

Are you sure that function
function parts_dols

is not already being included somewhere else?

I don't see how that page can cause all the other errors.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40497271
In order to avoid errors like that, I use a little more complicated method of getting my POST variables.  If the variable does not exist, then I create a blank variable.  This also avoids the problem of "Undefined index" errors.  If it does exist, I assign the POST value to the regular variable after limiting it's length.  That limits the amount of garbage people can post in the forms.

if (!isset($_POST["Name"]))  $Name = ''; else $Name = substr($_POST["Name"],0,64);

Open in new window

With all the typing that is required, programming seems to be specialized secretarial work at times.
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 150 total points
ID: 40497284
I just looked at 'parts_dols.php'.  It can NOT cause the errors you listed above.  It is just a database function.  About the only line that could cause a problem is the 'include'.  Putting that code in the 'main' program will not help any.

And frankly, you have not yet given us the error message that you get when you include 'parts_dols.php'.
0
 

Author Comment

by:Richard Korts
ID: 40497315
I put that code in the main program, it works perfectly.

FYI, the error became apparent because the program (as the name suggests) is supposed to update a Work Order with any changes specified on the referencing page, then redirect back to that page.

I noticed the failure because it never redirected back, I put in a bunch of echos, it goes ALL the way to the end.

It's nuts.

But you are both good guys & eager to help & it's Christmas, so I'll award you the points.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40497435
if (!isset($_POST["Name"]))  $Name = ''; else $Name = substr($_POST["Name"],0,64);

Open in new window

is sometimes written with a ternary operator like this:
$Name 
= !empty($_POST["Name"])
? substr($_POST["Name"],0,64)
: NULL
;

Open in new window

Other filtering functions are sometimes used.  Explore PHP filter_var() for more.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40497443
so I'll award you the points.
I'd rather have beer!
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40497471
I've seen that format before.  I've been using my version for years now.  I create so many forms that I have a PHP program that generates that for all the POST values from a form so I can just copy and paste it into the actual PHP page that will receive the form data.  It saves a Lot of typing (and error correcting!).  Here it is, I call it 'postgen.php'.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>POST Gen</title>
</head>
<body>
<pre>
<?php 
//var_dump($_POST);
foreach($_POST as $key => $value) {
    echo 'if (!isset($_POST["'.$key.'"]))  $'.$key.' = \'\'; else $'.$key.' = substr($_POST["'.$key.'"],0,64);'."<br>";
}

?>
</pre>
</body>
</html>

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

774 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question