Jump to content

XML->MyQSL import externe xml in een databse


interactive

Recommended Posts

Ik probeer een online gehost .xml bestand in een database te plaatsen.

Ik heb inmiddels alle foutmeldingen weg weten te werken (door ze op te lossen :-)) maar heb nu een blanco pagina zonder errors en geen data in mijn database

 

Hier is wat ik tot nu toe heb gedaan.

<?php
// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

// specify url of xml file   
$xmlData = file_get_contents(get_bloginfo('template_directory').'/import/external_db.xml');         
// read XML data string
$xml = simplexml_load_string($xmlData) or die("ERROR: Cannot create SimpleXML object");
// create and execute INSERT queries
foreach ($xml->item as $item) {
$id = $item->id;
$name = mysqli_real_escape_string($item->name);
$code = mysqli_real_escape_string($item->code);
$email = mysqli_real_escape_string($item->email);
$jobdescription = mysqli_real_escape_string($item->jobdescription);
$salary = mysqli_real_escape_string($item->salary);
$sql = "INSERT INTO wajv_employees (id, name, code, email, jobdescription, salary) VALUES ('$id', '$name', '$code', '$email', '$jobdescription', '$salary')";
mysqli_query($sql) or die ("ERROR:");
}
 //mysql_close($connection);

?>

Dit is de XML:

<?xml version="1.0" encoding="utf-8"?>
<data>
<Item>
	<field name="name">xxx</field>
	<field name="code">101</field>
	<field name="email">xxx@xxx.nl</field>
	<field name="jobdescription">Job Description</field>
	<field name="salary">1000</field>
</Item>
<Item>
	<field name="name">xxx</field>
	<field name="code">102</field>
	<field name="email">xxx@xxx.nl</field>
	<field name="jobdescription">Job Description</field>
	<field name="salary">1000</field>
</Item>
<Item>
	<field name="name">xxx</field>
	<field name="code">103</field>
	<field name="email">xxx@xxx.nl</field>
	<field name="jobdescription">Job Description</field>
	<field name="salary">1000</field>
</Item>
<Item>
	<field name="name">xxx</field>
	<field name="code">104</field>
	<field name="email">xxx@xxx.nl</field>
	<field name="jobdescription">Job Description</field>
	<field name="salary">1000</field>
</Item>
</data>

Hoop dat iemand mijn fout over het hoofd ziet

Link to comment
Share on other sites

-EDIT-EDIT-EDIT-EDIT-EDIT-EDIT-EDIT-EDIT-EDIT-EDIT-EDIT-EDIT-

 

Oke probleem 1 opgelost.

Fout zat hem in een hoofdletter

In het XML bestand zeg ik <Item> en in mijn PHP zeg ik foreach ($xml->item as $item) {

In de XML aangepast van Item naar item.

 

Tevens wordt er binnen WordPress geen gebruik gemaakt van mysqli_real_escape_string.

In plaats hiervan wordt $wpdb->prepare() gebruikt.

De code wordt dan:

foreach ($xml->Item as $item) {
$id = $item->id;
$name = esc_sql($item->name);
$code = esc_sql($item->code);
$email = esc_sql($item->email);
$jobdescription = esc_sql($item->jobdescription);
$salary = esc_sql($item->salary);
$myTitle = 'I have no idea';
$sql = $wpdb->prepare("INSERT INTO wajv_employees (id, name, code, email, jobdescription, salary) VALUES ('$id', '$name', '$code', '$email', '$jobdescription', '$salary')", "%" . $myTitle . "%");
mysql_query($sql) or die ("ERROR:");
echo 'SUCCESS<br>';
}

Nu worden de velden wel in de database aangemaakt maar zonder content. Tevens ook geen foutmelding

Edited by interactive
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.