Foros del Web » Programando para Internet » PHP »

Guardar en MYSQL una variable al cargar la pagina?

Estas en el tema de Guardar en MYSQL una variable al cargar la pagina? en el foro de PHP en Foros del Web. Buenas noches Programadores. Les comento, estoy haciendo un sistema de autogestión online, basado para una institución; es decir asistencias, calificaciones, subida de material, etc. Todo ...
  #1 (permalink)  
Antiguo 13/08/2015, 20:01
 
Fecha de Ingreso: agosto-2015
Ubicación: Córdoba
Mensajes: 3
Antigüedad: 8 años, 8 meses
Puntos: 0
Pregunta Guardar en MYSQL una variable al cargar la pagina?

Buenas noches Programadores.
Les comento, estoy haciendo un sistema de autogestión online, basado para una institución; es decir asistencias, calificaciones, subida de material, etc. Todo el sistema lo estoy desarrollando con lenguajes PHP, AJAX, JS.
Mi duda es la siguiente. En la parte de calificaciones, los docentes mediante selectores buscan la carrera, el cuatrimestre, la materia, el turno (mañana-tarde-noche) y el año lectivo, luego se carga la planilla mostrando una tabla con los alumnos cargados en esa clase; dicha tabla es editable es decir el docente selecciona el campo que se puede editar y carga la calificación correspondiente y automaticamente mediante AJAX se guarda en la BD. Como son varios campos de un solo alumno EJEMPLO:

ALUMNOS | Parcial1 | Parcial2 | Parcial3 | TP1 | TP2 | TP3 | Promedio
Jose 7 5 Ausente 5 8 6.25


El promedio lo saco mediante una operación con PHP y lo muestro con una variable.
Lo que yo necesito es guardar ese promedio en un campo de la BD en el mismo momento en que se ejecuta la operación para calcularlo, sin tener que tener un boton para que envie ese dato, es decir al momento de ejecutarse la planilla y muestre el resultado del calculo.
  #2 (permalink)  
Antiguo 13/08/2015, 20:25
Avatar de xfxstudios  
Fecha de Ingreso: junio-2015
Ubicación: Valencia - Venezuela
Mensajes: 2.448
Antigüedad: 8 años, 10 meses
Puntos: 263
Respuesta: Guardar en MYSQL una variable al cargar la pagina?

podria realizar un onChange en alguno de los campos para que este ejecute un codigo ajax que realice el guardado del promedio, pero sin ver el codigo no podria decirte mucho
__________________
[email protected]
HITCEL
  #3 (permalink)  
Antiguo 13/08/2015, 20:49
 
Fecha de Ingreso: agosto-2015
Ubicación: Córdoba
Mensajes: 3
Antigüedad: 8 años, 8 meses
Puntos: 0
Respuesta: Guardar en MYSQL una variable al cargar la pagina?

Muchas gracias por responder. Luego de los selectores ya antes mencionados el docente presiona un boton CARGAR PLANILLA y mediante AJAX se ejecuta y muestra el siguiente codigo donde alli calcula el promedio.

Código HTML:
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "evan.tesis";
	
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser);
	
//Select Database
mysql_select_db($dbname) or die(mysql_error());
	
// Retrieve data from Query String
$carreras = $_GET['carreras'];
$materias = $_GET['materias'];
$turno = $_GET['turno'];
$anio = $_GET['anio'];
	
// Escape User Input to help prevent SQL Injection
$carreras = mysql_real_escape_string($carreras);
$materias = mysql_real_escape_string($materias);
$turno = mysql_real_escape_string($turno);
$anio = mysql_real_escape_string($anio);


require_once 'inc/funciones.php';
$link = ConectarBD();  
$query = mysql_query("SELECT * FROM interactivo_evan WHERE carreraAlumno = '$carreras' AND materiasInscripcion = '$materias' AND turnoAlumno = '$turno' AND anioInscripcion = '$anio'"); 

while ($row = mysql_fetch_row($query)){ ?>
	
   <tr>
   <td><?php echo $row[2]; ?> <?php echo $row[3]; ?></td>
   <td><?php echo $row[1]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'1P','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[27]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'1Pr','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[28]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'2P','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[29]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'2Pr','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[30]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'3P','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[31]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'1TP','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[32]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'2TP','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[33]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'3TP','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[34]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'4TP','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[35]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'5TP','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[36]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'6TP','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[37]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'7TP','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[38]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'8TP','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[39]; ?></td>
  <?php if ($row[11] == "A") {$row[11] = NULL;}
if ($row[12] == "A") {$row[12] = NULL;}
if ($row[13] == "A") {$row[13] = NULL;}
if ($row[14] == "A") {$row[14] = NULL;}
if ($row[15] == "A") {$row[15] = NULL;}
if ($row[16] == "A") {$row[16] = NULL;}
if ($row[17] == "A") {$row[17] = NULL;}
if ($row[18] == "A") {$row[18] = NULL;}

if (empty($row[32])) {$row[32] = NULL;} else if ($row[32] == "A") {$row[32] = NULL;}
if (empty($row[33])) {$row[33] = NULL;} else if ($row[33] == "A") {$row[33] = NULL;}
if (empty($row[34])) {$row[34] = NULL;} else if ($row[34] == "A") {$row[34] = NULL;}
if (empty($row[35])) {$row[35] = NULL;} else if ($row[35] == "A") {$row[35] = NULL;}
if (empty($row[36])) {$row[36] = NULL;} else if ($row[36] == "A") {$row[36] = NULL;}
if (empty($row[37])) {$row[37] = NULL;} else if ($row[37] == "A") {$row[37] = NULL;}
if (empty($row[38])) {$row[38] = NULL;}	else if ($row[38] == "A") {$row[38] = NULL;}
if (empty($row[39])) {$row[39] = NULL;} else if ($row[39] == "A") {$row[39] = NULL;}

$Conteo =array($row[32], $row[33], $row[34], $row[35], $row[36], $row[37], $row[38], $row[39]);



foreach($Conteo as $index=>$value) {
    if($value === null) unset($Conteo[$index]);
}
$Cant = count($Conteo);	

$SumaNota = $row[32] + $row[33] + $row[34] + $row[35] + $row[36] + $row[37] + $row[38] + $row[39];
if ($SumaNota > 0) {
$Promedio = ($row[32] + $row[33] + $row[34] + $row[35] + $row[36] + $row[37] + $row[38] + $row[39]) / $Cant;} else {$Promedio = NULL;
}

?>
   <td><?php echo $Promedio;?></td>
   
   </tr>
   </tbody>

<?php
} 
	?>
	
  #4 (permalink)  
Antiguo 13/08/2015, 21:05
Avatar de chronos682  
Fecha de Ingreso: febrero-2004
Ubicación: Tunja - Boyacá - Colombia
Mensajes: 627
Antigüedad: 20 años, 2 meses
Puntos: 69
Respuesta: Guardar en MYSQL una variable al cargar la pagina?

Puedes hacer el insert luego de obtener el promedio:

Cita:
Iniciado por diegorpm4 Ver Mensaje
Muchas gracias por responder. Luego de los selectores ya antes mencionados el docente presiona un boton CARGAR PLANILLA y mediante AJAX se ejecuta y muestra el siguiente codigo donde alli calcula el promedio.

Código HTML:
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "evan.tesis";
	
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser);
	
//Select Database
mysql_select_db($dbname) or die(mysql_error());
	
// Retrieve data from Query String
$carreras = $_GET['carreras'];
$materias = $_GET['materias'];
$turno = $_GET['turno'];
$anio = $_GET['anio'];
	
// Escape User Input to help prevent SQL Injection
$carreras = mysql_real_escape_string($carreras);
$materias = mysql_real_escape_string($materias);
$turno = mysql_real_escape_string($turno);
$anio = mysql_real_escape_string($anio);


require_once 'inc/funciones.php';
$link = ConectarBD();  
$query = mysql_query("SELECT * FROM interactivo_evan WHERE carreraAlumno = '$carreras' AND materiasInscripcion = '$materias' AND turnoAlumno = '$turno' AND anioInscripcion = '$anio'"); 

while ($row = mysql_fetch_row($query)){ ?>
	
   <tr>
   <td><?php echo $row[2]; ?> <?php echo $row[3]; ?></td>
   <td><?php echo $row[1]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'1P','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[27]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'1Pr','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[28]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'2P','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[29]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'2Pr','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[30]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'3P','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[31]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'1TP','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[32]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'2TP','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[33]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'3TP','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[34]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'4TP','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[35]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'5TP','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[36]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'6TP','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[37]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'7TP','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[38]; ?></td>
   <td contenteditable='true' onBlur="saveToDatabase(this,'8TP','<?php echo $row[0]; ?>')" onClick='showEdit(this);'><?php echo $row[39]; ?></td>
  <?php if ($row[11] == "A") {$row[11] = NULL;}
if ($row[12] == "A") {$row[12] = NULL;}
if ($row[13] == "A") {$row[13] = NULL;}
if ($row[14] == "A") {$row[14] = NULL;}
if ($row[15] == "A") {$row[15] = NULL;}
if ($row[16] == "A") {$row[16] = NULL;}
if ($row[17] == "A") {$row[17] = NULL;}
if ($row[18] == "A") {$row[18] = NULL;}

if (empty($row[32])) {$row[32] = NULL;} else if ($row[32] == "A") {$row[32] = NULL;}
if (empty($row[33])) {$row[33] = NULL;} else if ($row[33] == "A") {$row[33] = NULL;}
if (empty($row[34])) {$row[34] = NULL;} else if ($row[34] == "A") {$row[34] = NULL;}
if (empty($row[35])) {$row[35] = NULL;} else if ($row[35] == "A") {$row[35] = NULL;}
if (empty($row[36])) {$row[36] = NULL;} else if ($row[36] == "A") {$row[36] = NULL;}
if (empty($row[37])) {$row[37] = NULL;} else if ($row[37] == "A") {$row[37] = NULL;}
if (empty($row[38])) {$row[38] = NULL;}	else if ($row[38] == "A") {$row[38] = NULL;}
if (empty($row[39])) {$row[39] = NULL;} else if ($row[39] == "A") {$row[39] = NULL;}

$Conteo =array($row[32], $row[33], $row[34], $row[35], $row[36], $row[37], $row[38], $row[39]);



foreach($Conteo as $index=>$value) {
    if($value === null) unset($Conteo[$index]);
}
$Cant = count($Conteo);	

$SumaNota = $row[32] + $row[33] + $row[34] + $row[35] + $row[36] + $row[37] + $row[38] + $row[39];
if ($SumaNota > 0) {
$Promedio = ($row[32] + $row[33] + $row[34] + $row[35] + $row[36] + $row[37] + $row[38] + $row[39]) / $Cant;} else {$Promedio = NULL;
}
$query = mysql_query("INSERT INTO tabla ('promedio') VALUES ('$Promedio')");
?>
   <td><?php echo $Promedio;?></td>
   
   </tr>
   </tbody>

<?php
} 
	?>
	
Cambias tabla por el nombre dela tabla y promedio por el nombre del campo.
__________________
Si te gustó la respuesta dale +1

HERNÁN G. SIABATO M.
[email protected]

Etiquetas: mysql, select, tabla, variable
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 21:05.