IRHM
IRHM

Reputation: 1326

INSERT IGNORE In mySQL

I wonder whether someone can help me please.

I'm trying to put together a PHP script that takes data from an xml file and places the data in a mySQL data. I've been working on this for a few days and I'm still can't seem to get this right.

This is the code that I've managed to put together:

    <? 

      $objDOM = new DOMDocument(); 
      $objDOM->load("xmlfile.xml"); 

      $Details = $objDOM->getElementsByTagName("Details"); 

      foreach( $Details as $value ) 
      { 

        $listentry = $value->getElementsByTagName("listentry"); 
        $listentrys  = $listentry->item(0)->nodeValue;

        $sitetype = $value->getElementsByTagName("sitetype"); 
        $sitetypes  = $sitetype->item(0)->nodeValue;

        $sitedescription = $value->getElementsByTagName("sitedescription"); 
        $sitedescriptions  = $sitedescription->item(0)->nodeValue;

        $siteosgb36lat = $value->getElementsByTagName("siteosgb36lat"); 
        $siteosgb36lats  = $siteosgb36lat->item(0)->nodeValue;

        $siteosgb36lon = $value->getElementsByTagName("siteosgb36lon"); 
        $siteosgb36lons  = $siteosgb36lon->item(0)->nodeValue;

        //echo "$listentrys :: $sitetypes :: $sitedescriptions :: $siteosgb36lats ::         $siteosgb36lons <br>"; 


      } 

    require("phpfile.php");

    //Opens a connection to a MySQL server
    $connection = mysql_connect ("hostname", $username, $password);
    if (!$connection) {
     die('Not connected : ' . mysql_error());
    }

    // Set the active MySQL database
    $db_selected = mysql_select_db($database, $connection);
    if (!$db_selected) {
    die ('Can\'t use db : ' . mysql_error());
    }

   mysql_query("INSERT IGNORE INTO scheduledsites (listentry, sitetype, sitedescription, siteosgb36lat, siteosgb36lon) VALUES('$listentrys','$sitetypes','$sitedescriptions','$siteosgb36lats','$siteosgb36lons') ")  
or die(mysql_error());  


    echo "Data Inserted!"; 


?>

I can pull the data from the xml file, but it's the part of the script that sends the data to my database table that I'm having trouble with.

The script runs but only the last record is saved to the database.

I can parse the fields from the xml file without any problems and the check I'm trying to put in place is, if there is a 'listentry' number in the new data that is matched to one already in the table then I don't want that record to be added to the table, i.e. ignore it.

I just wondered whether someone could perhaps take a look at this please and let me know where I'm going wrong.

Many thanks

Upvotes: 0

Views: 265

Answers (1)

evan
evan

Reputation: 12553

You are only calling mysql_query once. So it will only insert one row.

The sql needs to be inside the loop.

Upvotes: 2

Related Questions