E
Apart from what @Alex tells you, it is much clearer to make binding with separate variables than to use an array. Also, if you use ready-made queries, you don't need or real_escape_string and htmlentities. You can recover each data in variables and use them for binding. To avoid undefined index you can use a calf to check with empty If every data is, making assignment at the same time.You could write the code like that at no risk:include "../../basedd/archivoconexion.php";
$conexion=conexion();
$nombre =!empty($_POST['nombre']) ? $_POST['nombre'] : NULL;
$fecha =!empty($_POST['fecha']) ? $_POST['fecha'] : NULL;
$hora =!empty($_POST['hora']) ? $_POST['hora'] : NULL;
$entidad =!empty($_POST['entidad']) ? $_POST['entidad'] : NULL;
$municipio =!empty($_POST['municipio']) ? $_POST['municipio'] : NULL;
$tiempo1 =!empty($_POST['tiempo1']) ? $_POST['tiempo1'] : NULL;
$tiempo2 =!empty($_POST['tiempo2']) ? $_POST['tiempo2'] : NULL;
$checkboxes =!empty($_POST['checkboxes']) ? $_POST['checkboxes'] : NULL;
$otromot =!empty($_POST['otromot']) ? $_POST['otromot'] : NULL;
$sql="INSERT INTO nombre_tabla (nombre,fecha,hora,entidad,municipio,tiempo1,tiempo2,checkboxes,otromot) VALUES (?,?,?,?,?,?,?,?,?)";
if ($query=$conexion->prepare($sql)) {
$query->bind_param('sssssssss',$nombre,$fecha,$hora,$entidad,$municipio,$tiempo1,$tiempo2,$checkboxes,$otromot);
if($query->execute()) {
$msg= sprintf("Se insertaron %d filas",$conexion-> affected_rows);
$query->close();
} else {
$msg="Error en la inserción";
}
} else {
$msg="Error preparando la consulta";
}
echo $msg;
You'll see I've set up controls missing in the code. Preparation may fail, not necessarily a query will be inserted because of the type INSERT INTO. A common failure in this type of consultation is when restrictions that may have the table are violated. You cannot therefore determine that a query worked with a simple echo about the `execute.In the way of recovering the variables with the ternary, you can control those data that are mandatory, that have the restriction NOT NULL on the table, sending an error message to the client instead of trying a query that will be failed. Since I don't know that fact, I haven't put anything. Suppose $nombre and $fecha are mandatory, then that would be the first control before attempting to insert:if ($nombre && $fecha) {
//... todo el código de insertar
} else {
$msg="Hay campos obligatorios sin llenar";
}
echo $msg;
PD:There's a way to simplify the bind_paramto pass them as an array, using https://www.php.net/manual/es/functions.arguments.php#functions.variable-arg-list .