0

Here in my datatable, I want when we click on application id it should open a new page (formdetails.php) where all the rest of data will be shown. How can we make hyper link of application id?

Here is the code for my datatable ( page that index the applications list ).

<?php
    include("appconnect.php");
    $sql = "SELECT app_id, firstname, journeydate, appl_type, passportno, arrivalport, birthdate, phone FROM tblapps";
    $result = $connect->query($sql);
    if ($result->num_rows > 0) {
        echo "<table class=‘table table-bordered table-striped table-condensed flip-content’>
            <tr>
                <th>App ID</th><th>Name</th><th>Journey Date</th><th>App type</th><th>Passport No</th><th>Arrival Port</th><th>BirthDate</th><th>Phone</th>
            </tr>";

         // output data of each row
         while($row = $result->fetch_assoc()) {
            echo"<tr>
            <td>" . $row["app_id"]. "</td><td>" . $row["firstname"]. "</td><td>" . $row["journeydate"]. "</td><td>" . $row["appl_type"]. "</td><td>" . $row["passportno"]. "</td><td>" . $row["arrivalport"]. "</td><td>" . $row["birthdate"]. "</td><td>" . $row["phone"]. "</td></tr>";
    }
        echo "</table>";
    } else {
        echo "0 results";
    }

    $connect->close();
?>

this is on formdetails.php page

<?php 
        session_start(); /* Starts the session */
        if(!isset($_SESSION['UserData']['Username'])){  
        header("location:login.php");   exit;}  

        $appid = $_GET['app_id'];
        include("appconnect.php");

        $sql = "SELECT * FROM tblapps WHERE app_id='.$appid.'";
        $result = $connect->query($sql);

?>

<html>
<body>
<p>Applicant Name - <?php echo $firstname ?> </p><br>
<p> Passportno - <?php echo $passportno ?>
</body>
</html>

there is more than 50 fields that needs to be shown, but its not working. i know it is basic and easy to do but i am stucked as i am a beginner.

Gaurav Singh
  • 43
  • 2
  • 10

1 Answers1

1

The easiest way to do this is sending GET parameters using a hyperlink.

You can output your app_id column cells as

<td><a href=\"formdetails.php?app_id=".$row["app_id"]."\">". $row["app_id"]. "</a></td>

And then in your formdetails.php page you can access $_GET['app_id'] to query your database

However it would be wise to obfuscate the column name by passing id or something generic instead.

Also consider using PDO for your database interactions

EDIT:

Try this? I think there are issues with your sql query.

Also your SQL query is probably vulnerable to SQL INJECTION - please consider revising this in production

index.php

<?php
    include("appconnect.php");
    $sql = "SELECT app_id, firstname, journeydate, appl_type, passportno, arrivalport, birthdate, phone FROM tblapps";
    $result = $connect->query($sql);
    if ($result->num_rows > 0) {
        echo "<table class=‘table table-bordered table-striped table-condensed flip-content’>
            <tr>
                <th>App ID</th><th>Name</th><th>Journey Date</th><th>App type</th><th>Passport No</th><th>Arrival Port</th><th>BirthDate</th><th>Phone</th>
            </tr>";

         // output data of each row
         while($row = $result->fetch_assoc()) {
            echo"<tr>
            <td><a href=\"formdetails.php?app_id=".$row["app_id"]."\">". $row["app_id"]. "</a></td><td>" . $row["firstname"]. "</td><td>" . $row["journeydate"]. "</td><td>" . $row["appl_type"]. "</td><td>" . $row["passportno"]. "</td><td>" . $row["arrivalport"]. "</td><td>" . $row["birthdate"]. "</td><td>" . $row["phone"]. "</td></tr>";
    }
        echo "</table>";
    } else {
        echo "0 results";
    }

    $connect->close();
?>

formdetails.php

<?php 
    session_start(); /* Starts the session */
    if(!isset($_SESSION['UserData']['Username'])){  
    header("location:login.php");   exit;}  

    $appid = $_GET['app_id'];
    include("appconnect.php");

    $sql = 'SELECT * FROM tblapps WHERE app_id='.$appid;
    $result = $connect->query($sql);
?>

<html>
    <body>
    <p>Applicant Name - <?php echo $firstname ?> </p><br>
    <p> Passportno - <?php echo $passportno ?>
    </body>
</html>
mquinn
  • 454
  • 4
  • 14
  • i am stucked here, i have updated my question, because i am getting error on formdetails page. – Gaurav Singh Jan 04 '18 at 22:55
  • I have edited my answer to give you some pointers using your code. If you need any further help please update with the result you are getting. – mquinn Jan 05 '18 at 09:25