-1

right now I'm doing a project about a movie database. One action is create and add to the database a film and I tried a lot of things but I couldn't find my mistake. Here is the code.

First set: html code. Here i take all the data. codigoNuevo is a attempt to work, but I am not using actively.

<fieldset>
    <legend> Datos de la película </legend>
    <table>
        <tr>
            <td> <label>Nº - automático</label> </td>
            <td> <input type="text" name="codigoNuevo" placeholder="/!\ No introducir ningún dato." readonly value="" size="50" maxlength="50" required> </input> </td>
        </tr>
        <tr>
            <td> <label>Nombre</label> </td>
            <td> <input type="text" name="nombre" value="" size="50" maxlength="50" required> </input> </td>
        </tr>
        <tr>
            <td> <label>Director</label> </td>
            <td> <input type="text" name="director" value="" size="50" maxlength="50" required> </input> </td>
        </tr>
        <tr>
            <td> <label>Género</label> </td>
            <td> <input type="text" name="genero" value="" size="50" maxlength="50" required> </input> </td>
        </tr>
        <tr>
            <td> <label>Póster</label> </td>
            <td> <input type="file" name="poster" value=""> </input> </td>
        </tr>
        <tr>
            <td> <label>URL del tráiler</label> </td>
            <td> <input type="text" name="link" value="" size="50" maxlength="50" required> </input> </td>
        </tr>
    </table>
</fieldset>

Second set: the controller

function ctlPeliInsertar() {
    $nombre = $_POST['nombre'];
    $director = $_POST['director'];
    $genero = $_POST['genero'];
    $imagen = $_FILES['poster']['name'];
    $url = $_POST['link'];
    $peliculas = ModeloUserDB::insertarPelicula($nombre, $director, $genero, $imagen, $url);
    include_once 'plantilla/verpeliculas.php';


    // necesito una funcion para subir fotos al (subirfichero)
    // transportar desde tmp (fichero temporal) al app/img
}

Third set: the function

public static function insertarPelicula($nombre, $director, $genero, $imagen, $url) {
    echo "Nombre a introducir: "  . $nombre . "<br>";
    echo "Director a introducir: " . $director . "<br>";
    echo "Género a introducir: " . $genero . "<br>";
    echo "Imagen/enl. a introducir: " . $imagen . "<br>";
    echo "URL a introducir: " . $url . "<br>";

    $stmt = self::$dbh->prepare(self::$insertarPelicula);
    $stmt->bindValue(1, $nombre);
    $stmt->bindValue(2, $director);
    $stmt->bindValue(3, $genero);
    $stmt->bindValue(4, $imagen);
    $stmt->bindValue(5, $url);
    $stmt->execute();
}

The error log shows the $stmt -> execute() is the problem. Here is the full log:

Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\Pelisapp\app\modeloPeliDB.php:85 Stack trace: #0 C:\xampp\htdocs\Pelisapp\app\modeloPeliDB.php(85): PDOStatement->execute() #1 C:\xampp\htdocs\Pelisapp\app\controlerPeli.php(94): ModeloUserDB::insertarPelicula('Hola', 'Que', 'Tal', '', 'ggg') #2 C:\xampp\htdocs\Pelisapp\index.php(52): ctlPeliInsertar() #3 {main} thrown in C:\xampp\htdocs\Pelisapp\app\modeloPeliDB.php on line 85

Where is my mistake? Thanks in advance.


Here is the insert;

INSERT INTO `peliculas`(`codigo_pelicula`, `nombre`, `director`, `genero`, `imagen`, `url`) VALUES ('[value-1]','[value-2]','[value-3]','[value-4]','[value-5]','[value-6]')

I tried to add new fields, other prepared sentences... I am frustrated because it is the last order I need to finish this.

Arpit Jain
  • 1,599
  • 9
  • 23
Movilla
  • 1
  • 1
  • For what it's wroth, a phpMyAdmin insert is not a thing. phpMyAdmin is just one of the many programs you can use to interact with MySQL Server, it doesn't implement its own query language. When you look for documentation about SQL, look for MySQL, not phpMyAdmin. – Álvaro González Apr 30 '23 at 09:41

1 Answers1

-2

In your phpmyadmin insert statement, you have six wildcards:

INSERT INTO peliculas(codigo_pelicula, nombre, director, genero, imagen, url) VALUES ('[value-1]','[value-2]','[value-3]','[value-4]','[value-5]','[value-6]')

but in your prepare statement, you only have five

    $stmt = self::$dbh->prepare(self::$insertarPelicula);
    $stmt->bindValue(1, $nombre);
    $stmt->bindValue(2, $director);
    $stmt->bindValue(3, $genero);
    $stmt->bindValue(4, $imagen);
    $stmt->bindValue(5, $url);

That's why you are getting a mismatch in the number of tokens

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
mankowitz
  • 1,864
  • 1
  • 14
  • 32
  • private static $insertarPelicula = "Insert into peliculas (`nombre`, `director`, `genero`, `imagen`, `url`) VALUES ('?','?','?','?','?')"; // Here is the new insert statement and it gives the same log error. – Movilla Apr 30 '23 at 02:08
  • try without quotes around the question marks – mankowitz Apr 30 '23 at 02:11