Problema para hacer un update desde formulario.
Publicado por jose (71 intervenciones) el 10/12/2020 19:22:31
Buenas tardes,
Tengo un problema para poder actualizar registros desde un formulario de php. Tengo varios formularios, código que repito mucho, y que me están funcionando en todos menos este, llevo dos días buscando una solución y no veo solución.
Por una parte no puedo actualizar, a pesar que al pulsar el botón Editar redirige a la página.
En el registro de la base de datos no muestra ningún cambio.
y por otra en los select del valor tipo y vivienda no aparece el valor actual.
Sin embargo al ponerlo en input text se ve correcto.
Agradecería si alguien me pudiese ayudar a encontrar el fallo.
Expongo la tabla de mysql y luego el php.
Muchas gracias de antemano.
el show create table seguros;
El archivo php entero
Tengo un problema para poder actualizar registros desde un formulario de php. Tengo varios formularios, código que repito mucho, y que me están funcionando en todos menos este, llevo dos días buscando una solución y no veo solución.
Por una parte no puedo actualizar, a pesar que al pulsar el botón Editar redirige a la página.
En el registro de la base de datos no muestra ningún cambio.
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
<?php
include("../conn/connection.php");
if (isset($_POST["submit"])) {
# retrieve information from form.
$entidad = $_POST['entidad'];
$tipo = $_POST['tipo'];
$vivienda = $_POST['vivienda'];
$poliza = $_POST['poliza'];
$telefono = $_POST['telefono'];
$cantidad = $_POST['cantidad'];
$fecha_contratacion = $_POST['fecha_contratacion'];
$descripcion = $_POST['descripcion'];
$expirado = (isset($_POST['expirado'])) ? 1 : 0;
$query = "UPDATE seguros SET entidad='$entidad',tipo='$tipo',vivienda='$vivienda',poliza='$poliza',telefono='$telefono',fecha_contratacion = '$fecha_contratacion',cantidad='$cantidad',descripcion ='$descripcion',expirado = $expirado WHERE id = '".$id."'";
$result = mysqli_query($conn,$query);
if ($result){
header("location:seguros.php");
}else{
echo "Error";
}
}
?>
y por otra en los select del valor tipo y vivienda no aparece el valor actual.
1
<option value="<?php echo $row['tipo']; ?>"></option><!--No aparece en el select -->
Sin embargo al ponerlo en input text se ve correcto.
Agradecería si alguien me pudiese ayudar a encontrar el fallo.
Expongo la tabla de mysql y luego el php.
Muchas gracias de antemano.
el show create table seguros;
1
2
3
4
5
6
7
8
9
10
11
12
13
CREATE TABLE `seguros` (
`id` smallint(11) NOT NULL AUTO_INCREMENT,
`entidad` varchar(25) CHARACTER SET utf8 DEFAULT NULL,
`tipo` varchar(25) CHARACTER SET utf8 DEFAULT NULL,
`vivienda` varchar(50) COLLATE utf8_general_mysql500_ci DEFAULT NULL,
`poliza` varchar(20) COLLATE utf8_general_mysql500_ci DEFAULT NULL,
`telefono` varchar(20) COLLATE utf8_general_mysql500_ci DEFAULT NULL,
`fecha_contratacion` timestamp NOT NULL DEFAULT current_timestamp(),
`cantidad` decimal(10,2) NOT NULL DEFAULT 0.00,
`descripcion` varchar(255) COLLATE utf8_general_mysql500_ci DEFAULT NULL,
`expirado` tinyint(3) DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 COLLATE=utf8_general_mysql500_ci
El archivo php entero
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
require('../session/cookie.php');
ob_start();
session_start();
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
header("location:../../index.php");
}
?>
<script language="JavaScript">
//Evitar botón derecho & Cntrl - Shift + I,C,J, o Cntrl U
document.oncontextmenu = function(){return false}
document.onkeydown = function(e) {
if(event.keyCode == 123) {
return false;
}
if(e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0)) {
return false;
}
if(e.ctrlKey && e.shiftKey && e.keyCode == 'C'.charCodeAt(0)) {
return false;
}
if(e.ctrlKey && e.shiftKey && e.keyCode == 'J'.charCodeAt(0)) {
return false;
}
if(e.ctrlKey && e.keyCode == 'U'.charCodeAt(0)) {
return false;
}
}
function pregunta(){
if (confirm('¿Estas seguro de guardar este registro?')){
return true;
}
return false;
}
function countChars(obj){
var maxLength = 255;
var strLength = obj.value.length;
var charRemain = (maxLength - strLength);
if(charRemain <= 5){
document.getElementById("charNum").innerHTML = '<span style="color: red;">Faltan menos de '+charRemain+' caracteres</span>';
}else{
document.getElementById("charNum").innerHTML = '<span style="color: green;">Faltan '+charRemain+' caracteres</span>';
}
}
</script>
<!DOCTYPE html>
<html>
<head>
<title>Actualización de registro seguros</title>
<meta name="viewport" content="width=device-width, intial-scale=1.0">
<meta name="guardar_seguros" content="Seguros">
<meta http-equiv="content-type" content="text/html; charset=UTF-8" >
<link rel="stylesheet" type="text/css" href="../../css/seguros.css">
</head>
<body>
<div id="base">
<div id="log_out">
<a id="log_out_button" href="../session/logout.php">Cerrar Sesión</a>
</div>
<div id="pic">
<picture>
<img id="imagen" src="../../images/recibos.png" alt="Recibos">
</picture>
</div>
<div>
<h1>Actualización registro de inquilinos</h1>
<div id="menu"><a class="button" href="seguros.php">Inicio</a>-<a class="button" href="search.php">Busqueda</a></div>
</div>
<?php
require("../conn/connection.php");
if(isset($_GET['GetID'])){
$id = $_GET['GetID'];
$query="Select * from seguros where id = '".$id ."'";
$result = mysqli_query($conn,$query);
while($row=$result->fetch_array()){
?>
<div>
<center>
<form action="seguros.php" name="formulario" method="POST" enctype="multipart/form-data">
<br>
Entidad: <input type="text" name="entidad" placeholder="entidad" value="<?php echo $row['entidad']; ?>" size="50" maxlength="50" required /><br><br>
Tipo: <select name="tipo">
<option value="<?php echo $row['tipo']; ?>"></option><!--No aparece en el select -->
<option value="Hogar">Hogar</option>
<option value="Alquiler">Alquiler</option>
<option value="Hogar+Alquiler">Hogar+Alquiler</option>
<option value="Coche">Coche</option>
</select>
Vivienda: <select name="vivienda">
<option value="<?php echo $row['vivienda']; ?>"></option><!--No aparece en el select -->
<option value="Calle Avila">Calle Avila</option>
<option value="Pablo Casals">Calle Pablo Casals</option>
<option value="Lago Tiberiades">Calle Lago Tiberiades</option>
<option value="Calle Mesones">Calle Mesones</option>
</select>
Poliza: <input type="text" name="poliza" placeholder="poliza" value="<?php echo $row['poliza']; ?>"size="20" maxlength="20" /><br><br>
Telefono: <input type="text" name="telefono" placeholder="telefono" value="<?php echo $row['telefono']; ?>"size="15" maxlength="15" /><br><br>
Cantidad: <input type="number" name="cantidad" value="<?php echo $row['cantidad']; ?>" step=".01" required /><br><br>
Fecha de contratación: <input type="date" name="fecha_contratacion" value="<?php echo date("Y-m-d", strtotime($row['fecha_contratacion'])); ?>"/><br><br>
Expirado: <input type="checkbox" name="expirado" <?= $row['expirado']=='1'?"checked='checked'" : "" ?> /><br><br>
Descripción: <textarea name="descripcion" cols="40" rows="5" > <?php echo htmlspecialchars($row['descripcion']) ?></textarea><br><br>
<input class="button" type="submit" name="submit" onclick="return pregunta()" value="Editar" />
<input class="button" type="reset">
</form>
</center>
</div>
<!--FIN DIV BASE-->
</div>
</body>
</html>
<?php }
} ?>
<?php
include("../conn/connection.php");
if (isset($_POST["submit"])) {
# retrieve information from form.
$entidad = $_POST['entidad'];
$tipo = $_POST['tipo'];
$vivienda = $_POST['vivienda'];
$poliza = $_POST['poliza'];
$telefono = $_POST['telefono'];
$cantidad = $_POST['cantidad'];
$fecha_contratacion = $_POST['fecha_contratacion'];
$descripcion = $_POST['descripcion'];
$expirado = (isset($_POST['expirado'])) ? 1 : 0;
$query = "UPDATE seguros SET entidad='$entidad',tipo='$tipo',vivienda='$vivienda',poliza='$poliza',telefono='$telefono',fecha_contratacion = '$fecha_contratacion',cantidad='$cantidad',descripcion ='$descripcion',expirado = $expirado WHERE id = '".$id."'";
$result = mysqli_query($conn,$query);
if ($result){
header("location:seguros.php");
}else{
echo "Error";
}
}
?>
<?php
/* Cerrar la conexión */
mysqli_close($conn);
ob_end_flush();
?>
Valora esta pregunta


0