[PHP-NOTES] note 130326 added to pdo.lastinsertid

[email protected] ("[email protected]")
Newsgroups php.notes
Message-ID <[email protected]>
EJEMPLO:
public function insertarResena($nif, $referencia, $fecha, $clasificacion, $comentario) {
    try {
        $sql = "INSERT INTO " . self::TABLA . " (nif, referencia, fecha, clasificacion, comentario)
                VALUES (:nif, :referencia, :fecha, :clasificacion, :comentario)";
        $stmt = $this->pdo->prepare($sql);
        $stmt->bindParam(":nif", $nif);
        $stmt->bindParam(":referencia", $referencia);
        $stmt->bindParam(":fecha", $fecha); // Asegúrate de pasar un string 'Y-m-d' o DateTime formateado
        $stmt->bindParam(":clasificacion", $clasificacion, PDO::PARAM_INT);
        $stmt->bindParam(":comentario", $comentario);
        $stmt->execute();
        return $this->pdo->lastInsertId();
    } catch (Exception $e) {
        throw new Exception('Error al insertar la reseña');
    }
}
COntroller
$this->peticiones = [
    'buscarResenas' => ['modelo' => ModeloResenaNC::class, 'vista' => VistaResenaNC::class],
    'insertarResena' => ['modelo' => ModeloResenaNC::class, 'vista' => VistaResenaNC::class] // O la vista que prefieras para mostrar resultado
];
Modifica el método gestionarPeticion() para manejar la nueva petición:

php
public function gestionarPeticion() {
    try {
        $peticion = $_POST['peticion'] ?? null;

        if (!$peticion || !isset($this->peticiones[$peticion])) {
            throw new Exception('La petición es inválida');
        }
        $modeloClass = $this->peticiones[$peticion]['modelo'];
        $vistaClass = $this->peticiones[$peticion]['vista'];

        if (!class_exists($modeloClass) || !class_exists($vistaClass)) {
            throw new Exception('Ha habido un problema con el modelo o con la vista');
        }
        $modelo = new $modeloClass();

        if ($peticion === 'insertarResena') {
            // Recoge los datos del formulario
            $nif = $_POST['nif'] ?? null;
            $referencia = $_POST['referencia'] ?? null;
            $fecha = $_POST['fecha'] ?? date('Y-m-d');
            $clasificacion = $_POST['clasificacion'] ?? null;
            $comentario = $_POST['comentario'] ?? null;

            $idInsertado = $modelo->insertarResena($nif, $referencia, $fecha, $clasificacion, $comentario);
            $vista = new $vistaClass();
            $vista->enviarSalida(['mensaje' => 'Reseña insertada con ID: ' . $idInsertado]);
        } else {
            // Petición buscarResenas
            $nif = $_POST['nif'] ?? null;
            $reseñas = $modelo->procesaPeticion($nif);
            $vista = new $vistaClass();
            $vista->enviarSalida($reseñas);
        }
    } catch (Exception $e) {
        $vistaError = new \vista\VistaErrorNC();
        $vistaError->muestraError($e);
    }
}
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 87.223.130.93)
----
Manual Page -- https://php.net/manual/en/pdo.lastinsertid.php
Edit        -- https://main.php.net/note/edit/130326
Del: integrated  -- https://main.php.net/note/delete/130326/integrated
Del: useless     -- https://main.php.net/note/delete/130326/useless
Del: bad code    -- https://main.php.net/note/delete/130326/bad+code
Del: spam        -- https://main.php.net/note/delete/130326/spam
Del: non-english -- https://main.php.net/note/delete/130326/non-english
Del: in docs     -- https://main.php.net/note/delete/130326/in+docs
Del: other reasons-- https://main.php.net/note/delete/130326
Reject      -- https://main.php.net/note/reject/130326
Search      -- https://main.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.