'How can I store data from JS into a MySQL database

First things first, I'm new to the forum, anything I write that isn't concise or just flatt out adjective let me know!

I'm developing an app that can retrieve data from the TMDB API in JavaScript, so then I can show it to the user in PHP. Everything is working fine but there's a slight tweak I want to make. I want to store the retrieved data from the API into a database I've created so I can store specific movies and show them to the user, sort of like a "Favorite" tab.

Database name "Movies-info" (this is the only table for test purposes)

CREATE TABLE Movies(
    ID int NOT NULL,
    Producer_ID int NOT NULL,
    GenreID int NOT NULL,
    transcode_ID int NOT NULL,
    title varchar(255) NOT NULL,
    summary varchar(255) NOT NULL,
    release_date date NOT NULL,
    runtime time
)ENGINE=InnoDB DEFAULT CHARSET=latin1;

JavaScript function that returns the movie information from the API and the the database connection that isn't working

    function getMovie() {
    let movieId = sessionStorage.getItem('id');

    //test id -> 299536
    axios.get(`https://api.themoviedb.org/3/movie/${movieId}?api_key=5ec279387e9aa9488ef4d00b22acc451`)
        .then((response) => {
            console.log(response);
            let movie = response.data;

            if (movie.poster_path === null) {
                poster = "../image/default-movie.png";
            } else {
                poster = "https://image.tmdb.org/t/p/w185_and_h278_bestv2" + movie.poster_path;
            }

            let date = movie.release_date;

            let year = date.slice(0, 4);
            let Rated;

            let revenue = movie.revenue / 1000000;
            let budget = movie.budget / 1000000;
            revenue = Math.round(revenue);
            budget = Math.round(budget);

            if (revenue === 0) {
                revenue = "Revenue is less than million dollers"
            }

            if (budget === 0) {
                budget = "Budget is less than million dollers"
            }

            let genre = [];
            movie.genres.forEach(element => {
                genre.push(element.name);
            });

            genres = genre.join(' / ');

            let output1 = `
            <div class="row">
                <div class="col-md-4 box1">
                    <img src="${poster}" class="poster-image">
                </div>
                <div class="col-md-4 box2">
                    <h1 class="movie-title">${movie.title}</h1>

                    <h5 style="color: white; font-weight:bold">${year}</h5>
                    <h5 style="color: white; font-weight:bold; margin-top: -10px;">${genres}</h5>

                    <ul class="list-group">
                        <li class="list-group-item active">
                            <strong>Rating: </strong> ${movie.vote_average} / 10</li>
                        <li class="list-group-item active">
                            <strong>Status: </strong> ${movie.status}</li>
                        <li class="list-group-item active">
                            <strong>Duration: </strong> ${movie.runtime} min</li>
                        <li class="list-group-item active">
                            <strong>Budget: </strong> $ ${budget} million</li>
                        <li class="list-group-item active">
                            <strong>Revenue: </strong> $ ${revenue} million</li>
                    </ul>

                </div>
            </div>
            `
            $('#movie').html(output1);
        })
        .catch((error) => {
            console.log(error);
        });

var conn = mysql.createConnection({
        host: "localhost",
        user: "root",
        password:"",
        db: "Movies-info"
    });

    conn.connect(function(err, conn) {
        if(err)throw err;
        var sql = `INSERT INTO Movies (ID, title, summary) VALUES ('${movieID}', '${movie.title}', '${movie.overview}')`
        conn.query(sql, function(err, result) {
            if(err)throw err;
            console.log("1 entry added");
            console.log(result);
        })
    });
}

Any additional information you need let me know, thanks in advance!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source