![](/img/trans.png)
[英]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.