我需要做一個選擇到數據庫,並通過ID搜索。選擇作品,我有一個功能,顯示我所有的事件,但我需要一個按鈕,通過ID搜索事件。我如何做到這一點?如何使用where條件從PHP中進行SQL選擇?
編輯:問題已解決。
我使用了會話變量,它們是全局變量,您必須聲明它並在每個頁面中執行會話啓動,以便使用它。語法是$_SESSION['whatever'];
我在這裏發佈代碼。我創建了4個頁面(castigos.php和castigos2.php來顯示incidentes和generarJSON.php和generarJSON2.php來創建數組,首先我們用generarJSON.php獲取整個數組,然後我們在generarJSON2.php中過濾數組通過cod_alumno。
Castigos.php
<?php
session_start();
if($_SESSION['userid'] == TRUE)
\t {
include 'config.php'
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>TABLA JSON</title>
<link rel="stylesheet" type="text/css" href="css/estilos.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" ></script>
<link rel='stylesheet prefetch' href='http://netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css'>
\t
\t
</head>
<body>
<!-- Cabecera -->
<header>
<h1 style=" width:58%; margin: 0 auto; margin-top: 1%; margin-bottom: 1%;"><IMG SRC="css/logo.png" ALT="Logo del IES Emilio Jimeno" style="padding-right:2%;" >Registros de todos los alumnos</h1>
</header>
\t \t
\t \t
\t \t <form method="POST" action="castigos2.php" style="width: 40%; margin: 0 auto; margin-bottom:1%;">
\t \t Buscar por DNI de alumno: <input type="text" name="buscador">
\t \t <input type="submit" class="btn btn-info" value="Buscar">
\t \t <?php
\t \t
\t \t {
\t \t ?>
\t \t <input class="btn btn-info" type="button" value="Volver a calendario" onClick="location.href='index.php'" />
\t \t
\t \t
\t \t </form>
\t \t </br>
\t \t
<!-- Contenido -->
<section>
\t \t \t <table class="table table-hover" id="tablajson" style=" width: 98%; margin: 0 auto;">
\t \t \t <thead>
\t \t \t <th>Cod de castigo</th>
\t \t \t <th>Fecha</th> \t
\t \t \t <th>Curso</th>
\t \t \t <th>DNI - Alumno</th>
\t \t \t <th>Nombre - Alumno</th>
\t \t \t <th>Falta</th>
\t \t \t <th>Profesor Sancionador</th>
\t \t \t <th>Ubicación del castigo</th>
\t \t \t <th>Observaciones</th>
\t \t \t <th>Asistencia</th> \t
\t \t \t <th>Comportamiento</th>
\t \t \t <th>Tarea</th> \t \t \t
\t \t \t </thead>
\t \t \t <tbody></tbody>
\t \t \t </table>
\t \t \t <script type="text/javascript">
\t \t \t \t $(document).ready(function(){
\t \t \t \t var url="generarJSON.php";
\t \t \t \t $("#tablajson tbody").html("");
\t \t \t \t $.getJSON(url,function(castigos){
\t \t \t \t $.each(castigos, function(i,castigo){
\t \t \t \t var newRow =
\t \t \t \t "<tr>"
\t \t \t \t +"<td>"+castigo.cod_castigo+"</td>"
\t \t \t \t +"<td>"+castigo.fecha+"</td>"
\t \t \t \t +"<td>"+castigo.curso+"</td>"
\t \t \t \t +"<td>"+castigo.cod_alumno+"</td>"
\t \t \t \t +"<td>"+castigo.nombre+"</td>"
\t \t \t \t +"<td>"+castigo.falta+"</td>"
\t \t \t \t +"<td>"+castigo.nombre_usuario+"</td>"
\t \t \t \t +"<td>"+castigo.emplazamiento+"</td>"
\t \t \t \t +"<td>"+castigo.observaciones+"</td>"
\t \t \t \t +"<td>"+castigo.asistencia+"</td>"
\t \t \t \t +"<td>"+castigo.comportamiento+"</td>"
\t \t \t \t +"<td>"+castigo.tarea+"</td>"
\t \t \t \t +"</tr>";
\t \t \t \t $(newRow).appendTo("#tablajson tbody");
\t \t \t \t });
\t \t \t \t });
\t \t \t \t });
\t \t \t </script>
</section>
\t <?php
\t \t }
\t \t //mysql_free_result($buscar_alumnos)
\t ?>
<!-- Pie de pagina -->
</body>
</html>
<?php
} else {
header("Location: index.php");
}
?>
generarJSON.php
<?php
include 'config.php';
\t \t
$sql = "SELECT * FROM castigos ca JOIN alumnos a JOIN curso cu JOIN faltas fa JOIN usuario us JOIN emplazamiento_castigos emp JOIN tarea tar JOIN asistencia asi JOIN comportamiento comp ON ca.cod_alumno=a.cod_alumno AND a.cod_curso=cu.cod_curso AND ca.cod_falta=fa.cod_falta AND ca.cod_profesor=us.id AND ca.cod_emplazamiento=emp.cod_emplazamiento AND ca.cod_tarea=tar.cod_tarea AND ca.cod_asistencia=asi.cod_asistencia AND ca.cod_comportamiento=comp.cod_comportamiento ORDER BY ca.cod_castigo"; \t \t \t
mysqli_set_charset($conexion, "utf8");
if(!$result = mysqli_query($conexion, $sql)) die();
$castigos = array(); //creamos un array
while($row = mysqli_fetch_array($result))
{
$id=$row['cod_castigo'];
$curso=$row['curso'];
$falta=$row['falta'];
\t $alumno=$row['cod_alumno'];
\t $nomalumno=$row['nombre'];/* nuevo */
$prof=$row['nombre_usuario'];
\t $emp=$row['emplazamiento'];
$fecha=$row['fecha'];
$obs=$row['observaciones'];
$asi=$row['asistencia'];
\t $comp=$row['comportamiento'];
\t $tarea=$row['tarea'];
\t
$castigos[] = array('cod_castigo'=> $id, 'curso'=> $curso, 'falta'=> $falta, 'cod_alumno' => $alumno, 'nombre' => $nomalumno, 'nombre_usuario'=> $prof,
'emplazamiento' => $emp, 'fecha'=> $fecha,'observaciones'=> $obs, 'asistencia'=> $asi, 'comportamiento' =>$comp, 'tarea' => $tarea,);
\t
}
//desconectamos la base de datos
$close = mysqli_close($conexion)
or die("Ha sucedido un error inexperado en la desconexion de la base de datos");
//Creamos el JSON
$json_string = json_encode($castigos);
echo $json_string;
//$file = 'castigos.json';
//file_put_contents($file, $json_string)
?>
castigos2.php
<?php
session_start();
if($_SESSION['userid'] == TRUE)
\t {
include 'config.php'
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>TABLA JSON</title>
<link rel="stylesheet" type="text/css" href="css/estilos.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" ></script>
<link rel='stylesheet prefetch' href='http://netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css'>
\t
\t
</head>
<body>
<!-- Cabecera -->
<header>
<h1 style=" width:58%; margin: 0 auto; margin-top: 1%; margin-bottom: 1%;"><IMG SRC="css/logo.png" ALT="Logo del IES Emilio Jimeno" style="padding-right:2%;" >Registros de todos los alumnos</h1>
</header>
\t \t
\t \t <?php
\t \t \t \t \t if (isset($_POST['buscador'])) //Si llego un dato via el formulario lo grabamos en la Sesion
\t \t \t {
\t \t \t \t $_SESSION['comprobante'] = $_POST['buscador']; //Dni Grabado
\t \t \t }
\t \t \t
\t \t \t ?>
\t \t \t
\t \t
\t \t
\t \t <?php
\t \t
\t \t {
\t \t ?>
\t \t <div style="width:25%; margin: 0 auto;" >
\t \t <input class="btn btn-info" type="button" value="Volver al buscador" onClick="location.href='castigos.php'" />
\t \t <input class="btn btn-info" type="button" value="Volver a calendario" onClick="location.href='index_cal.php'" />
\t \t </div>
\t \t </br>
\t \t
<!-- Contenido -->
<section>
\t \t \t <table class="table table-hover" id="tablajson" style=" width: 98%; margin: 0 auto;">
\t \t \t <thead>
\t \t \t <th>Cod de castigo</th>
\t \t \t <th>Fecha</th> \t
\t \t \t <th>Curso</th>
\t \t \t <th>DNI - Alumno</th>
\t \t \t <th>Nombre - Alumno</th>
\t \t \t <th>Falta</th>
\t \t \t <th>Profesor Sancionador</th>
\t \t \t <th>Ubicación del castigo</th>
\t \t \t <th>Observaciones</th>
\t \t \t <th>Asistencia</th> \t
\t \t \t <th>Comportamiento</th>
\t \t \t <th>Tarea</th> \t \t \t
\t \t \t </thead>
\t \t \t <tbody></tbody>
\t \t \t </table>
\t \t \t <script type="text/javascript">
\t \t \t \t $(document).ready(function(){
\t \t \t \t var url="generarJSON2.php";
\t \t \t \t $("#tablajson tbody").html("");
\t \t \t \t $.getJSON(url,function(castigos){
\t \t \t \t $.each(castigos, function(i,castigo){
\t \t \t \t var newRow =
\t \t \t \t "<tr>"
\t \t \t \t +"<td>"+castigo.cod_castigo+"</td>"
\t \t \t \t +"<td>"+castigo.fecha+"</td>"
\t \t \t \t +"<td>"+castigo.curso+"</td>"
\t \t \t \t +"<td>"+castigo.cod_alumno+"</td>"
\t \t \t \t +"<td>"+castigo.nombre+"</td>"
\t \t \t \t +"<td>"+castigo.falta+"</td>"
\t \t \t \t +"<td>"+castigo.nombre_usuario+"</td>"
\t \t \t \t +"<td>"+castigo.emplazamiento+"</td>"
\t \t \t \t +"<td>"+castigo.observaciones+"</td>"
\t \t \t \t +"<td>"+castigo.asistencia+"</td>"
\t \t \t \t +"<td>"+castigo.comportamiento+"</td>"
\t \t \t \t +"<td>"+castigo.tarea+"</td>"
\t \t \t \t +"</tr>";
\t \t \t \t $(newRow).appendTo("#tablajson tbody");
\t \t \t \t });
\t \t \t \t });
\t \t \t \t });
\t \t \t </script>
</section>
\t <?php
\t \t }
\t \t //mysql_free_result($buscar_alumnos)
\t ?>
<!-- Pie de pagina -->
</body>
</html>
<?php
} else {
header("Location: index.php");
}
?>
generarJSON2.php
<?php
session_start();
include 'config.php';
\t \t
\t \t
$sql = "SELECT * FROM castigos ca JOIN alumnos a JOIN curso cu JOIN faltas fa JOIN usuario us JOIN emplazamiento_castigos emp JOIN tarea tar JOIN asistencia asi JOIN comportamiento comp ON ca.cod_alumno=a.cod_alumno AND a.cod_curso=cu.cod_curso AND ca.cod_falta=fa.cod_falta AND ca.cod_profesor=us.id AND ca.cod_emplazamiento=emp.cod_emplazamiento AND ca.cod_tarea=tar.cod_tarea AND ca.cod_asistencia=asi.cod_asistencia AND ca.cod_comportamiento=comp.cod_comportamiento WHERE ca.cod_alumno like '%".$_SESSION['comprobante']."%' ORDER BY ca.cod_castigo"; \t \t \t
\t \t
mysqli_set_charset($conexion, "utf8");
if(!$result = mysqli_query($conexion, $sql)) die();
$castigos = array(); //creamos un array
while($row = mysqli_fetch_array($result))
{
$id=$row['cod_castigo'];
$curso=$row['curso'];
$falta=$row['falta'];
\t $alumno=$row['cod_alumno'];
\t $nomalumno=$row['nombre'];/* nuevo */
$prof=$row['nombre_usuario'];
\t $emp=$row['emplazamiento'];
$fecha=$row['fecha'];
$obs=$row['observaciones'];
$asi=$row['asistencia'];
\t $comp=$row['comportamiento'];
\t $tarea=$row['tarea'];
\t
$castigos[] = array('cod_castigo'=> $id, 'curso'=> $curso, 'falta'=> $falta, 'cod_alumno' => $alumno, 'nombre' => $nomalumno, 'nombre_usuario'=> $prof,
'emplazamiento' => $emp, 'fecha'=> $fecha,'observaciones'=> $obs, 'asistencia'=> $asi, 'comportamiento' =>$comp, 'tarea' => $tarea,);
\t
}
//desconectamos la base de datos
$close = mysqli_close($conexion)
or die("Ha sucedido un error inexperado en la desconexion de la base de datos");
//Creamos el JSON
$json_string = json_encode($castigos);
echo $json_string;
?>
甲版本,忒redirige一個generalJSON porque ESO ES LO闕秈稻EL ACTION德爾形式 '<形式方法= 「POST」 行動= 「generarJSON.php」' 沒有關於提交地點的信息。 –
我很高興你找到了解決問題的辦法。但是,實際的答案/解決方案應該不會**編輯到您的問題中。一般而言,您應該[編輯]問題來澄清問題*,但不要在問題中包含答案。您應該使用您用於解決問題的代碼創建自己的答案,然後接受它(系統可能需要48小時的延遲才能接受您自己的答案)。如果你自己解決了這個問題,[鼓勵回答你自己的問題](http://stackoverflow.com/help/self-answer)。 – Makyen