Solved

Php empty file

Posted on 2014-01-05
16
291 Views
Last Modified: 2014-01-10
The attached file is working perfectly well except for one thing:

I need to get the file to ignore the GTE url if the value is empty

The GET url has 5 possible values sent from a form:
xmlfile1
xmlfile2
xmlfile3
xmlfile4
xmlfile5

If, for example, I only send xmlfile1 and xmlfile2 values from the form, I need the attached page to stop processing after the second value

I have tried using "if(!empty($_GET("xmlfile".$i));" but this does not work
xmlimport-form-loop.php
xmlimport-form-loop.php
0
Comment
Question by:doctorbill
  • 7
  • 7
  • 2
16 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 428 total points
ID: 39757639
You've attached the same page twice so I am guessing a bit here.

do {
   echo $i;

if($_GET['xmlfile'.$i]!=""){ // check if the value is empty

$file = $_GET['xmlfile'.$i];
$filepath = "xmlimport/";
...
...

}
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 72 total points
ID: 39757666
See lines 12/13.  Some things to consider...

The GET request is appropriate for information-only queries.  But this script changes the data model.  This script should be changed to use POST.

You might also want to do some error checking and visualize error conditions.  What if the XML did not create an object?  Would you still want to update the data base?  I probably would not want that.

<?php 

$dbh = new PDO("mysql:host=localhost;dbname=1callorders", "XXX", "XXX");
		
$i = 1;

do {
   echo $i;

$file = $_GET['xmlfile'.$i];

// SKIP THE EMPTY FILE POSITIONS
if (empty($file)) continue;

$filepath = "xmlimport/";

$xml = file_get_contents($filepath . $file);
$obj = simplexml_load_string($xml);

//echo out statements from xml
//$name = (string)$obj->header->manifest->document->name;
//$orderdesc = (string)$obj->body->ORDER->ORDERHEAD->ORDERDESC;
//$orderdesc2 = (string)$obj->body->ORDER->ORDERHEAD->FUNCDESC;

$space = "&nbsp;";
//echo "Name: " . $name . $space;
//echo "Desc: " . $orderdesc . $space;
//echo "Desc2: " . $orderdesc2 . $space;
//end statements
		
//prepare the query
$stmt = $dbh->prepare("INSERT INTO ret_orders (ret_id, ord_prodtpe, ord_prodcdedesc, ord_proddesc) VALUES (:ret_id, :name, :orderdesc, :orderdesc2)");

//bind the named parameter to a PHP variable (called $name and $orderdesc etc )
$stmt->bindParam('ret_id', $ret_id);
$stmt->bindParam('name', $name);
$stmt->bindParam('orderdesc', $orderdesc);
$stmt->bindParam('orderdesc2', $orderdesc2);

$ret_id = "DRM";
$name = (string)$obj->header->manifest->document->name;
$orderdesc = (string)$obj->body->ORDER->ORDERHEAD->ORDERDESC;
//$orderdesc2 = (string)$obj->body->ORDER->ORDERHEAD->FUNCDESC;

//loop through the orderline to get each product description
foreach ($obj->body->ORDER->ORDERLINE as $orderline):
             
	$orderdesc2 = $orderline->PRODUCT->DESCRIPTION;	

	//execute the query
	$stmt->execute();	
endforeach;

//run the second query
//$stmt2 = $dbh->prepare("INSERT INTO ret_orders (ord_linetype) VALUES (:name)");
//$stmt2->bindParam('name', $name);
///$stmt2->execute();

$i++;
} while ($i <= 5);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body><br /><br />


<?php
$i = 1;
$break = "<br />";
$space = "&nbsp;";

do {
   //echo $i;

$filenames = $_GET['xmlfile'.$i];


echo "File Imported:".$space.$filenames.$break;

$i++;
} while ($i <= 5);
?>

</body>
</html>

Open in new window

0
 

Author Comment

by:doctorbill
ID: 39758030
Ray,
Your code on 12/13 works in terms of entering the correct data into the database but the page hangs after the url data is parsed to it and the code from lines 78 onwards does not show on the page

where exactly should the second } bracket be in your code - could you edit the page to demonstrate please
0
 
LVL 58

Assisted Solution

by:Gary
Gary earned 428 total points
ID: 39758041
If you are talking to me then it is just before

$i++;
} while ($i <= 5);


But my code is no different than Rays's, just written slightly different.
0
 

Author Comment

by:doctorbill
ID: 39758059
Both your code and Ray's cause the same problem:

Code works in terms of entering the correct data into the database but the page hangs after the url data is parsed to it and the code from lines 78 onwards does not show on the page
0
 
LVL 58

Assisted Solution

by:Gary
Gary earned 428 total points
ID: 39758085
I don't see how unless the prior code is failing - have you got error messages turned on?
0
 

Author Comment

by:doctorbill
ID: 39758100
Sorry - your code did not paste correctly
What haappens now is that the page does not hang and the code from 78 onwards runs correctly BUT Ionly send one GET value I get 5 insertions of the same record into the database
0
 
LVL 58

Assisted Solution

by:Gary
Gary earned 428 total points
ID: 39758107
Do a
var_dump($_GET);

and post the result here.

You will need to amend the last block of code

do {
   //echo $i;
if($_GET['xmlfile'.$i]!=""){ // check if the value is empty
$filenames = $_GET['xmlfile'.$i];


echo "File Imported:".$space.$filenames.$break;
}
$i++;
} while ($i <= 5);

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:doctorbill
ID: 39758118
Sorry - your code did not paste correctly
What haappens now is that the page does not hang and the code from 78 onwards runs correctly BUT Ionly send one GET value I get 5 insertions of the same record into the database

DUMP:
1array(2) { ["xmlfile1"]=> string(30) "Purchase_Order_FI2-4098097.xml" ["xmlsubmit"]=> string(6) "Submit" } 2array(2) { ["xmlfile1"]=> string(30) "Purchase_Order_FI2-4098097.xml" ["xmlsubmit"]=> string(6) "Submit" } 3array(2) { ["xmlfile1"]=> string(30) "Purchase_Order_FI2-4098097.xml" ["xmlsubmit"]=> string(6) "Submit" } 4array(2) { ["xmlfile1"]=> string(30) "Purchase_Order_FI2-4098097.xml" ["xmlsubmit"]=> string(6) "Submit" } 5array(2) { ["xmlfile1"]=> string(30) "Purchase_Order_FI2-4098097.xml" ["xmlsubmit"]=> string(6) "Submit" }
0
 
LVL 58

Assisted Solution

by:Gary
Gary earned 428 total points
ID: 39758134
I think you need to paste your code as you have it, you seem to be looping when you shouldn't be.
0
 

Author Comment

by:doctorbill
ID: 39758146
<?php 

$dbh = new PDO("mysql:host=localhost;dbname=1callorders", "root", "xxxx");
		
$i = 1;

do {
   echo $i;

if($_GET['xmlfile'.$i]!=""){ // check if the value is empty

$file = $_GET['xmlfile'.$i];

// SKIP THE EMPTY FILE POSITIONS
//if (empty($file)) continue;

$filepath = "xmlimport/";
}

$xml = file_get_contents($filepath . $file);
$obj = simplexml_load_string($xml);

//echo out statements from xml
//$name = (string)$obj->header->manifest->document->name;
//$orderdesc = (string)$obj->body->ORDER->ORDERHEAD->ORDERDESC;
//$orderdesc2 = (string)$obj->body->ORDER->ORDERHEAD->FUNCDESC;

$space = "&nbsp;";
//echo "Name: " . $name . $space;
//echo "Desc: " . $orderdesc . $space;
//echo "Desc2: " . $orderdesc2 . $space;
//end statements
		
//prepare the query
$stmt = $dbh->prepare("INSERT INTO ret_orders (ret_id, ord_prodtpe, ord_prodcdedesc, ord_proddesc) VALUES (:ret_id, :name, :orderdesc, :orderdesc2)");

//bind the named parameter to a PHP variable (called $name and $orderdesc etc )
$stmt->bindParam('ret_id', $ret_id);
$stmt->bindParam('name', $name);
$stmt->bindParam('orderdesc', $orderdesc);
$stmt->bindParam('orderdesc2', $orderdesc2);

$ret_id = "DRM";
$name = (string)$obj->header->manifest->document->name;
$orderdesc = (string)$obj->body->ORDER->ORDERHEAD->ORDERDESC;
//$orderdesc2 = (string)$obj->body->ORDER->ORDERHEAD->FUNCDESC;

//loop through the orderline to get each product description
foreach ($obj->body->ORDER->ORDERLINE as $orderline):
             
	$orderdesc2 = $orderline->PRODUCT->DESCRIPTION;	

	//execute the query
	$stmt->execute();	
endforeach;

//run the second query
//$stmt2 = $dbh->prepare("INSERT INTO ret_orders (ord_linetype) VALUES (:name)");
//$stmt2->bindParam('name', $name);
///$stmt2->execute();

$i++;
} while ($i <= 5);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body><br /><br />


<?php
$i = 1;
$break = "<br />";
$space = "&nbsp;";

do {
   //echo $i;
if($_GET['xmlfile'.$i]!=""){ // check if the value is empty
$filenames = $_GET['xmlfile'.$i];
}

echo "File Imported:".$space.$filenames.$break;

$i++;
} while ($i <= 5);

?>

</body>
</html>

Open in new window

0
 
LVL 58

Accepted Solution

by:
Gary earned 428 total points
ID: 39758151
<?php 

$dbh = new PDO("mysql:host=localhost;dbname=1callorders", "root", "xxxx");
            
$i = 1;

do {
   echo $i;

if($_GET['xmlfile'.$i]!=""){ // check if the value is empty

$file = $_GET['xmlfile'.$i];

// SKIP THE EMPTY FILE POSITIONS
//if (empty($file)) continue;

$filepath = "xmlimport/";


$xml = file_get_contents($filepath . $file);
$obj = simplexml_load_string($xml);

//echo out statements from xml
//$name = (string)$obj->header->manifest->document->name;
//$orderdesc = (string)$obj->body->ORDER->ORDERHEAD->ORDERDESC;
//$orderdesc2 = (string)$obj->body->ORDER->ORDERHEAD->FUNCDESC;

$space = "&nbsp;";
//echo "Name: " . $name . $space;
//echo "Desc: " . $orderdesc . $space;
//echo "Desc2: " . $orderdesc2 . $space;
//end statements
            
//prepare the query
$stmt = $dbh->prepare("INSERT INTO ret_orders (ret_id, ord_prodtpe, ord_prodcdedesc, ord_proddesc) VALUES (:ret_id, :name, :orderdesc, :orderdesc2)");

//bind the named parameter to a PHP variable (called $name and $orderdesc etc )
$stmt->bindParam('ret_id', $ret_id);
$stmt->bindParam('name', $name);
$stmt->bindParam('orderdesc', $orderdesc);
$stmt->bindParam('orderdesc2', $orderdesc2);

$ret_id = "DRM";
$name = (string)$obj->header->manifest->document->name;
$orderdesc = (string)$obj->body->ORDER->ORDERHEAD->ORDERDESC;
//$orderdesc2 = (string)$obj->body->ORDER->ORDERHEAD->FUNCDESC;

//loop through the orderline to get each product description
foreach ($obj->body->ORDER->ORDERLINE as $orderline):
             
      $orderdesc2 = $orderline->PRODUCT->DESCRIPTION;      

      //execute the query
      $stmt->execute();      
endforeach;

//run the second query
//$stmt2 = $dbh->prepare("INSERT INTO ret_orders (ord_linetype) VALUES (:name)");
//$stmt2->bindParam('name', $name);
///$stmt2->execute();
}
$i++;
} while ($i <= 5);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body><br /><br />


<?php
$i = 1;
$break = "<br />";
$space = "&nbsp;";

do {
   //echo $i;
if($_GET['xmlfile'.$i]!=""){ // check if the value is empty
$filenames = $_GET['xmlfile'.$i];


echo "File Imported:".$space.$filenames.$break;
}
$i++;
} while ($i <= 5);

?>

</body>
</html> 

Open in new window

0
 

Assisted Solution

by:doctorbill
doctorbill earned 0 total points
ID: 39758175
Perfect - thanks very much
0
 
LVL 58

Expert Comment

by:Gary
ID: 39758200
Accepting one of your own comments delays the closing process, especially when your comment is not part of the solution.
In this case it would be just easier to split the points between Ray's and my first comments (as you see fit) as they were the solution and the other comments were just clarifications. (Rather than 70 points for every comment ;o)
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39758222
@doctorbill: Please, please get some professional programming help, or take some time to step away from this work and learn how to use PHP.  It's not rocket science, but it is extremely detail oriented work, and it takes time and practice to get what you want.  

As an example of what's important but overlooked, you can look no further than the recommendation to change $_GET to $_POST.  I'm concerned that you did not recognize the importance of this recommendation.  We can help with many things, and if you ask more questions along the lines of "what are good learning resources," you will get better results.
0
 

Author Closing Comment

by:doctorbill
ID: 39770581
solution
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now