#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Exclamation Importing XML File into MYSQL


    Hello Guys.

    Thank you for the opportunity to post my question here. I am trying to import an xml file into mysql and I am able to connect to the DB but then it always add an empty row in my DB without the data in it.

    PHP CODE

    PHP Code:
    <?php

    ini_set
    ('display_errors','On');

    $contodb mysql_connect("dbserver","user","pass");
    if (!
    $contodb)  {  
        die(
    'I could not connect to: ' mysql_error());  
    }

    $selectdb mysql_select_db("online_order"$contodb);
    if (!
    $selectdb)  { 
        die(
    'The database could not be selected: ; ' mysql_error());  
    }

    $library =  simplexml_load_file('address to the file here');

        
    $conv simplexml_load_file;

        
    //Start Parsing XML

        
    $ordernum mysql_real_escape_string($conv->ordernum);
        
    $location mysql_real_escape_string($conv->location);
        
    $orcost mysql_real_escape_string($conv->orcost);
        
    $itmno mysql_real_escape_string($conv->itmno);
        
    $ordesc mysql_real_escape_string($conv->ordesc);
        
    $uom mysql_real_escape_string($conv->uom);
        
    $quan mysql_real_escape_string($conv->quan);
        
    $status mysql_real_escape_string($conv->status);
        
    $user mysql_real_escape_string($conv->user);
        
    $orddate mysql_real_escape_string($conv->orddate);

        
    //insert statement                     
        
    mysql_query("INSERT INTO OrderDet (ordernum, location, orcost, itmno, ordesc, uom, quan, status, user, orddate)
        VALUES ('
    $ordernum', '$location', '$orcost', '$itmno', '$ordesc', '$uom', '$quan', '$status', '$user', '$orddate')")
        or die(
    mysql_error());
        
    //closing connection here
    mysql_close($contodb);
    XML FILE:

    PHP Code:
    <xml>
     <
    order>
      <
    ordernum>172</ordernum><user>USER   </user><status>100</status><orddate>2013-08-18</orddate><location>MI </location><items>
      <
    item><itmno>260.0059                 </itmno><quan>1</quan><uom>CA</uom></item>
      <
    item><itmno>260.0049                 </itmno><quan>6</quan><uom>BX</uom></item>
      <
    item><itmno>540.0075                 </itmno><quan>2</quan><uom>EA</uom></item>
      <
    item><itmno>540.0059                 </itmno><quan>1</quan><uom>EA</uom></item>
      <
    item><itmno>540.0069                 </itmno><quan>1</quan><uom>EA</uom></item>
      <
    item><itmno>265.0279                 </itmno><quan>4</quan><uom>EA</uom></item>
      <
    item><itmno>540.0087                 </itmno><quan>1</quan><uom>CA</uom></item>
      <
    item><itmno>540.0088                 </itmno><quan>1</quan><uom>CA</uom></item>
      <
    item><itmno>540.0109                 </itmno><quan>3</quan><uom>EA</uom></item>
      <
    item><itmno>260.0310                 </itmno><quan>2</quan><uom>CA</uom></item>
      <
    item><itmno>260.0359                 </itmno><quan>1</quan><uom>CA</uom></item>
      <
    item><itmno>260.0479                 </itmno><quan>2</quan><uom>CA</uom></item>
      <
    item><itmno>265.0369                 </itmno><quan>4</quan><uom>BX</uom></item>
      <
    item><itmno>540.0060                 </itmno><quan>1</quan><uom>EA</uom></item>
      <
    item><itmno>540.0038                 </itmno><quan>1</quan><uom>CA</uom></item>
      <
    item><itmno>540.0048                 </itmno><quan>1</quan><uom>CA</uom></item>
      <
    item><itmno>206.0391                 </itmno><quan>1</quan><uom>RL</uom></item>
      <
    item><itmno>206.0221                 </itmno><quan>2</quan><uom>RL</uom></item>
      <
    item><itmno>540.0065                 </itmno><quan>1</quan><uom>EA</uom></item>
      <
    item><itmno>260.0389                 </itmno><quan>6</quan><uom>BX</uom></item>
      <
    item><itmno>201.1159                 </itmno><quan>5</quan><uom>BX</uom></item>
      <
    item><itmno>520.0209                 </itmno><quan>2</quan><uom>EA</uom></item>
      <
    item><itmno>210.0319                 </itmno><quan>11</quan><uom>EA</uom></item>
      <
    item><itmno>230.0501                 </itmno><quan>3</quan><uom>CA</uom></item>
      <
    item><itmno>540.0055                 </itmno><quan>1</quan><uom>EA</uom></item>
      <
    item><itmno>240.0043                 </itmno><quan>9</quan><uom>BX</uom></item>
      <
    item><itmno>240.0040                 </itmno><quan>2</quan><uom>BX</uom></item>
      <
    item><itmno>240.0041                 </itmno><quan>4</quan><uom>BX</uom></item>
      <
    item><itmno>240.0039                 </itmno><quan>4</quan><uom>BX</uom></item>
      <
    item><itmno>240.0042                 </itmno><quan>6</quan><uom>BX</uom></item>
      <
    item><itmno>260.0490                 </itmno><quan>5</quan><uom>BX</uom></item>
      <
    item><itmno>207.0159                 </itmno><quan>3</quan><uom>PK</uom></item>
      <
    item><itmno>540.0063                 </itmno><quan>2</quan><uom>CA</uom></item>
      <
    item><itmno>265.0629                 </itmno><quan>6</quan><uom>CN</uom></item>
      <
    item><itmno>520.0119                 </itmno><quan>2</quan><uom>EA</uom></item>
      </
    items>
     </
    order>
     </
    xml
    Again, once the code is executed it creates a blank entry in the DB. Any help will be greatly, greatly appreciated it.

    Thanks in advance!!!!!
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    This code is incorrect since it doesn't take the structure of the XML document into account.

    Also, this:
    $conv = simplexml_load_file;

    Throws a warning. You're developing without errors turned on (and have no error handling of your own). Turn on error_reporting and add error-checking into this file, then read up on the proper use of simpleXML objects.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo