过滤数据库表中的记录以进行查看和更新

时间:2013-06-09 14:53:02

标签: php html

我正在尝试从数据库中提取记录并将其显示在我的php表单上,供用户更新。在我的数据库表中,有一个名为stage的列。在舞台下,有基础,中级和进步。这是一个例子:

Valid XHTML http://img259.imageshack.us/img259/2461/databasei.pngValid XHTML http://imageshack.us/photo/my-images/849/profileint.png/

因此,当我按下我的php表格(第2张图片)中的粉底按钮时,只会记录他们的舞台为基础的记录,将显示在列表中(连同他们的姓名,联系电话,电子邮件和学生证) 有什么方法可以做到吗

2 个答案:

答案 0 :(得分:0)

你在找这样的东西吗?

<?php
  $stage = $_GET['stage'];
  $sql = 'SELECT * FROM {$tablename} WHERE stage=:stage';

  $db = new PDO('mysql:host=localhost;dbname=yourdbname', $user, $pass);
  try {
    $statement = $db->prepare($sql);
    $statement->execute(array(':stage', db->quote($stage)));
    foreach($statement->fetchAll() as $row) {
      // Do things here.
    }
  } catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
  }

答案 1 :(得分:0)

<?php
    include('connect-db2.php');

    // get results from database
    $result = mysql_query("SELECT * FROM players2 WHERE stage = 'foundation'") 
            or die(mysql_error());  

    echo "<table border='1' cellpadding='10'>";
    echo "<tr> <th>S/N</th> <th>Student_ID</th> <th>Name</th> <th>Contacts No.</th> <th>Email</th><th>Stage</th></tr>";

    while($row = mysql_fetch_array( $result )) {
        // echo out the contents of each row into a table
        echo "<tr>";
        echo '<td>' . $row['id'] . '</td>';
        echo '<td>' . $row['student_id'] . '</td>';
        echo '<td>' . $row['name'] . '</td>';
        echo '<td>' . $row['contact_no'] . '</td>';
        echo '<td>' . $row['email'] . '</td>';
        echo '<td>' . $row['stage'] . '</td>';
        echo '<td><a href="edit2.php?id=' . $row['id'] . '">Edit</a></td>';
        echo '<td><a href="delete2.php?id=' . $row['id'] . '">Delete</a></td>';
        echo "</tr>";
    } 
?>