使用PHP从mysql显示的可点击数据

时间:2013-09-17 07:58:01

标签: javascript php html mysql forms

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

我有三个页面:page1.phppage2.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上编写代码,以便用户点击每列旁边的视图 sdq 按钮,应捕获学生ID,向数据库发送请求,并查询与该特定学生相关的其他数据,如年龄,地址,电话等。并将其显示在page3.php

2 个答案:

答案 0 :(得分: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 sentense:

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

这是一般的想法。

答案 1 :(得分:0)

<强>使page2.php

您必须在第2页添加一个hidden input,其中包含学生的ID。

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

将您的action页面更改为第3页(如果您要显示学生信息的位置:姓名,年龄......)

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

第2页的代码:

<?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

第3页的可行解决方案可以是:(添加您需要的列:年龄,地址,电话......)

 <?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);
    ?>