interactive Posted January 19, 2015 Share Posted January 19, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/294056-xml-myqsl-import-externe-xml-in-een-databse/ Share on other sites More sharing options...
interactive Posted January 19, 2015 Author Share Posted January 19, 2015 (edited) -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 January 19, 2015 by interactive Quote Link to comment https://forums.phpfreaks.com/topic/294056-xml-myqsl-import-externe-xml-in-een-databse/#findComment-1503382 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.