The combobox does not send the selected code

1

Good evening I know they did not miss me, but I'm still stuck like a truck in the sand.

Accomplishments this week: I've already 'awakened' and I can pass not only one but my three variables. I already prepared all the 'pretty' query as Cedano likes, then I hang it up. But I still have a little problem. COMBOBOX does not work does not send the idgr that I choose (see) I used the same one I use in forms to complete data and that works great for INSERT, here it does not work for UPDATE magic, sorcery , simple ignorance of mine? Go code

<?php
session_start();
ob_start();
// Include config file
require_once '990conn.php';
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sin título</title>
<link href="css/sbg.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
require_once '990conn.php';
$idusr=$_GET['idusr'];
$nomb=$_GET['nomb'];    
$grup=$_GET['grup'];
echo "<p>Nº usuar: <strong>$idusr</strong>";
echo "<p>Nombre : <strong>$nomb</strong>";
echo "<p>Grupo act: <strong>$grup</strong>";
?>  
<table width="280" border="0" cellspacing="0" cellpadding="0"><tbody>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" target="_self" class="row" title="f098">
<!-- Selecciono el grupo dinamicamente de la tabla $t98g = 98gr que solo contiene dos columnas idgr (numérica) y gr (nombre del grupo) ver foto-->
<tr><td>
<select name="gru" size="12" required id="gru" form="f098" title="gru" >
    <option value=""></option>
<?php $sql098b="select idgr, gr from $t98g"; 
$r098b=mysqli_query($mysqli, $sql098b); 
while($rw098b=mysqli_fetch_array($r098b))
{ 
?>
    <option value="<?php echo $rw098b['idgr']; ?>"><?php echo $rw098b['gr']; ?></option>
<?php 
    $gru=$rw098b['idgr'];
} 
?>
</select></td></tr>
<tr><input name="bt-098" type="submit" class="btn-primary" id="bt-098" value="CAMBIAR GRUPO" /> </tr>
<tr><input name="idusr" value="<?php echo $idusr; ?>"></tr><!--EL idusr sigue firme-->
<!--<tr><input value="<?php //echo $gru; ?>"/></tr><!--AQUI ESTA MAL, toma por defecto el último de la lista 8=licencia-->
</form></tbody></table>
<?php
    if($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_POST['bt-098']))
{
    $sql098 = "UPDATE 98usr SET 98grupo=$gru WHERE 98idusr=$idusr";
        if(mysqli_query($mysqli, $sql098)){
    echo "El grupo fue cambiado."; // me dice siempre que fue cambiado
} else{
    echo "ERROR: No es posible ejecutar $sql098. " . $mysqli->error;
}
}
}
// Close connection
mysqli_close($mysqli);
?>
</body>
</html>

Go photo

    
asked by Silvia Gaviota Garcia 08.11.2017 в 00:55
source

1 answer

1

One tip is to not mix the object-oriented styles with the style by procedures in your statements. If you look at the PHP manual the connection changes from one style to another.

Second point in your statement update I do not see that you are passing the values of your form obtained by POST in the checks, but you get it from your select .

A possible example (Object oriented style)

connection

$mysqli = new mysqli("servidor", "usuario", "contraseña", "base de datos");

/* comprobar la conexión */
if ($mysqli->connect_errno) {
    printf("Falló la conexión: %s\n", $mysqli->connect_error);
    exit();
}

form

<?php
  require_once '990conn.php';
  $idusr=$_GET['idusr'];
  $nomb=$_GET['nomb'];    
  $grup=$_GET['grup'];
  //vienen de página anterior.
  echo"
    <p>Nº usuar: <strong>$idusr</strong></p>
    <p>Nombre: <strong>$nomb</strong></p>
    <p>Grupo act: <strong>$grup</strong></p>
  ";
?>  

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" class="row" title="f098">
    <select name="gru" size="11" id="gru" form="f098" required >
        <option value="0"></option>
        <?php
            //Sentencia select
            $sql098b="select idgr, gr from $t98g"; 
            $r098b= $mysqli->query($sql098b);
            //Salida datos
            while($row = $r098b->fetch_assoc()) { 
        ?>
                <option value="<?php echo $row['idgr']; ?>"><?php echo $row['gr']; ?></option>
        <?php     
            } 
            //Cerrar sentencia.
            $r098b->close();
        ?>
    </select>   
    <input type="hidden" name="idusr" value="<?php echo $idusr; ?>" /><!--EL idusr sigue firme-->
    <input name="bt-098" type="submit" class="btn-primary" id="bt-098" value="CAMBIAR GRUPO" />
</form>

<?php
    //Reseteo
    $gru_frm = $idusr_frm = NULL;

    //Si esta definido el formulario y no es NULL.
    if (isset($_POST['bt-098'])) {
        //Comprobación si el select esta vacío.
        if (empty($_POST['gru'][0])) {
            echo "Selecciona el grupo dinamicamente.";
        } elseif (empty($_POST['idusr'])) {
            echo "Hubbo un error con el 'ID'.";
        } else {
            //Obtienes datos formulario
            $gru_frm = $_POST['gru'];
            //Como tambien tienes un $idusr que viene de otra página, e puesto idusr_frm para no tener conflictos y saber que viene desde el formulario.
            $idusr_frm = $_POST['idusr'];
        }
        //Verdadero datos formulario.
        if ($gru_frm && $idusr_frm) {
            //Sentencia update
            $sql098 = $mysqli->query("UPDATE 98usr SET 98grupo=$gru_frm WHERE 98idusr=$idusr_frm");
            //Comprobamos si se ejecuto.
            if($sql098===true) {
                echo "El grupo fue cambiado."; // me dice siempre que fue cambiado
            } else{
                echo "ERROR: No es posible ejecutar $sql098. " . $mysqli->error;
            }
            //Cerrar sentencia.
            $sql098->close();
        }           
    }   
?>
  

Note: I advise you to use sentencia preparadas or PDO to get more security in your application, here is an interesting post ( How to avoid SQL injection in PHP? ).

    
answered by 08.11.2017 / 03:30
source