Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Function to query information based on
- * a parameter: in this case, location.
- *
- */
- if (isset($_POST['submit'])) {
- try {
- $host = "localhost";
- $username = "root";
- $password = "";
- $dbname = "test";
- $dsn = "mysql:host=$host;dbname=$dbname";
- $options = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION );
- function escape($html) { return htmlspecialchars($html, ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8"); }
- $connection = new PDO($dsn, $username, $password, $options);
- $sql = "SELECT *
- FROM student
- WHERE id = :id";
- $id = $_POST['id'];
- $statement = $connection->prepare($sql);
- $statement->bindParam(':id', $id, PDO::PARAM_STR);
- $statement->execute();
- $result = $statement->fetchAll();
- } catch(PDOException $error) {
- echo $sql . "<br>" . $error->getMessage();
- }
- }
- ?>
- <center><h1>MBA P1 Student's Database App</h1>
- <style>
- label { display: block; margin: 5px 0; }
- table { border-collapse: collapse; border-spacing: 0; } td,
- th { padding: 5px; border-bottom: 1px solid #aaa; }
- </style>
- <?php
- if (isset($_POST['submit'])) {
- if ($result && $statement->rowCount() > 0) { ?>
- <h2>Results</h2>
- <table>
- <thead>
- <tr>
- <th>ID</th>
- <th>Full Name</th>
- <th>Student ID</th>
- </tr>
- </thead>
- <tbody>
- <?php foreach ($result as $row) { ?>
- <tr>
- <td><?php echo escape($row["id"]); ?></td>
- <td><?php echo escape($row["fullname"]); ?></td>
- <td><?php echo escape($row["student_id"]); ?></td>
- </tr>
- <?php } ?>
- </tbody>
- </table>
- <?php } else { ?>
- <blockquote>No results found for <?php echo escape($_POST['id']); ?>.</blockquote>
- <?php }
- } ?>
- <h2>Find Student's Full Name</h2>
- <form method="post">
- <label for="id">Last 2 Digit of id's</label>
- <input type="text" id="id" name="id">
- <input type="submit" name="submit" value="View">
- </form>
Advertisement
Add Comment
Please, Sign In to add comment