Place field that can be left blank or not

0

I have a couple of fields in mysql that I want them to be blank but I miss the error that I have to enter them and the problem is that they will not always be blank

this is the insert of the data

$Nombre = mysqli_real_escape_string($con,(strip_tags($_POST["Nombre"],ENT_QUOTES)));
            $equipo = mysqli_real_escape_string($con,(strip_tags($_POST["equipo"],ENT_QUOTES)));
            $ciudad = mysqli_real_escape_string($con,(strip_tags($_POST["ciudad"],ENT_QUOTES)));
            $departamento = mysqli_real_escape_string($con,(strip_tags($_POST["departamento"],ENT_QUOTES)));
            $Cargo = mysqli_real_escape_string($con,(strip_tags($_POST["Cargo"],ENT_QUOTES)));
        $cantidad = mysqli_real_escape_string($con,(strip_tags($_POST["cantidad"],ENT_QUOTES)));
        $justificacion = mysqli_real_escape_string($con,(strip_tags($_POST["justificacion"],ENT_QUOTES)));
        $Cargo = mysqli_real_escape_string($con,(strip_tags($_POST["Cargo"],ENT_QUOTES)));
            $agregado=date("Y-m-d H:i:s");
            $entregado=mysqli_real_escape_string($con,(strip_tags($_POST["entregado"],ENT_QUOTES)));
             $sql = "SELECT * FROM funcionarios WHERE Nombre = '" . $Nombre . "' OR Nombre = '" . $Nombre . "';";
            $query_check_Nombre = mysqli_query($con,$sql);


                $sql = "INSERT INTO funcionarios (Nombre, Cargo, equipo, ciudad, departamento, cantidad, justificacion, agregado, entregado)
                        VALUES('".$Nombre."','".$Cargo."','" .$equipo. "','" .$ciudad. "','" .$departamento. "','" .$cantidad. "','" .$justificacion. "', '" .$agregado . "', '" .$entregado ."' );";
                $query_new_prestamo_insert = mysqli_query($con,$sql);


                // if user has been added successfully
                if ($query_new_prestamo_insert) {
                    $messages[] = "La cuenta ha sido creada con éxito.";
                } else {
                    $errors[] = "Lo sentimos , el registro falló. Por favor, regrese y vuelva a intentarlo.";
                }
            }

these are the fields in the modal where I add them

<div class="form-group">
				<label for="Nombre" class="col-sm-3 control-label">Nombres</label>
				<div class="col-sm-8">
				  <input type="text" class="form-control" id="Nombre" name="Nombre" placeholder="Nombre" required>
				</div>
			  </div>
			  <div class="form-group">
				<label for="equipo" class="col-sm-3 control-label">Equipo</label>
				<div class="col-sm-8">
				  <input type="text" class="form-control" id="equipo" name="equipo" placeholder="Equipo" required>
				</div>
			  </div>
                 <div class="form-group">
				<label for="cantidad" class="col-sm-3 control-label">Cantidad</label>
				<div class="col-sm-8">
				  <input type="text" class="form-control" id="cantidad" name="cantidad" placeholder="cantidad" required>
				</div>
			  </div>
                 <div class="form-group">
				<label for="justificacion" class="col-sm-3 control-label">Justificacion</label>
				<div class="col-sm-8">
				  <input type="text" class="form-control" id="justificacion" name="justificacion" placeholder="justificacion">
				</div>
			  </div>
                <div class="form-group">
				<label for="departamento" class="col-sm-3 control-label">Departamento</label>
				<div class="col-sm-8">
				  <input type="text" class="form-control" id="departamento" name="departamento" placeholder="Departamento" required>
				</div>
			  </div>
			  <div class="form-group">
				<label for="Cargo" class="col-sm-3 control-label">Cargo</label>
				<div class="col-sm-8">
				  <input type="text" class="form-control" id="Cargo" name="Cargo" placeholder="Cargo" required>
				</div>
			  </div>
                <div class="form-group">
				<label for="ciudad" class="col-sm-3 control-label">Ciudad</label>
				<div class="col-sm-8">
				  <input type="text" class="form-control" id="ciudad" name="ciudad" placeholder="ciudad" required>
				</div>
			  </div>
			  <div class="form-group">
				<label for="agregado" class="col-sm-3 control-label">Agregado</label>
				<div class="col-sm-8">
				  <input type="date" class="form-control" id="agregado" name="agregado" placeholder="Fecha de agregada la solicitud">
				</div>
			  </div>
			   <div class="form-group">
				<label for="entregado" class="col-sm-3 control-label">Entregado</label>
				<div class="col-sm-8">
				  <input type="date" class="form-control" id="entregado" name="entregado" placeholder="Fecha de entrega del equipo">
				</div>
			  </div>

and the fields that I want that I want to be free

I removed the required from the modal but in the insert of the data it gives me this error $errors[] = "Lo sentimos , el registro falló. Por favor, regrese y vuelva a intentarlo.";

    
asked by Kevin Salazar 16.11.2017 в 16:02
source

2 answers

1

I do not know what fields you want to be blank but if you mean to let them pass if they are empty some I put the code as I do.

if(isset($datos["calle"]))
            $calle = $this->conexion -> real_escape_string(strip_tags(stripslashes(trim($datos["calle"]))));
        else
            $calle = NULL;

here is

if(isset($_POST["justificacion"]))
$justificacion = mysqli_real_escape_string($con,(strip_tags($_POST["justificacion"],ENT_QUOTES)));
else
$justificacion =NULL;
    
answered by 16.11.2017 / 16:25
source
0

Review the following:

1-The sql fields must be able to allow NULL. 2-Check with an isset the variables that receive POST. So that if they are not instantiated, make them NULL.

    
answered by 16.11.2017 в 16:26