Insert XML into Mysql table

Asked

Viewed 294 times

0

I have an xml with the name of some movies, I want when I press a button in html the names are inserted in a table in the database. I made the connection to php using ajax. The problem is that when I do the insertion, the first title is skipped and is not inserted.

<script>
    $(document).ready(function() {

        $("#bInserir").click(function(){
            fInserir();
        });

        function fInserir()
        {
                $.ajax({
                type:"POST",
                dataType: "html",
                url: "revisao.php",
                success:function(ret){
                    alert(ret);
                    }
            });
        }
    });
</script>

<xml>
    <filme>
        <titulo>
            Assassinato no Expresso Oriente
        </titulo>
        <ano>
            2017
        </ano>
    </filme>
    <filme>
        <titulo>
            A Cabana
        </titulo>
        <ano>
            2016
        </ano>
    </filme>
</xml>

$xml_string = file_get_contents("revisao.xml");
$xml_object = simplexml_load_string($xml_string);

$conexao = mysqli_connect("localhost", "root", "root", "estudo");

foreach ($xml_object as $key) {


        mysqli_query($conexao, "INSERT INTO nota (titulo) VALUES ('$key->titulo')");
}

mysqli_close($conexao);
echo json_encode("Sucesso");

create table nota(
idnota int not null auto_increment,
titulo varchar(25) not_null,
primary key(idnota)
);

As an example, when this code I placed is executed it is only inserting the title "The Hut" in the table and skipping the first.

1 answer

1


It is because the name of the film exceeds the limit set in the creation of the column (25)

titulo varchar(25) not_null,

"Murder on the Orient Express" has more than 25 characters

  • Damn my lack of attention, I always put 25. Thank you very much!

Browser other questions tagged

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