I want to display data from a specific table when i select same in the select tag in view page, don't know why the code is displayed this way - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Monday, April 17, 2023

I want to display data from a specific table when i select same in the select tag in view page, don't know why the code is displayed this way

View_rentee.php

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Your PG</title>
    <?php require 'navbar.php' ?>
    <link rel="stylesheet" href="view_rentee.css">

</head>


<body>

    <div class="selection">
        <select name="pgname" id="pgname">
            <?php
      include 'dbconnect.php';
      $user = $_SESSION['username'];
      $existSql = "SELECT pgname FROM `addpg` WHERE `username` = '$user'";
      $result = mysqli_query($conn, $existSql);
      $numExistRows = mysqli_num_rows($result);

      if ($numExistRows > 1) {
        echo " <option value='Select PG' selected>Select PG </option>";
        $sql = "SELECT pgname FROM addpg where username = '$user' ";
        $res = mysqli_query($conn, $sql);

        while ($pgname = mysqli_fetch_assoc($res)) {
          echo '<option value="' . $pgname['pgname'] . '">' . $pgname['pgname'] . '</option>';
        }
      }
      if ($numExistRows == 1) {

        $sql = "SELECT pgname FROM addpg where username = '$user' ";
        $res = mysqli_query($conn, $sql);
        $pgname = mysqli_fetch_assoc($res);
        echo '<option value="' . $pgname['pgname'] . '">' . $pgname['pgname'] . '</option>';
      }
      if ($numExistRows == 0) {
        echo " <option value='Register Your PG' selected>Register Your PG To Data </option>";
      }

      ?>
        </select>
    </div>

    <div class="searchbar">
        <form action="view_rentee.php" method="POST">
            <input id="search" name="search" type="search" placeholder="Search here...">
            <button type="submit">Search</button>
        </form>
    </div>






    <div class="container">
        <div class="row">
            <div class="col-sm-8">
                <?php echo $deleteMsg ?? ''; ?>
                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>
                            <tr>

                                <th>Room Number</th>
                                <th>Aadhar Number</th>
                                <th>Full Name</th>
                                <th>Email</th>
                                <th>Father Name</th>
                                <th>Mobile Number</th>
                                <th>Parents Mobile Number</th>
                                <th>Date of Joining</th>
                                <th>Address</th>
                                <th>Gender</th>
                                <th>Edit</th>
                                <th>Delete</th>
                        </thead>
                        <tbody>
                            <?php
              if (is_array($fetchData)) {
                foreach ($fetchData as $data) {
                  ?>
                            <tr>

                                <td>
                                    <?php echo $data['Roomno'] ?? ''; ?>
                                </td>
                                <td>
                                    <?php echo $data['Aadharno'] ?? ''; ?>
                                </td>
                                <td>
                                    <?php echo $data['fullname'] ?? ''; ?>
                                </td>
                                <td>
                                    <?php echo $data['email'] ?? ''; ?>
                                </td>
                                <td>
                                    <?php echo $data['fathername'] ?? ''; ?>
                                </td>
                                <td>
                                    <?php echo $data['phoneno'] ?? ''; ?>
                                </td>
                                <td>
                                    <?php echo $data['pphoneno'] ?? ''; ?>
                                </td>
                                <td>
                                    <?php echo $data['doj'] ?? ''; ?>
                                </td>
                                <td>
                                    <?php echo $data['address'] ?? ''; ?>
                                </td>
                                <td>
                                    <?php echo $data['gender'] ?? ''; ?>
                                </td>
                                <td>
                                    <form action="first_register.php" method="POST">
                                        <input type="hidden" name="edit_username"
                                            value="<?php echo $row['username']; ?>">
                                        <button type="submit" name="edit_button" class="btn btn-success">Edit</button>
                                    </form>
                                </td>
                                <td>
                                    <button type="submit" class="btn btn-danger">Delete</button>
                                </td>
                            </tr>
                            <?php
                }
              } else { ?>
                            <tr>
                                <td colspan="12">
                                    <?php echo $fetchData; ?>
                                </td>
                            <tr>
                                <?php
              } ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div id="search results"></div>

</body>

</html> 

developers.php

1) { $sql = "SELECT pgname FROM addpg where username = '$user' "; $res = mysqli_query($conn, $sql); $cnfpgname = mysqli_fetch_assoc($res); $cnfname = $cnfpgname['PGname']; if ($pgname == $cnfname) { $tableName= preg_replace('/\s+/', '', $pgname); } } if ($numExistRows == 1) { $sql = "SELECT pgname FROM addpg where username = '$user' "; $res = mysqli_query($conn, $sql); $pgname = mysqli_fetch_assoc($res); $pgnam = $pgname['pgname']; $tableName = preg_replace('/\s+/', '', $pgnam); } $db = $conn; $columns = [ 'PGname', 'Roomno', 'Aadharno', 'fullname', 'email', 'fathername', 'phoneno', 'pphoneno', 'doj', 'address', 'gender' ]; $fetchData = fetch_data($db, $tableName, $columns); function fetch_data($db, $tableName, $columns) { if (empty($db)) { $msg = "Database connection error"; } elseif (empty($columns) || !is_array($columns)) { $msg = "columns Name must be defined in an indexed array"; } elseif (empty($tableName)) { $msg = "Table Name is empty"; } else { $columnName = implode(", ", $columns); $query = "SELECT " . $columnName . " FROM $tableName" . " "; $result = $db->query($query); if ($result == true) { if ($result->num_rows > 0) { $row = mysqli_fetch_all($result, MYSQLI_ASSOC); $msg = $row; } else { $msg = "No Data Found"; } } else { $msg = mysqli_error($db); } } return $msg; } ?>

source https://stackoverflow.com/questions/76029520/i-want-to-display-data-from-a-specific-table-when-i-select-same-in-the-select-ta

No comments:

Post a Comment