How to send data from a dynamic php table inside an anchor tag to another page

Tags: ,



How to send data from a dynamic php table inside an anchor tag to another page(description_of_gpu.php) and also how to retrieve that data on the new page.

I am trying to send the variable generated inside the table-data tag to another page, which is in itself retrieved from “MySQL” database. Once I get that data, I will search through my database with the same “NameOfVcard” and display it’s discription.

Code inside “gpu_table_page.php”

<?php
$conn = mysqli_Connect('localhost', 'root', '', 'gpu_db');
//$result = mysqli_query($conn,"select * from gpu_table");
?>
<!DOCTYPE html>
 <html>
  <head>
       <title>gpu page</title>
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
       <link rel="stylesheet" href="css/style_of_gpu.css" type="text/css">
       <script type="text/javascript" src="javascript/javascript_of_gpu.js"></script>
  </head>
  <body>
   <br />
      <br/>
      <div style="margin-left: 150px; margin-right: 20px; vertical-align: right">
       <table style="margin-top: 40px" border="3px">
         <thead>
              <tr>
               <th>SrNo</th>
               <th>Video card Name</th>
               <th>Architecture</th>
               <th>Boost_clock</th>
               <th>Core_speed</th>
               <th>Memory</th>
               <th>Manufacturer</th>
              </tr>
      </thead>
        <tbody>
          <?php
              $data = 1000;
              $sql = "SELECT * FROM gpu_table WHERE Core_Speed<? ORDER BY Core_Speed DESC;";
              $stmt = mysqli_stmt_init($conn);
              if(!mysqli_stmt_prepare($stmt, $sql)){
                echo "SQL statement failed";
              }
              else{
                mysqli_stmt_bind_param($stmt,"i",$data);
                mysqli_stmt_execute($stmt);
                $result = mysqli_stmt_get_result($stmt);
                $k = 1;
                while ($row = mysqli_fetch_array($result))
                {
                echo "<tr>";
                echo "<td>".$k."</td>";
                echo "<td><a href="description_of_gpu.php?NameOfVcard=.$row['Name'].">".$row['Name']."</a></td>";;
                echo "<td>".$row['Architecture']."</td>";
                echo "<td>".$row['Boost_Clock']."</td>";
                echo "<td>".$row['Core_Speed']."</td>";
                echo "<td>".$row['Memory']."</td>";
                echo "<td>".$row['Manufacturer']."</td>";
                echo "</tr>";
                $k = $k + 1;
              }//while end
            }//else end
              ?>
        </tbody>
      </table>
    </div>

   <br />
  </body>
</html>

Code inside “description_of_gpu.php”

<?php
$conn = mysqli_Connect('localhost', 'root', '', 'gpu_db');
//$result = mysqli_query($conn,"select * from gpu_table");
?>
<!DOCTYPE html>
 <html>
  <head>
       <title>gpu page</title>
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
       <link rel="stylesheet" href="css/style_of_gpu.css" type="text/css">
       <script type="text/javascript" src="javascript/javascript_of_gpu.js"></script>
  </head>
  <body>
   <br />
      <br/>

          <?php
              //the below "$data" variable will contain the "NameOfVcard" data.
              $data = "GeForce GTX 1080 Ti Zotac Founders Edition 11GB";//currently "$data" is a static variable  
              $sql = "SELECT * FROM gpu_table WHERE Name=?;";
              $stmt = mysqli_stmt_init($conn);
              if(!mysqli_stmt_prepare($stmt, $sql)){
                echo "SQL statement failed";
              }
              else{
                mysqli_stmt_bind_param($stmt,"s",$data);
                mysqli_stmt_execute($stmt);
                $result = mysqli_stmt_get_result($stmt);
                $k = 1;
                while ($row = mysqli_fetch_array($result))
                {
                echo "<p>";
                                echo "<em>".$row['Name']."</em>"." is from the architecture "."<b>".$row['Architecture']."</b>"."where Boost_Clock is "."<b>".$row['Boost_Clock']."</b>"."Mhz and Core_Speed is ".$row['Core_Speed']."Mhz having ".$row['Memory']."mb of DRAM which is of the type ".$row['Memory_Type']." and have ".$row['DVI_Connection']." DVI connections";
                                echo "<br>";
                                echo "also having ".$row['HDMI_Connection']." HDMI ports which is Manufactured by ".$row['Manufacturer']." consuming ".$row['Max_Power']." power and it has ".$row['Power_Connector']." power connectors ";
                                echo "</p>";
                $k = $k + 1;
              }//while end
            }//else end
              ?>
    </div>

   <br />
  </body>
</html>

Answer

As the PHP is using double-quotes around the strings you can embed the variable within this string without needing to escape it so you can modify

echo "<tr>";
echo "<td>".$k."</td>";
echo "<td><a href="description_of_gpu.php?NameOfVcard=.$row['Name'].">".$row['Name']."</a></td>";;
echo "<td>".$row['Architecture']."</td>";
echo "<td>".$row['Boost_Clock']."</td>";
echo "<td>".$row['Core_Speed']."</td>";
echo "<td>".$row['Memory']."</td>";
echo "<td>".$row['Manufacturer']."</td>";
echo "</tr>";

to be more like this

echo "
    <tr>
        <td>{$k}</td>
        <td><a href='description_of_gpu.php?NameOfVcard={$row['Name']}'>{$row['Name']}</a></td>
        <td>{$row['Architecture']}</td>
        <td>{$row['Boost_Clock']}</td>
        <td>{$row['Core_Speed']}</td>
        <td>{$row['Memory']}</td>
        <td>{$row['Manufacturer']}</td>
    </tr>";

Then, within description_of_gpu.php you should be able to access the GET variable NameOfVcard, like so:

$NameOfVcard=isset( $_GET['NameOfVcard'] ) ? $_GET['NameOfVcard'] : false;

leading on to

$sql = "SELECT * FROM gpu_table WHERE Name=?;";
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt,$sql);
mysqli_stmt_bind_param($stmt,"s",$NameOfVcard);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);


Source: stackoverflow