How do I verify a php variable is empty? Null [duplicated]

0

This would be the code, I need to know how to verify if the variable is empty or not. (comments where I need the verification)

<?php include("coneccion.php");
/***************   SQL    ******************/
    @$buscar=$_POST["buscar"];
if($buscar){
    $ceda = $_POST["ceda"];
    $sql ="select * from cliente where cedula ='$ceda'";
    $result=mysqli_query($conn, $sql);
    $total=mysqli_num_rows($result);
    if($total){
        while($registro = mysqli_fetch_assoc($result)){
            $nomba = $registro["nombre"];
            $direca = $registro["direccion"];
            $tela = $registro["telefono"];
        }

    }
    else{       echo "<script>alert('No Existe el Cliente');</script>"; }
}
@$boton2=$_POST["boton2"];
if($boton2){
$ceda= $_POST["cedu"];
$nomba=$_POST["nombn"];
$direca=$_POST["direcn"];
$tela=$_POST["teln"];
/*Verificacion de cada campo, $ceda, nomba, direca, tela*/
$sql ="select * from cliente where cedula ='$ceda'";
$result=mysqli_query($conn, $sql);
$total=mysqli_num_rows($result);
    if($total){
    $sql= "update cliente set nombre='$nomba', direccion='$direca', telefono='$tela' where cedula='$ceda'";
    mysqli_query($conn, $sql);
    $nomb="";
    $direc="";
    $tel="";
    echo "<script>alert('Cliente Actualizado');</script>";
    }
    else{
    echo "<script>alert('Error en la Base de Datos');</script>";

    }
}

/***************formulario******************/

echo"<form action=index.php#services method=post>";
echo"<table id='table2'><tr>";
    echo"<th>Cedula del Cliete a Actuaizar</th>";
    echo"<td><input type=text name=ceda size=10 value=\" ".@$ceda." \"></td>";
    echo"<td><input type=submit name=buscar value=Buscar>";
    echo"</td>";
echo"</table>";
echo"</form>";

echo"<form action=index.php#services method=post>";
echo"<table id='table2'><tr>";
    echo"<th colspan=2><br>Datos Actuales</th>";
    echo"<th colspan=2><br>Datos Nuevos</th></tr>";
    echo"<th>Nombre</th>";
    echo"<td><input type=text name=nomb size=20 value=\" ".@$nomba." \"></td><td>/*if nombn es nulo...*/<input type=text name=nombn size=20 ></td>";
    echo"</tr><tr>";
    echo"<th>Direccion</th>";
    echo"<td><input type=text name=direc size=25 value=\" ".@$direca." \"></td><td>/*if direcn es nulo...*/<input type=text name=direcn size=25 ></td>";
    echo"</tr><tr>";
    echo"<th>Telefono</th>";
    echo"<td><input type=text name=tel size=15 value=\" ".@$tela." \"></td><td>/*if teln es nulo...*/<input type=text name=teln size=15 ></td>";
    echo"</tr><tr><td colspan=2>";
    echo"<input type=submit name=boton2 value=Actualizar Cliente>";
        echo"<input type=hidden name=cedu size=10 value=".@$ceda.">";
    echo"</tr>";
echo"</table>";
echo"</form>";
?>
    
asked by inusui 30.10.2018 в 00:34
source

2 answers

0

It occurs to me that you can solve it in the front of your application using javascript or jquery.

If you use javascript you can use the following sentence:

var valor = document.getElementById("id de tu input").value;
if(valor !== '')
{  //ejecuto algo CUANDO ES NO VACIO }
else
{  //ejecuto otra acción CUANDO ES VACIO }

Also, you can use document.getElementByName("name de tu input") if you assigned a name to the input.

Another way, by javascript can be accessing the input as

var valor = $("#id de tu input").val()
if(valor !== '')
{  //ejecuto algo cuando no es vacio }
else
{  //ejecuto otra acción CUANDO ES VACIO }

You can also use the .length property as follows:

var tamanio = $("#id de tu input").val().length()
if(tamanio === 0) { //el input es vacio }

I hope it's useful for you. Greetings

    
answered by 30.10.2018 / 00:51
source
1

Validations of data entered can be done in 2 parts:

  • On the client's side
  • On the server side

On the client side: means that in your html you have placed a code with javascript that validates the entered data, in this case, for example, if the field is empty, it does not allow the form. Although it is a widely used option because it prevents the server from processing incorrect requests by optimizing CPU usage, it is not a reliable option because javascript could be deactivated in the browser causing such validations not to apply, or someone could even launch a request with another tool to skip such controls.

On the server side: It means that in the server technology used (for your PHP case), the request is processed and validated if the incoming data meet the necessary conditions, such as If the field is not empty, this is the safest option, since regardless of where the request comes from or who generates it, the controls could not be skipped, the disadvantage is that the server would always have to process the request even if it is valid or not, which implies a high consumption of server resources.

And then? What to do? It is best to implement the 2 options, it may take a little more time but on the one hand in normal situation will prevent the server from processing invalid requests, and in an abnormal situation would prevent possible attacks on your site or web application.

    
answered by 30.10.2018 в 00:56