[英]how to send information form one page to another page using anchor tag and fetch data from database in php
[英]How to send data from a dynamic php table inside an anchor tag to another page
如何將數據從錨標記內的動態 php 表發送到另一個頁面(description_of_gpu.php),以及如何在新頁面上檢索該數據。
我正在嘗試將在 table-data 標記內生成的變量發送到另一個頁面,該頁面本身是從“MySQL”數據庫中檢索的。 獲得該數據后,我將使用相同的“NameOfVcard”搜索我的數據庫並顯示它的描述。
“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>
“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>
由於 PHP 在字符串周圍使用雙引號,因此您可以在此字符串中嵌入變量而無需對其進行轉義,因此您可以修改
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>";
更像這樣
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>";
然后,在description_of_gpu.php
,您應該能夠訪問 GET 變量NameOfVcard
,如下所示:
$NameOfVcard=isset( $_GET['NameOfVcard'] ) ? $_GET['NameOfVcard'] : false;
導致
$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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.