-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertarProducto.php
113 lines (94 loc) · 4.22 KB
/
insertarProducto.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
session_start();
if ($_SESSION == null || $_SESSION["usuario"] == null) {
echo "No estás autorizado para ver esta página.";
} else {
// Conexión a la base de datos
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "futboleros";
$bbdd = mysqli_connect($servername, $username, $password, $dbname);
if (mysqli_connect_error()) {
printf("Error conectando a la base de datos: %s\n", mysqli_connect_error());
exit();
}
// Verificar si se envió el formulario de inserción
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$descripcion = isset($_POST['nombreProducto']) ? $_POST['nombreProducto'] : "";
$precio = isset($_POST['precio']) ? $_POST['precio'] : "";
$existencias = isset($_POST['existencia']) ? $_POST['existencia'] : "";
$imagen = "";
if (isset($_FILES['imagen'])) {
$file = $_FILES['imagen'];
$filename = $file['name'];
$filetmp = $file['tmp_name'];
// Verificar si se seleccionó una imagen
if (!empty($filename)) {
$ext = pathinfo($filename, PATHINFO_EXTENSION);
// Insertar nuevo producto
$sql = "INSERT INTO productos (descripcion, precio, existencias, imagen) VALUES ('$descripcion', $precio, $existencias, '')";
$result = $bbdd->query($sql);
if ($result) {
$codigo = $bbdd->insert_id; // Obtener el código del nuevo producto insertado
$imagen = "images/$codigo.$ext";
// Actualizar la URL de la imagen en la base de datos
$sql = "UPDATE productos SET imagen = '$imagen' WHERE codigo = $codigo";
$result = $bbdd->query($sql);
if ($result) {
move_uploaded_file($filetmp, $imagen);
mysqli_close($bbdd);
echo '<script>alert("Producto insertado en la base de datos satisfactoriamente.\nHas añadido: '.$descripcion.'\nPrecio: '.$precio.'\nExistencias: '.$existencias.'"); window.location.href = "adminProductos.php";</script>';
exit();
} else {
$error_message = "Error al guardar el producto.";
}
} else {
$error_message = "Error al guardar el producto.";
}
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Insertar producto | Futboleros</title>
<meta charset="UTF-8">
<link rel="shortcut icon" href="./images/icon.png">
<link rel="stylesheet" href="css/general.css">
<link rel="stylesheet" href="css/form.css">
<link rel="stylesheet" href="css/boton.css">
<script src="js/libCapas2122.js"></script>
</head>
<body>
<header>FUTBOLER@S</header>
<nav class="topnav" id="myTopnav">
<a href="adminUsuarios.php">Usuarios</a>
<a href="adminProductos.php">Productos</a>
<a href="adminPedidos.php">Pedidos</a>
<a href="logout.php">Cerrar sesión</a>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</nav>
<div class="content">
<h1>Insertar nuevo producto</h1>
<form method="POST" action="" enctype="multipart/form-data">
<label for="nombreProducto">Descripción:</label>
<input type="text" id="nombreProducto" name="nombreProducto" required>
<label for="precio">Precio:</label>
<input type="number" id="precio" name="precio" required>
<label for="existencia">Existencias:</label>
<input type="number" id="existencia" name="existencia" required>
<label for="imagen">Imagen:</label>
<input type="file" id="imagen" name="imagen">
<input class="button2" type="submit" value="Guardar">
</form>
</div>
</body>
</html>
<?php
mysqli_close($bbdd);
}
?>