简体   繁体   English

php a href 显示来自 mysql 数据库的数据并将其链接到新页面

[英]php a href display data from mysql database and link it to a new page

I have a php & mysql search script which is working, after result displayed users can click below link and it will open a new page and display more mysql info, i am stuck with linking the ahref link ($field2name) to the new page (getprofile.php).我有一个正在运行的 php 和 mysql 搜索脚本,结果显示后,用户可以单击下面的链接,它将打开一个新页面并显示更多 mysql 信息($ 字段链接到新页面)获取配置文件.php)。

Now the problem is getprofile.php showed nothing and displayed 0 results, however i tried to put static data in getprofile.php it is working properly and can show profile data from mysql, can anyone enlighten me what is missing? Now the problem is getprofile.php showed nothing and displayed 0 results, however i tried to put static data in getprofile.php it is working properly and can show profile data from mysql, can anyone enlighten me what is missing?

from search page user can click this link:从搜索页面用户可以单击此链接:

<td><a href="getprofile.php?'.$field2name.'" target = "_blank">'.$field2name.'</td>

getprofile.php: getprofile.php:

<?php
 $conn = mysqli_connect("localhost","root","password","demo");
 $pkey = mysql_real_escape_string($_GET[$field2name]);
// $pkey = "4027500001"; <-------if i put static data it can show profile data
    
  $query = "SELECT * FROM nhc WHERE code =" . $pkey;
    $result = $conn->query($query);


if ($result->num_rows > 0) {
    echo "<table border='1'><tr><th>code</th><th>Name</th><th>class</th><th>AM</th><th>May GA</th><th>June GA</th></tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row["code"]."</td> <td>".$row["name"]."</td> <td> ".$row["class"]."</td>  <td>".$row["am"]."</td> <td>".$row["may"]."</td><td>".$row["june"]. "</td></tr>";
  
    }
    echo "</table>";
} else {
    echo "0 results";
}
?>

You need to give the GET parameter a name so that you can access it in PHP:您需要为GET参数命名,以便您可以在 PHP 中访问它:

<td><a href="getprofile.php?code='.$field2name.'" target = "_blank">'.$field2name.'</td>

then in PHP:然后在 PHP 中:

$pkey = mysql_real_escape_string($_GET['code']);

Note that you should use prepared statements to protect yourself from SQL injection;请注意,您应该使用准备好的语句来保护自己免受 SQL 注入; mysql_real_escape_string is not sufficient (see this Q&A ). mysql_real_escape_string是不够的(请参阅此问答)。 For example:例如:

$query = "SELECT * FROM nhc WHERE code = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('i', $_GET['code']);
$stmt->execute();
$result = $stmt->get_result();

You are not assigning $field2name in any variable.您没有在任何变量中分配 $field2name 。 Moreover your quotes are mismatching and are not forming a well formatted url此外,您的报价不匹配并且没有形成格式良好的 url

<td><a href="getprofile.php?value=<?=$field2name?>" target = "_blank">'<?=$field2name?></td>

now to get the value use $_GET['value']现在获取值使用$_GET['value']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM