2

我不太擅长 JavaScript,但这是我的问题。

我有三页:page1.php,,,page2.phppage3.php

在 上page1.php,我有一个表单供用户选择他们想要查看的年级,然后执行操作page2.php——显示该年级所有学生的列表。

这是代码page2.php

     <?php

//database variables
require_once('admin_settings.php');


//these variables are from a form used to display the current data
$level = $_POST['level_group'];
$room = $_POST['room_group'];


$con=mysqli_connect("$host","$dbuser","$dbpass","$dbname");
mysqli_set_charset($con, "utf8");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  } 
$result = mysqli_query($con,"SELECT std_id, std_name FROM students  WHERE std_level LIKE '$level%' AND std_room LIKE '$room';");

//table 
echo"
<table border='1' id='mytable'>
<tr bgcolor = #99CCFF>
<th><b>Student ID</b></th>
<th><b>Name</b></th>
<th><b>Action</b></th>
</tr>";

//loop through the database



while($row = mysqli_fetch_array($result))
  {
  echo"<form action='view_one_student.php' method='post'>";
  echo "<tr bgcolor = '#c0eae4n'  id = 'listings'>";
  echo "<td name= 'stdid'>" . $row['std_id'] . "</td>";
  echo "<td>" . $row['std_name'] . "</td>";
  echo "<td>" . '<input type="submit" value="view"> <input type="submit" value="sdq">' . "</td>";
  echo "</tr>";
  echo "</form>";
  }
echo "</table>";

mysqli_close($con);
?> 

问题...现在是如何编写代码,page3.php以便当用户单击每列旁边的viewsdq按钮时,应捕获学生 ID,向数据库发送请求,并查询与此相关的其他数据特定的学生,例如年龄、地址、电话等。并在上面显示它们page3.php

4

2 回答 2

0

您可以像这样修改您的代码。隐藏字段会将学生 ID 发送到 view_one_student.php 页面。

while($row = mysqli_fetch_array($result))
  {
  echo"<form action='view_one_student.php' method='post'>";
  echo "<tr bgcolor = '#c0eae4n'  id = 'listings'>";
  echo "<td name= 'stdid'>" . $row['std_id'] . "</td>";
  echo "<td>" . $row['std_name'] . "</td>";
  echo '<input type="hidden" value="'.$row["std_id"].'" name="std_id">';
  echo "<td>" . '<input type="submit" value="view"> <input type="submit" value="sdq">' . "</td>";
  echo "</tr>";
  echo "</form>";
  }
echo "</table>";

mysqli_close($con);

在 view_one_student.php 你必须抓住它:

$_POST["std_id"]

并最终将其输入到 view_one_student.php 中的 sql 语句中:

$sql = "SELECT * FROM <table> WHERE id=".$_POST["std_id"];

这就是一般的想法。

于 2013-09-17T08:18:13.203 回答
0

page2.php

您必须hidden input在 page2 中添加一个学生的 id。

<input type="hidden" value="'.$row["std_id"].'" name="std_id">

将您的页面更改action为 page3(如果您想在此处显示学生信息:姓名、年龄...)

<form action='page3.php' method='post'>page2.php

page2的代码:

<?php

//database variables
require_once('admin_settings.php');


//these variables are from a form used to display the current data
$level = $_POST['level_group'];
$room = $_POST['room_group'];


$con=mysqli_connect("$host","$dbuser","$dbpass","$dbname");
mysqli_set_charset($con, "utf8");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  } 
$result = mysqli_query($con,"SELECT std_id, std_name FROM students  WHERE std_level LIKE '$level%' AND std_room LIKE '$room';");

//table 
echo"
<table border='1' id='mytable'>
<tr bgcolor = #99CCFF>
<th><b>Student ID</b></th>
<th><b>Name</b></th>
<th><b>Action</b></th>
</tr>";

//loop through the database



while($row = mysqli_fetch_array($result))
  {
  echo"<form action='page3.php' method='post'>";
  echo "<tr bgcolor = '#c0eae4n'  id = 'listings'>";
  echo "<td name= 'stdid'>" . $row['std_id'] . "</td>";
  echo "<td>" . $row['std_name'] . "</td>";
  echo "<td> <input type='hidden' value='" . $row["std_id"] . "' name='std_id'>";
  echo "<input type='submit' value='view'>";
  echo "<input type='submit' value='sdq'></td>";
  echo "</tr>";
  echo "</form>";
  }
echo "</table>";

mysqli_close($con);
?> 

page3.php

page3 的一个可行解决方案可以是:(添加您需要的列:年龄、地址、电话..)

 <?php

    //database variables
    require_once('admin_settings.php');


    //these variables are from a form used to display the current data
    $id = $_POST['std_id'];

    $con=mysqli_connect("$host","$dbuser","$dbpass","$dbname");
    mysqli_set_charset($con, "utf8");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      } 
    $result = mysqli_query($con,"SELECT * FROM students  WHERE std_id ='$id';");



    echo '<h3>Student detail</h3>';

    //table 
    echo"
    <table border='1' id='mytable'>
    <tr bgcolor = #99CCFF>
    <th><b>Student ID</b></th>
    <th><b>Name</b></th>
    <th><b>Age</b></th>
    </tr>";


    //loop through the database

    while($row = mysqli_fetch_array($result))
      {
      echo "<tr bgcolor = '#c0eae4n'  id = 'listings'>";
      echo "<td name= 'stdid'>" . $row['std_id'] . "</td>";
      echo "<td>" . $row['std_name'] . "</td>";
      echo "<td>" . $row['std_age'] . "</td>";
      echo "</tr>";
      echo "</form>";
      }
    echo "</table>";


    mysqli_close($con);
    ?> 
于 2013-09-17T09:41:35.097 回答