普通网友 2015-10-07 14:28
浏览 33
已采纳

数据库仅从链接列表中吐出特定ID的行数据,而不是数据库中的所有行

I have a PHP page that spits out the ID # and the Last Name from a table in a database.

I want to be able to click on the ID #, have it go to a new page, and view the rows info for that specfic ID # only.

Here is some code from my print.php page where it lists out the ID # and the Last Name which works great.

ID # = m_id

Last Name = m2_2

$db = new mysqli('localhost', 'username', 'password', 'name');

if($db->connect_errno > 0)
{ 
die('Unable to connect to database [' . $db->connect_error . ']'); 
}

$sql = "SELECT * FROM `test_gina` ORDER BY m_id DESC";

if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}

while($row = $result->fetch_assoc()){

if($row['m_id'] =="x"){echo " ";}else{ echo '<a href="id.php">'; echo $row['m_id'];}echo '</a>';

if($row['m2_2'] =="x"){echo " ";}else{echo $row['m2_2'];}echo '<br>';

When it goes to the id.php, it shows ALL the rows from the table in the database.

I am not that expierenced in databases, so I know I am doing it wrong, but how do I "connect" the 2 pages to have id.php only show the information for that specfic ID #?

Here is some of my code from my id.php page.

$db = new mysqli('localhost', 'username', 'password', 'name');

if($db->connect_errno > 0)
{ 
die('Unable to connect to database [' . $db->connect_error . ']'); 
}

$sql = "SELECT * FROM `test_gina` ORDER BY m_id DESC";

if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}

while($row = $result->fetch_assoc()){

if($row['m_id'] =="x"){echo " ";}else{echo '<strong>ID #:</strong>'; echo $row['m_id'];}echo '</br>';
echo "";if($row['m_P'] =="x"){echo " ";}else
{
switch($row['m_P'])
{
case "35310":
echo "<strong>Persons living/Household:</strong> 1"; 
break;
}

Any help would be appreciated greatly!!!

  • 写回答

1条回答 默认 最新

  • doucai1901 2015-10-07 14:40
    关注

    You should really look into writing "clean" codes. That being said, in your "print.php" replace

    echo '<a href="id.php">'; echo $row['m_id'];}echo '</a>'
    

    with

    echo '<a href="id.php?id='. $row['m_id']. '">'. $row['m_id']. '</a>'
    

    Notice I'm using concatenation, rather than multiple echo statements.

    Briefly put, the "?" and what follows it in the href attribute of the <a> tag is what is called a query string.

    Then, in your "id.php" file, you get hold of the query string using the GET super global, like below:

    $id = $_GET['id'];
    

    your query string should then look like this:

    $sql = "SELECT * FROM `test_gina` WHERE m_id = $id ORDER BY m_id DESC";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥18 关于#贝叶斯概率#的问题:这篇文章中利用em算法求出了对数似然值作为概率表参数,然后进行概率表计算,这个概率表是怎样计算的呀
  • ¥15 Android Navigation: 某XDirections类不能自动生成
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥100 单片机hardfaulr
  • ¥20 手机截图相片分辨率降低一半
  • ¥50 求一段sql语句,遇到小难题了,可以50米解决
  • ¥15 速求,对多种商品的购买力优化问题(用遗传算法、枚举法、粒子群算法、模拟退火算法等方法求解)
  • ¥100 速求!商品购买力最优化问题(用遗传算法求解,给出python代码)
  • ¥15 虚拟机检测,可以是封装好的DLL,可付费