XML to DB with PHP and MYSQL

Asked

Viewed 26 times

0

I created a code that reads the XML and then inserts it into the MYSQL database, but only tries because an error is returned. It’s simple because I’m doing it for blocks.

Filing cabinet conn.php

<?php
if(!session_id())
{
    session_start();
}
header("Content-type: text/html; charset=utf-8");
$mysqli = mysqli_connect('localhost', 'eliseu', '123mudar', 'xml');
mysqli_query($mysqli, "SET NAMES 'utf8'");
mysqli_query($mysqli, 'SET character_set_connection=utf8');
mysqli_query($mysqli, 'SET character_set_client=utf8');
mysqli_query($mysqli, 'SET character_set_results=utf8');

Filing cabinet index.php [Reads XML and writes to DB Mysql]

<?php
include_once("conn.php");
$arquivo_xml = simplexml_load_file('options.xml');
$cores = $arquivo_xml->cor[0]->value;
$count_cor = count($arquivo_xml->cor[0]->value);
####### FILTRA VAR
if( !function_exists('filtra_var') )
{
    function filtra_var($var) {
        $var = trim($var);
        $var = strip_tags($var);
        $var = htmlspecialchars($var, ENT_QUOTES, 'UTF-8');
        //$var = fgetss($var);
        $var = addslashes($var);
        $var = filter_var($var, FILTER_SANITIZE_STRING);
        return $var;
    }
}
####### PREPARAR MYSQL INSERT
if( !function_exists('insert_data') ){
    function insert_data($valor,$cat,$desc=NULL)
    {
        $valor = filtra_var($valor);
        ##
        $cat = filtra_var($valor);
        ##
        $desc = filtra_var($desc);
        ##
        $sql = mysqli_query("INSERT INTO `site_options` (`opt_value`,`opt_option`.`opt_desc`) VALUES ('" . mysqli_real_escape_string($mysqli, $valor) . "','" . mysqli_real_escape_string($mysqli, $cat) . "','" . mysqli_real_escape_string($mysqli, $desc) . "') LIMIT 1;");
        $exec_sql = mysqli_query($mysqli, $sql);
        unset($exec_sql);
    }
}
#######
for($x=0;$x<$count_cor;$x++)
{
    if(insert_data($cores[$x],'Cor',''))
    {
        echo "<strong>Inserido:<strong> " . $cores[$x] . "<br>";
    }
}
echo "<hr>";
$combustivel = $arquivo_xml->combustivel[0]->value;
$count_combustivel = count($arquivo_xml->combustivel[0]->value);
for($x=0;$x<$count_combustivel;$x++)
{
    if(insert_data($cores[$x],'Combustível',''))
    {
        echo "<strong>Inserido:<strong> " . $combustivel[$x] . "<br>";
    }
}

The structure of the file options.xml

<?xml version="1.0" encoding="UTF-8"?>
<options>
    <cor>
        <value>Amarelo</value>
        <value>Azul</value>
        <value>Verde</value>
        <value>Branco</value>
        <value>Prata</value>
        <value>Outros</value>
    </cor>
    <combustivel>
        <value>Gasolina</value>
        <value>Álcool</value>
        <value>Flex</value>
    </combustivel>
</options>

Returns the error on the screen:

Warning: mysqli_real_escape_string() expects Parameter 1 to be mysqli, null Given in X: Apache2 www xml index.php on line 29

1 answer

0


corrected by setting global variable for connection link $mysqli:

####### PREPARAR MYSQL INSERT
if( !function_exists('insert_data') ){
    function insert_data($valor,$cat,$desc=NULL)
    {
        global $mysqli // Inseri o link de conexão como global
        ## 
        $valor = filtra_var($valor);
        ##
        $cat = filtra_var($valor);
        ##
        $desc = filtra_var($desc);
        ##
        $sql = mysqli_query("INSERT INTO `site_options` (`opt_value`,`opt_option`,`opt_desc`) VALUES ('" . mysqli_real_escape_string($mysqli, $valor) . "','" . mysqli_real_escape_string($mysqli, $cat) . "','" . mysqli_real_escape_string($mysqli, $desc) . "') LIMIT 1;");
        mysqli_query($mysqli, $sql);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.