Php radio button with if you do not choose

0

I have a form created in a page where there are two radio buttons for sex, in the following I try to do an if to know if in the bd I keep M or F, but it is as if it stayed in the value that it had before to make the update

while($fila=mysqli_fetch_array($resultados, MYSQLI_ASSOC)){     
            echo "<div align='center'>";    
            echo "<form action='cliente_actualizar_datos.php' method='get'>";

            echo "<label for='identificacion'>Identificación </label> &nbsp;";      
            echo "<input type='text' name='identificacion' value='". $fila['identificacion'] . "'><br>";

            echo "<label for='nombre'>Nombre </label> &nbsp;";      
            echo "<input type='text' name='nombre' value='". $fila['nombre'] . "'><br>";

            echo "<label for='direccion'>Dirección </label> &nbsp;";        
            echo "<input type='text' name='direccion' value='". $fila['direccion'] . "'><br>";

            echo "<label for='telefono'>Teléfono </label> &nbsp;";      
            echo "<input type='text' name='telefono' value='". $fila['telefono'] . "'><br>";

            echo "<label for='correo'>Correo </label> &nbsp;";  
            echo "<input type='text' name='correo' value='". $fila['correo'] . "'><br>";

            /*echo "<label for='sexo'>Sexo </label> &nbsp;";    
            echo "<input type='text' name='sexo' value='". $fila['sexo'] . "'><br>"; */

            echo "<label id='label1' name='sexoLabel'>Sexo </label> &nbsp"; 
            if($fila['sexo'] == 'M'){
                echo "<input type='radio' name='sexo' value='M' class='textoBlanco' checked> Masculino &nbsp";
                echo "<input type='radio' name='sexo' value='F' class='textoBlanco'> Femenino<br><br>";
            }else{
                echo "<input type='radio' name='sexo' value='M' class='textoBlanco' > Masculino &nbsp";
                echo "<input type='radio' name='sexo' value='F' class='textoBlanco' checked> Femenino<br><br>";
            }                               

            echo "<label for='estado_civil'>Estado Civil </label> &nbsp;";  
            echo "<input type='text' name='estado_civil' value='". $fila['estado_civil'] . "'><br>";

            echo "<div class='button'>";
            echo "<button type='submit' name='submit' value='actualizar'>Actualizar</button>";  
             //<button type="submit" name="submit">Guardar</button>     
            echo "</div>";
            echo "</form>"; 
            echo "</div>";  

And the one who works with the data:

$identificacion=$_GET["identificacion"];
    $nombre=$_GET["nombre"];
    $direccion=$_GET["direccion"];
    $telefono=$_GET["telefono"];
    $correo=$_GET["correo"];
    //$sexo=$_GET["sexo"];
    $Masculino = (isset($_GET["Masculino"]))?true:false;                                
        if($Masculino == true){
            $sexo = "M";
        } 
        else{
            $sexo = "F";
        }   
    $estado_civil=$_GET["estado_civil"];        

    $conexion=mysqli_connect($db_host,$db_usuario,$db_contra,$db_nombre);

    if(mysqli_connect_errno()){
        echo "Fallo al conectar con la base de datos";
        exit();
    }

    mysqli_select_db($conexion, $db_nombre) or die("No se encuentra la base de datos");
    mysqli_set_charset($conexion,"utf8");

    $consulta="UPDATE tbl_clientes SET nombre='$nombre', 
    direccion='$direccion', 
    telefono='$telefono', 
    correo='$correo', 
    sexo='$sexo', 
    estado_civil='$estado_civil'
    WHERE identificacion='$identificacion'";
    
asked by Jhon Hernández 10.06.2018 в 01:39
source

1 answer

0

All this is unnecessary:

$Masculino = (isset($_GET["Masculino"]))?true:false;                                
        if($Masculino == true){
            $sexo = "M";
        } 
        else{
            $sexo = "F";
        } 

First, in the GET there will not be any value with the key Masculino , since what the key does when sending the form is the value of the name of each element within the form.

If you use this conditional:

    if($fila['sexo'] == 'M'){
        echo "<input type='radio' name='sexo' value='M' class='textoBlanco' checked> Masculino &nbsp";
        echo "<input type='radio' name='sexo' value='F' class='textoBlanco'> Femenino<br><br>";
    }else{
        echo "<input type='radio' name='sexo' value='M' class='textoBlanco' > Masculino &nbsp";
        echo "<input type='radio' name='sexo' value='F' class='textoBlanco' checked> Femenino<br><br>";
    }                               

You can determine the value checked simply like this:

$sexo=(empty($_GET["sexo"])) ? "" : $_GET["sexo"];

As you can see, GET is used in sexo , which is how the checkbox is identified by its name tag. The ternary operator evaluates with empty not only if it is set, but also if it is not empty. If it is, it assigns "" to $sexo and if not, it collects the value collected in the key sexo of GET .

Also, this line is:

mysqli_select_db($conexion, $db_nombre) or die("No se encuentra la base de datos");

Well, you've already selected the database here:

$conexion=mysqli_connect($db_host,$db_usuario,$db_contra,$db_nombre);

And also, you must finally execute the query:

if ($resultado = mysqli_query($conexion, $consulta)) {
    printf("Se actualizaron %d filas.\n", mysqli_num_rows($resultado));

    /* liberar el conjunto de resultados */
    mysqli_free_result($resultado);
}
  

NOTE ON SECURITY:

     

Your code is highly vulnerable to SQL injection attacks. Once   solved this problem, consider using queries prepared to give   code security.

Other observations

The way to use your checkboxes is precarious in the sense that no control is established to avoid for example that both are selected . If that happens you will have server-side problems. In this case, on the client side you should have a control (via Javascript) that allows you to select only one checkbox at a time. That control could also use the value of $fila['sexo'] to decide what value should be checked.

Moreover, if it is a value that will not change, you can put it as disabled , thus avoiding surprises.

    
answered by 10.06.2018 / 02:50
source