D. php / producto-agrega.php

1
<?php
2
3
require_once __DIR__ . "/lib/manejaErrores.php";
4
require_once __DIR__ . "/lib/recibeTextoObligatorio.php";
5
require_once __DIR__ . "/lib/recibeBytesObligatorios.php";
6
require_once __DIR__ . "/lib/devuelveCreated.php";
7
require_once __DIR__ . "/Bd.php";
8
require_once __DIR__ . "/archivoAgrega.php";
9
10
$nombre = recibeTextoObligatorio("nombre");
11
$bytes = recibeBytesObligatorios("imagen");
12
13
$bd = Bd::pdo();
14
$bd->beginTransaction();
15
16
$archId = archivoAgrega($bd, $bytes);
17
18
$stmt = $bd->prepare(
19
 "INSERT INTO PRODUCTO (
20
    PROD_NOMBRE, ARCH_ID
21
   ) values (
22
    :PROD_NOMBRE, :ARCH_ID
23
   )"
24
);
25
$stmt->execute([
26
 ":PROD_NOMBRE" => $nombre,
27
 ":ARCH_ID" => $archId
28
]);
29
$prodId = $bd->lastInsertId();
30
31
$bd->commit();
32
33
$encodeId = urlencode($prodId);
34
$encodeArchId = urlencode($archId);
35
$htmlEncodeArchId = htmlentities($encodeArchId);
36
// Los bytes de las imágenes se descargan con "archivo.php"; no desde aquí.
37
devuelveCreated("/php/producto-vista-modifica.php?id=$encodeId", [
38
 "id" => ["value" => $prodId],
39
 "nombre" => ["value" => $nombre],
40
 "imagen" => [
41
  "imagen" => [
42
   "data-src" => $htmlEncodeArchId === ""
43
    ? ""
44
    : "php/archivo.php?id=$htmlEncodeArchId"
45
  ]
46
 ]
47
]);
48
skip_previous skip_next