I'm doing a fill-in-registration form where everything was fine except for the select, the code is as follows:
Database:
CREATE TABLE usuarios(
id INT NOT NULL UNIQUE AUTO_INCREMENT,
tipo_de_usuario VARCHAR(100) NOT NULL,
nombre VARCHAR(25) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
fecha_registro DATETIME NOT NULL,
activo TINYINT NOT NULL,
PRIMARY KEY(id)
);
Constructor:
class ValidadorRegistro {
private $aviso_inicio;
private $aviso_cierre;
private $tipo_de_usuario;
private $nombre;
private $email;
private $clave;
private $error_tipo_de_usuario;
private $error_nombre;
private $error_email;
private $error_clave1;
private $error_clave2;
public function __construct($tipo_de_usuario, $nombre, $email, $clave1, $clave2, $conexion) {
$this->aviso_inicio="<br><div class='alert alert-danger' role='alert'>";
$this->aviso_cierre="</div>";
$this->tipo_de_usuario = "";
$this->nombre = "";
$this->email = "";
$this->clave ="";
$this->error_tipo_de_usuario = $this->validar_tipo_de_usuario($tipo_de_usuario);
$this->error_nombre = $this->validar_nombre($conexion, $nombre);
$this->error_email = $this->validar_email($conexion, $email);
$this->error_clave1 = $this->validar_clave1($clave1);
$this->error_clave2 = $this->validar_clave2($clave1, $clave2);
if($this->error_clave1 === "" && $this->error_clave2 === ""){
$this-> clave = $clave1;
}
}
Select validator:
private function validar_tipo_de_usuario($tipo_de_usuario) {
if ($this->variable_iniciada($tipo_de_usuario)) {
return "Debes seleccionar un tipo de usuario.";
} else if ($tipo_de_usuario == "Individuo (Persona Natural)"){
$this-> tipo_de_usuario = $tipo_de_usuario;
} else if ($tipo_de_usuario == "Empresa o negocio (Persona Juridica)"){
$this-> tipo_de_usuario = $tipo_de_usuario;
}
Show error type of user (select):
$this->error_tipo_de_usuario = $this->validar_tipo_de_usuario($tipo_de_usuario);
public function mostrar_tipo_de_usuario(){
if($this->tipo_de_usuario!==""){
echo 'value="'. $this -> tipo_de_usuario . '"';
}
}
public function obtener_error_tipo_de_usuario(){
return $this-> error_tipo_de_usuario;
}
$this->aviso_inicio="<br><div class='alert alert-danger' role='alert'>";
$this->aviso_cierre="</div>";
public function mostrar_error_tipo_de_usuario(){
if($this->error_tipo_de_usuario!==""){
echo $this->aviso_inicio . $this -> error_tipo_de_usuario . $this->aviso_cierre;
}
}
Valid record: Insert in the database only if there are no errors in any fields to fill (just in case this code fragment)
public function registro_valido(){
if($this-> error_tipo_de_usuario === "" && $this-> error_nombre === "" && $this-> error_email === "" && $this-> error_clave1 === "" && $this-> error_clave2 === ""){
return true;
}else{
return false;
}
}
Select Empty:
<div class="form-group">
<label>Tipo de Usuario: </label>
<select name="tipo_de_usuario" class="form-control">
<option>Seleccione... </option>
<option value="Individuo (Persona Natural)">Individuo (Persona Natural)</option>
<option value="Empresa o negocio (Persona Juridica)">Empresa o negocio (Persona Juridica)</option>
</select>
</div>
Select Validated (What should appear when one selects one of the options and does not enter another data or entered it incorrectly):
<div class="form-group">
<label>Tipo de Usuario: </label>
<select name="tipo_de_usuario" class="form-control">
<option>Seleccione...</option>
<option value="Individuo (Persona Natural)">Individuo (Persona Natural)</option>
<option value="Empresa o negocio (Persona Juridica)">Empresa o negocio (Persona Juridica)</option>
</select>
</div>
Enter users to the database:
if(isset($_POST['enviar'])){
Conexion :: abrir_conexion();
$validador = new ValidadorRegistro($_POST['tipo_de_usuario'], $_POST['nombre'], $_POST['email'], $_POST['clave1'], $_POST['clave2'], Conexion :: obtener_conexion());
if($validador->registro_valido()){
$usuario = new Usuario('', $validador-> obtener_tipo_de_usuario(), $validador-> obtener_nombre(), $validador-> obtener_email(), password_hash($validador-> obtener_clave(), PASSWORD_DEFAULT), '', '');
$usuario_insertado = RepositorioUsuario :: insertar_usuario(Conexion :: obtener_conexion(), $usuario);
if($usuario_insertado){
Redireccion :: redirigir(RUTA_REGISTRO_CORRECTO. '/' . $usuario -> obtener_nombre());
}
}
Conexion :: cerrar_conexion();
}
The problem is that every time I select some of the options of the select even entering everything else correctly (name, email, password1 and password2) and pressing the button that sends forms (is well programmed) nothing is sent to the database, and it also does not store the selected option in case of entering another one of the wrong data, in what part of the code am I wrong and how is the code to be corrected? Greetings and thanks in advance