Register data received from api with php

0

I'm having trouble saving data received by an api in the database with php, it always returns me the error: Error trying to register record!

I have already verified the data that is being inserted with print_r ($ sql) and everything is ok. I do not know what else to do.

Javascript no index:

$(function() {
            $.ajax({
                headers: {
                    'user_token': 'aqui_vai_o_meu_token',
                },
                type: 'GET',
                dataType: "json",
                url: 'http://moviecom.com.br/MoviecomAPI/',
                data: {
                    'praca': 'JAU',
                    'data_ini': '2018-05-10',
                    'data_fim': '2018-05-20'
                },
                success: function(response) {
                    const filmes = response.data[0].filmes;

                    if(response.status == "Success") {
                        $(filmes).each( function (i, el){

                            const filme = el.filme;

                            $.post("salvar.php", {

                                titulo: filme.titulo,
                                cartaz: filme.cartaz,
                                sinopse: filme.sinopse,
                                genero: filme.genero

                            }, function(titulo){
                                $(".titulo").html(titulo);
                            })

                        })
                    }
                    else {
                        console.log(response.data[0]);
                    }   
                }
            }).fail(error => {
                console.log(error);
            });
        })

save.php:

<?php

include("conexao.php");

$titulo = $_POST['titulo'];
$cartaz = $_POST['cartaz'];
$sinopse = $_POST['sinopse'];
$genero = $_POST['genero'];

$sql = "INSERT INTO filme (TITULO', 'CARTAZ', 'SINOPSE', 'GENERO') values ($titulo, $cartaz, $sinopse, $genero)";
print_r($titulo);

mysqli_query($conect, $sql) or die("Erro ao tentar cadastrar registro");

$response = array("success" => true);

echo json_encode($response);
?>

connection.php:

<?php

    //conexão com o servidor
    $conect = mysqli_connect("localhost", "root", "", "teste");

    // Caso a conexão seja reprovada, exibe na tela uma mensagem de erro
    if (!$conect) die (
        "<h1>Falha na conexão com o Banco de Dados!</h1>"
    );
?>
    
asked by anonymous 12.05.2018 / 23:50

1 answer

0

Your problem is here:

$sql = "INSERT INTO filme (TITULO', 'CARTAZ', 'SINOPSE', 'GENERO') values ($titulo, $cartaz, $sinopse, $genero)";

1 - The field TITULO is written TITLE 'with only one quotation mark. This causes a syntax error.

2- The values that are likely to be in CHAR or VARCHAR are NOT single quotation marks.

The right thing would be something like this:

$sql = "INSERT INTO filme ('TITULO', 'CARTAZ', 'SINOPSE', 'GENERO') values ('$titulo', '$cartaz', '$sinopse', '$genero')";

The print_r($sql) will not show the error.

    
13.05.2018 / 07:31