auto_importate table

I’m sorry for my english.
I’m trying to add some functionalities at my WordPress website and to do this i have added a function in my function.php theme. The idea is to save in a custom table some datas and create a wp_post for any element added in the custom table.

I hooked the function in this way:

add_action ( 'init', 'inserimentoAutoDatabase' );

The problem is that when the function is executed the query that save the datas in the custom table and create a new post is executed twice. I report my code:

function inserimentoAutoDatabase(){
//get datas from an xml file
$result = fetchData("");
//save the xml  
$fp = fopen('autodealerk-1.xml', 'w+');
fwrite($fp, $result);
//load xml file
//start parsing xml content
foreach($xml->car as $auto)
    //this echo is executed only once
    echo ("test");
    //not important for the question | checks some content
    if($auto->km == null || $auto->km  == ''){
        $kilometri = "nuova";
        $anno_registrazione = "-";
        $kilometri = $auto->km;
        $anno_registrazione = $auto->registrationDate;
    $inevidenza = 0;
        $inevidenza = 1;
    // create an array for the query
    $insData = array(
        'id' => $auto['id'],
        'make' => (string) $auto->make,
        'model' => (string) $auto->model,
        'version' => (string) $auto->version,
        'bodyType' => $auto->bodyType,
        'fuelType' => $auto->fuelType,
        'type' => $auto->type,
        'dealer_name' => (string) $auto->dealer->name,
        'gear_gearType' => (string) $auto->gear->gearType,
        'tractionType' => $auto->tractionType,
        'kw' => $auto->kw,
        'doors' => $auto->doors,
        'seats' => $auto->seats,
        'emissionClass' => $auto->emissionClass,
        'prices_listPrices' => $auto->prices->listPrice,
        'exterior_color_paint' => $auto->exterior->color . " " . $auto->exterior->paint,
        'km' => $kilometri,
        'typewarrantyMonths' =>  $auto->warranty->type . " " . $auto->warranty->warrantyMonths,
        'equipments' => "equipaggiamenti",
        'media' => $auto->image,
        'description' => $auto->description,
        'registrationDate' => $anno_registrazione
    $columns = implode("`, `",array_keys($insData));
    $escaped_values = array_map('mysql_real_escape_string', array_values($insData));
    $values  = implode("', '", $escaped_values);
    //execute the query ******* the query is executed twice
    mysql_query('INSERT INTO  `auto_importate` (`'.$columns.'`) VALUES (''.$values.'')');
    // define the post
    $my_post = array(
      'post_title'    => (string) $auto->make . (string) $auto->model . (string) $auto->version,
      'post_content'  => $auto->description,
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_type'     => "vehicles"
    // ***** post are created twice
    $post_id = wp_insert_post( $my_post );
    //i break the cycle, i expect to find in the table only one elements but there is twice
    if( $i == 1 ) break;

As you can see in the images below at the end of the cicle (breaked after the first execution) i expect only one element but there are twice.

auto_importate table content

Anyone can help me??? Have you any idea about this issue?

Read more here: mySQL queries are executed twice on wordpress website


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , ,

Wordpress related questions and answers: