newphpcoder Posted April 13, 2012 Share Posted April 13, 2012 Hi.. I have code for importing .xml file to database. the problem is I cannot upload my file but no error display. And also I got a problem in importing data from .xml file to database but not all row will be save. here is my code: <?php $data = array(); $con = mysql_connect("localhost", "root",""); if (!$con) { die(mysql_error()); } $db = mysql_select_db("mes", $con); if (!$db) { die(mysql_error()); } $sql = "select * from sales_order"; $result = mysql_query($sql); if (!$result) { die(mysql_error()); } function add_employee($ProductType,$WorkOrder,$POIssueDate,$SalesMonth) { global $data; $con = mysql_connect("localhost", "root",""); if (!$con){ die(mysql_error());} $db = mysql_select_db("mes", $con); if (!$db) { die(mysql_error()); } $ProductType= $ProductType; $WorkOrder = $WorkOrder; $POIssueDate = $POIssueDate; $SalesMonth = $SalesMonth; $sql = "INSERT INTO sales_order (ProductType,WorkOrder,POIssueDate,SalesMonth) VALUES ('$ProductType','$WorkOrder','$POIssueDate','$SalesMonth')" or die(mysql_error()); mysql_query($sql, $con); $data []= array('ProductType'=>$ProductType,'WorkOrder'=>$WorkOrder,'POIssueDate'=>$POIssueDate,'SalesMOnth'=>$SalesMonth); } if ( $_FILES['file']['tmp_name'] ['error']) //f (empty($_FILES['file']['tmp_name']['error'])) { //$dom = DOMDocument::load('SalesOrder.xml'); $dom = DOMDocument::load($_FILES['file']['tmp_name']); $dom = DOMDocument::__construct(); $rows = $dom->getElementsByTagName('Row'); global $last_row; $last_row = false; $first_row = true; foreach ($rows as $row) { if ( !$first_row ) { $ProductType = ""; $WorkOrder = ""; $POIssueDate = ""; $SalesMonth = ""; $index = 1; $cells = $row->getElementsByTagName( 'Cell' ); foreach( $cells as $cell ) { $ind = $cell->getAttribute( 'Index' ); if ( $ind != null ) $index = $ind; if ( $index == 1 ) $ProductType = $cell->nodeValue; if ( $index == 2 ) $WorkOrder = $cell->nodeValue; if ( $index == 3 ) $POIssueDate = $cell->nodeValue; if ( $index == 4 ) $SalesMonth = $cell->nodeValue; $index += 1; } if ($ProductType=='' AND $WorkOrder=='' AND $POIssueDate=='' AND $SalesMonth=='') { $last_row = true; } else { add_employee($ProductType,$WorkOrder,$POIssueDate,$SalesMonth); } } if ($last_row==true) { $first_row = true; } else { $first_row = false; } } } ?> <html> <body> <table> <tr> <th>Sales Order</th> </tr> <?php foreach( $data as $row ) { ?> <tr> <td><?php echo( $row['ProductType'] ); ?></td> <td><?php echo( $row['WorkOrder'] ); ?></td> <td><?php echo( $row['POIssueDate']) ;?> </td> <td><?php echo( $row['SalesMonth'] ); ?></td> </tr> <?php } ?> </table> </body> </html> and I will attach my sample data and the data with color yellow background is only row I want to save to my database. Thank you so much.. 18058_.doc Quote Link to comment https://forums.phpfreaks.com/topic/260839-problem-in-uploading-xml-file-with-not-all-row-will-be-import-to-the-database/ Share on other sites More sharing options...
xyph Posted April 13, 2012 Share Posted April 13, 2012 Did you really insert a screen capture of Excel rendering an XML file into a Word file, and attach it with a 'sample data' label? For one, you're using the global keyword. This makes debugging harder, no matter how simple the script. Second, your condition for running the XML parsing code is that $_FILES['file']['tmp_name'] ['error'] evaluates to TRUE Third, your add_employee() function is only used once. Does it really need to be a function? Can you re-post your code, and comment the lines you're having issues with? I'm not entirely sure if you've copied and pasted this together, or if this is concious, misguided coding. Quote Link to comment https://forums.phpfreaks.com/topic/260839-problem-in-uploading-xml-file-with-not-all-row-will-be-import-to-the-database/#findComment-1336897 Share on other sites More sharing options...
newphpcoder Posted April 13, 2012 Author Share Posted April 13, 2012 <?php $data = array(); $con = mysql_connect("localhost", "root",""); if (!$con) { die(mysql_error()); } $db = mysql_select_db("mes", $con); if (!$db) { die(mysql_error()); } $sql = "select * from sales_order"; $result = mysql_query($sql); if (!$result) { die(mysql_error()); } function add_employee($ProductType,$WorkOrder,$POIssueDate,$SalesMonth) { global $data; $con = mysql_connect("localhost", "root",""); if (!$con){ die(mysql_error());} $db = mysql_select_db("mes", $con); if (!$db) { die(mysql_error()); } $ProductType= $ProductType; $WorkOrder = $WorkOrder; $POIssueDate = $POIssueDate; $SalesMonth = $SalesMonth; $sql = "INSERT INTO sales_order (ProductType,WorkOrder,POIssueDate,SalesMonth) VALUES ('$ProductType','$WorkOrder','$POIssueDate','$SalesMonth')" or die(mysql_error()); mysql_query($sql, $con); $data []= array('ProductType'=>$ProductType,'WorkOrder'=>$WorkOrder,'POIssueDate'=>$POIssueDate,'SalesMOnth'=>$SalesMonth); } //I having issue in this line first it did not import the data [b] if ( $_FILES['file']['tmp_name'] ['error']) //when I tried this commented code it import the data but It needs the .xml file is in the same folder of my php file. //if (empty($_FILES['file']['tmp_name']['error'])) { //$dom = DOMDocument::load('SalesOrder.xml'); $dom = DOMDocument::load($_FILES['file']['tmp_name']); $dom = DOMDocument::__construct(); $rows = $dom->getElementsByTagName('Row'); global $last_row; $last_row = false; $first_row = true; //i think here is the code that an issue with saving not all columns only with yellow background. foreach ($rows as $row) { if ( !$first_row ) { $ProductType = ""; $WorkOrder = ""; $POIssueDate = ""; $SalesMonth = ""; $index = 1; $cells = $row->getElementsByTagName( 'Cell' ); foreach( $cells as $cell ) { $ind = $cell->getAttribute( 'Index' ); if ( $ind != null ) $index = $ind; if ( $index == 1 ) $ProductType = $cell->nodeValue; if ( $index == 2 ) $WorkOrder = $cell->nodeValue; if ( $index == 3 ) $POIssueDate = $cell->nodeValue; if ( $index == 4 ) $SalesMonth = $cell->nodeValue; $index += 1; } if ($ProductType=='' AND $WorkOrder=='' AND $POIssueDate=='' AND $SalesMonth=='') { $last_row = true; } else { add_employee($ProductType,$WorkOrder,$POIssueDate,$SalesMonth); } } if ($last_row==true) { $first_row = true; } else { $first_row = false; } } }[/b] ?> <html> <body> <table> <tr> <th>Sales Order</th> </tr> <?php foreach( $data as $row ) { ?> <tr> <td><?php echo( $row['ProductType'] ); ?></td> <td><?php echo( $row['WorkOrder'] ); ?></td> <td><?php echo( $row['POIssueDate']) ;?> </td> <td><?php echo( $row['SalesMonth'] ); ?></td> </tr> <?php } ?> </table> </body> </html> Actually the .xml file is created in ms excel and I save it as .xml and I printscreen so that I post it Thank you Quote Link to comment https://forums.phpfreaks.com/topic/260839-problem-in-uploading-xml-file-with-not-all-row-will-be-import-to-the-database/#findComment-1336901 Share on other sites More sharing options...
MMDE Posted April 13, 2012 Share Posted April 13, 2012 Basic debugging: turn on all error reporting (to detect and determine syntax errors) print data to make sure it is what you think it is (to detect and determine logical errors) Quote Link to comment https://forums.phpfreaks.com/topic/260839-problem-in-uploading-xml-file-with-not-all-row-will-be-import-to-the-database/#findComment-1336907 Share on other sites More sharing options...
newphpcoder Posted April 13, 2012 Author Share Posted April 13, 2012 I got an error: Notice: Undefined index: file in D:\Program Files\xampp\htdocs\MES PROJECT\import_salesorder.php on line 47 when I used this code: error_reporting(E_ALL); line 47 is : if ( $_FILES['file']['tmp_name'] ['error']){ Thank you Quote Link to comment https://forums.phpfreaks.com/topic/260839-problem-in-uploading-xml-file-with-not-all-row-will-be-import-to-the-database/#findComment-1336923 Share on other sites More sharing options...
MMDE Posted April 13, 2012 Share Posted April 13, 2012 I got an error: Notice: Undefined index: file in D:\Program Files\xampp\htdocs\MES PROJECT\import_salesorder.php on line 47 when I used this code: error_reporting(E_ALL); line 47 is : if ( $_FILES['file']['tmp_name'] ['error']){ Thank you I'm going to go ahead and guess it's because you the form tag in HTML lack a certain parameter... enctype="multipart/form-data" <form action="upload_file.php" method="post" enctype="multipart/form-data"> Quote Link to comment https://forums.phpfreaks.com/topic/260839-problem-in-uploading-xml-file-with-not-all-row-will-be-import-to-the-database/#findComment-1336964 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.