I do not get the exact value of a session variable in php

1

I am working on an online course system (class project) and I need to make a form where a user adds the necessary lessons for each course, but at the moment of clicking the aggLeccion button, he sends me data with the variable of another record. I do not know if it's because I show the information by means of a while shella and grabs the last value.

here the codes

<?php
 include("conexion.php");
  $usuario2 = $_SESSION['usuario'] ;
			$sql2 = "SELECT * FROM CURSOS WHERE USUARIO = '$usuario2'";
			$result2 = $conn->query($sql2);
		//verificar si existen registros de la busqueda
			if($result2->num_rows > 0){
				while($row = $result2->fetch_assoc()){
				$NOMBRE_C = $row['NOMBRE_C'];
		$DURACION = $row['DURACION'];
		$CATEGORIA = $row['CATEGORIA'];
		$REQUISITOS = $row['REQUISITOS'];
		$DESCRIPCION = $row['DESCRIPCION'];
		$TIPO_CONTENIDO = $row['TIPO_CONTENIDO'];
		$INSTRUCTOR = $row['INSTRUCTOR'];
		$CORREO_INSTRUCTOR = $row['CORREO_INSTRUCTOR'];
		$PRECIO = $row['PRECIO'];
		$NO_LECCIONES = $row['NO_LECCIONES'];
    
		echo"
  <div class='col-md-4 col-sm-4 col-xs-12 profile_details'>
    <div class='well profile_view'>
        <div class='col-sm-12'>
              <h4 class='brief'><i>".$NOMBRE_C."</i></h4>
              <div class='left col-xs-7'>
                  <h2><strong>Incluye: </strong> ".$DURACION." / ".$NO_LECCIONES."</h2>
                  <p> <strong>Descripcion:".$DESCRIPCION." </strong></p>
                      <ul class='list-unstyled'>
                          <li><i class='fa fa-building'></i>Categoria:".$CATEGORIA." </li>
                          <li><i class='fa fa-phone'></i>Contenido en:".$TIPO_CONTENIDO." </li>
                      </ul>
              </div>

              <div class='right col-xs-5 text-center'>
              <img src='images/img.jpg' alt='' class='img-circle img-responsive'>
              </div>
              </div>


          <div class='col-xs-12 bottom text-center'>             
              <div class='col-xs-12 col-sm-12 emphasis'>
                <a href='aggLecciones.php?msj='>  <button type='button' class='btn btn-success btn-xs'> <i class='fa fa-comments-o'></i> Agg Leciones
                  </button></a>                 
                <a href='aggAlumnos.php?msj='> <button type='button' class='btn btn-primary btn-xs'>
                     <i class='fa fa-user'> </i> Agg Alumnos
                  </button></a>
              </div>
          </div>
    </div>
  </div>";					
}
}

$_SESSION['nombre_c'] = $NOMBRE_C;
$_SESSION['duracion'] = $DURACION;
$_SESSION['categoria'] = $CATEGORIA;

?>

    <div class="row">
              <div class="col-md-12 col-sm-12 col-xs-12">
                       <div class="x_panel">
              <div class="x_title">
                <h2>Agrega Leciones <small>curso de    <?php echo $_SESSION['nombre_c'] ?>   </small></h2>
                
                <div class="clearfix"></div>
              </div>
              <div class="x_content">
                <br />

                <h4>Agrega lecciones a tu curso</h4>
                <p class="font-gray-dark">
                  Con este formulario podras agregar todas las llecciones que tu cursos lleve no tendras un limite pero si deberas llevar un orden al agregarlas.  
                </p>
                <form class="form-horizontal form-label-left"  action="lecciones.php" method="POST">
                  <div class="form-group">
                    <label class="control-label col-md-3" for="first-name">Tema
                    </label>
                    <div class="col-md-7">
                      <input name="tema" type="text" id="first-name2" required="required" class="form-control col-md-7 col-xs-12">
                    </div>
                  </div>
                  <div class="form-group">
                    <label class="control-label col-md-3" for="last-name">Descripcion 
                    </label>
                    <div class="col-md-7">
                     
                       <textarea name="des"  maxlength="80"  type="text" id="last-name" name="last-name" required="required" class="form-control col-md-7 col-xs-12" name="comentarios" rows="2" cols="30"></textarea>
                    </div>
                  </div>
             

                <h4>Link del contenido</h4>
                <p class="font-gray-dark">
                  Ingresa el link donde esta el contenido de la leccion ya sea un PDF,video, etc.
                </p>
                <div class="col-md-8 center-margin">
                
                    <div class="form-group">
                      <label>Link del video</label>
                      <input name="link_video" type="text" class="form-control" placeholder="Enter email">
                    </div>
                 
                 <div class="form-group">
                      <label>Link del recurso</label>
                      <input name="linl_recurso" type="text" class="form-control" placeholder="Enter email">
                    </div>
                 

                 
                </div>

                <h4>Sobre el curso</h4>
                <p class="font-gray-dark">
                  Agrega los datos que se te piden a continuacion <strong>Son obligatorios</strong>
                </p>
               
                  <div class="form-group form-inline ">
                    <label for="ex3">Id del curso al que pertecene</label>
                    <input name="id_curso" type="text" id="ex3" class="form-control" placeholder=" ">
               
                
                  </div>
               
                
                  <center>
                  <button type="submit" class="btn btn-default">Agregar Leccion</button>
                  </center>
                  <?php

$mensaje = $_GET['msj'];
if ($mensaje == "si"){
    header('Location:aggLecciones.php?msj=') ;
} 
if($mensaje == "no"){
    echo "leccione incorrecta";
}
if ($mensaje == "ya"){
    
    
    echo "La leccion ya esta agregagda, intenta con otra";

}
?>
                </form>
              </div>
            </div>
              </div>
            </div>

in the first lines shows how I call the session variable to show me the data

    
asked by Leo Tutorials 31.07.2018 в 04:05
source

1 answer

0

I would use Jquery on the button and work with Ajax.

But good according to your logic is true.

 //asignas la sesión fuera del While por lo que   
 // $NOMBRE_C vale lo que contiene en el último registro
 $_SESSION['nombre_c'] = $NOMBRE_C;

What I recommend is that you better send the encrypted name from the first source.

aggLecciones.php?ref=base64_encode(".$NOMBRE_C.")&msj=

And when you show it in the second source that I see is the file "aggLecciones.php" you decrypt it.

<h2>Agrega Leciones <small>curso de <?php echo base64_decode($_GET['ref']); ?> </small></h2>

And once decrypted you can put it in session if you are going to continue using that session in your application.

$_SESSION['nombre_c'] = base64_decode($_GET['ref']);

You can also send only the id of the table and then in aggLecciones.php you get the name or other information you need by making the query with that value.

I hope I have helped you.
Greetings.

    
answered by 31.07.2018 / 06:09
source