Register incoming api data with php

Asked

Viewed 167 times

0

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

I already checked the data being entered with print_r($sql) and this is all ok. I don’t 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);
?>

php connection.:

<?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>"
    );
?>

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 spelled TITLE' with only one quote. This generates a syntax error.

2- The values that are probably in CHAR or VARCHAR are WITHOUT single quotes.

Right 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 error.

  • Can solve just by putting the call like this: "INSERT INTO film (TITLE, POSTER, SYNOPSIS, GENERO) values ('". $title." ', "'. $poster'". '". $synopsis." ', "'. $genero."')";

  • @So does Igorfaria. But my answer is correct?

  • If she’s solved your problem, please approve. This helps the community and gives a gas to other people to be interested in answering their next questions, if any. Hug!

Browser other questions tagged

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