Automatizar igualación a cero
Publicado por Lucas (46 intervenciones) el 01/05/2020 22:08:38
Les comento, estoy pasando a una base de datos mysql un formulario con 20 valores numéricos, los cuales se transfieren entre 4 filas (4 por cada fila). Quiero que cada vez que se deje un valor vacío se complete automáticamente con un 0 a la hora de pasarse a la base de datos, y si es posible evitar copiar 20 veces un mismo código. Los elementos están llamados "elemento1", "elemento2", "elemento3", etc... hasta el "elemento20"
Se me había ocurrido usar un bucle como el siguiente pero no funcionó:
aunque era dudoso que funcione desde un primer momento jaja
A continuación tambien les adjunto el codigo completo por si puede llegar a servir:
Se me había ocurrido usar un bucle como el siguiente pero no funcionó:
1
2
3
4
5
6
for($i=1;$i<21;$i++){
$elementogeneral = "$" . "elemento" . $i;
if(is_null($elementogeneral)){
$elementogeneral=0;
}
}
aunque era dudoso que funcione desde un primer momento jaja
A continuación tambien les adjunto el codigo completo por si puede llegar a servir:
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="formulario.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
<title>Formulario</title>
<link href="https://fonts.googleapis.com/css?family=Baloo+Bhaina+2&display=swap" rel="stylesheet">
</head>
<body>
<div class="total">
<div calss="titulo">
<h1>Corte</h1>
</div>
<form name="form" method="post" action="formulario.php">
<div class="comunes">
<label for="fecha">Fecha</label>
<input type="date" name="fecha" >
<label for="lote">Lote</label>
<input name="lote" type="text" autocomplete="off">
</div>
<div class="cuerpo">
<div class="nombre">
<label for="nombre">Nombre</label><input type="text" name="nombre"autocomplete="off">
</div>
<input type="number" name="elemento1">
<input type="number" name="elemento2">
<input type="number" name="elemento3">
<input type="number" name="elemento4">
<input type="number" name="elemento5">
<div class="nombre">
<label for="nombre">Nombre</label><input type="text" name="nombre2"autocomplete="off">
</div>
<input type="number" name="elemento6">
<input type="number" name="elemento7">
<input type="number" name="elemento8">
<input type="number" name="elemento9">
<input type="number" name="elemento10">
<div class="nombre">
<label for="nombre">Nombre</label><input type="text" name="nombre3"autocomplete="off">
</div>
<input type="number" name="elemento11">
<input type="number" name="elemento12">
<input type="number" name="elemento13">
<input type="number" name="elemento14">
<input type="number" name="elemento15">
<div class="nombre">
<label for="nombre">Nombre</label><input type="text" name="nombre4"autocomplete="off">
</div>
<input type="number" name="elemento16">
<input type="number" name="elemento17">
<input type="number" name="elemento18">
<input type="number" name="elemento19">
<input type="number" name="elemento20">
<input type="submit" value="Listo" name="listo">
</div>
</div>
</from>
</div>
<!-- -------------------------------------------------------------------------------------------------------------------- -->
<?php
if(isset($_POST["listo"])){
$nombre=$_POST["nombre"];
$fecha=$_POST["fecha"];
$elemento1=$_POST["elemento1"];
$elemento2=$_POST["elemento2"];
$elemento3=$_POST["elemento3"];
$elemento4=$_POST["elemento4"];
$elemento5=$_POST["elemento5"];
$lote=$_POST["lote"];
$suma=$elemento1+$elemento2+$elemento3+$elemento5+$elemento4;
$conexion= new mysqli ("localhost", "id12035304_aks4", "CS9&Zs^wP\cX%FE", "id12035304_planillas");
if($conexion->errno){
echo "fallo en la conexión" . $conexion->connect_errno;
}
$conexion->set_charset("utf8");
$insertar="INSERT INTO DATOS(FECHA, LOTE, NOMBRE, ELEMENTO1, ELEMENTO2, ELEMENTO3, ELEMENTO4, ELEMENTO5, SUMA) VALUES('$fecha', '$lote', '$nombre', $elemento1, $elemento2, $elemento3,$elemento4,$elemento5, $elemento1+$elemento2+$elemento3+$elemento4+$elemento5)";
$conexion->query($insertar);
if($conexion->errno){
echo $conexion->connect_errno . "<br>";
}
$nombre=$_POST["nombre2"];
$fecha=$_POST["fecha"];
$elemento1=$_POST["elemento6"];
$elemento2=$_POST["elemento7"];
$elemento3=$_POST["elemento8"];
$elemento4=$_POST["elemento9"];
$elemento5=$_POST["elemento10"];
$lote=$_POST["lote"];
$suma=$elemento1+$elemento2+$elemento3+$elemento5+$elemento4;
$insertar="INSERT INTO DATOS(FECHA, LOTE, NOMBRE, ELEMENTO1, ELEMENTO2, ELEMENTO3, ELEMENTO4, ELEMENTO5, SUMA) VALUES('$fecha', '$lote', '$nombre', $elemento1, $elemento2, $elemento3,$elemento4,$elemento5, $elemento1+$elemento2+$elemento3+$elemento4+$elemento5)";
$conexion->query($insertar);
if($conexion->errno){
echo $conexion->connect_errno . "<br>";
}
$nombre=$_POST["nombre3"];
$fecha=$_POST["fecha"];
$elemento1=$_POST["elemento11"];
$elemento2=$_POST["elemento12"];
$elemento3=$_POST["elemento13"];
$elemento4=$_POST["elemento14"];
$elemento5=$_POST["elemento15"];
$lote=$_POST["lote"];
$suma=$elemento1+$elemento2+$elemento3+$elemento5+$elemento4;
$insertar="INSERT INTO DATOS(FECHA, LOTE, NOMBRE, ELEMENTO1, ELEMENTO2, ELEMENTO3, ELEMENTO4, ELEMENTO5, SUMA) VALUES('$fecha', '$lote', '$nombre', $elemento1, $elemento2, $elemento3,$elemento4,$elemento5, $elemento1+$elemento2+$elemento3+$elemento4+$elemento5)";
$conexion->query($insertar);
if($conexion->errno){
echo $conexion->connect_errno . "<br>";
}
$nombre=$_POST["nombre4"];
$fecha=$_POST["fecha"];
$elemento1=$_POST["elemento16"];
$elemento2=$_POST["elemento17"];
$elemento3=$_POST["elemento18"];
$elemento4=$_POST["elemento19"];
$elemento5=$_POST["elemento20"];
$lote=$_POST["lote"];
$suma=$elemento1+$elemento2+$elemento3+$elemento5+$elemento4;
$insertar="INSERT INTO DATOS(FECHA, LOTE, NOMBRE, ELEMENTO1, ELEMENTO2, ELEMENTO3, ELEMENTO4, ELEMENTO5, SUMA) VALUES('$fecha', '$lote', '$nombre', $elemento1, $elemento2, $elemento3,$elemento4,$elemento5, $elemento1+$elemento2+$elemento3+$elemento4+$elemento5)";
$conexion->query($insertar);
if($conexion->errno){
echo $conexion->connect_errno . "<br>";
}
}
?>
</body>
</html>
Valora esta pregunta


0