0

我开发了一个 PHP 页面,显示 MySQL 表中的所有项目。现在我想在用户单击此页面上的某个项目时显示一个特定项目。如何获取此特定 ID 以及如何配置<a href="">链接?

更新问题:

这是 cars.php 页面的标题(包含所有结果):

<?

require("admin/db/connect.php");

$sql = "SELECT * FROM tb_carros";

$limite = mysql_query("$sql");

$dados = array();
while ($sql = mysql_fetch_array($limite) ) {
    $dados[] = $sql;
}

?>

和 HTML:

    <?php foreach ($dados as $row): ?>

    <div id="containerResumo">

        <a href="#"> <!-- this is the key problem -->

        <div class="dadosResumo">
            <?=$row['carro']?><br /><br />
            Ano:   <?=$row['ano']?><br /><br />
            Câmbio: <?=$row['cambio']?><br /><br />
            R$ <?=$row['valor']?>
        </div><!-- END of dadosItem -->
        </a>

    </div><!-- END of containerResumo -->

    <?php endforeach ?>

现在,当用户单击一个项目时,我想打开页面 carro_item.php 并在其上加载项目的数据。

数据库中的参考 ID 是 id_carro

我尝试了许多类型的代码,但没有奏效。即使我将完整的网址放在浏览器上,也不会加载数据:

http://adrianomachado.com/testesClientes/carro_item.php?id_carro=1

这是 carro_item.php 的 PHP:

<?

require("admin/db/connect.php");

$id = (int)$_GET['id_carro'];

$sql = "SELECT * FROM tb_carros WHERE id = $id";

?>

和 HTML:

            <div class="dadosItem">
                R$ <?php $valor ?><br /><br />
                Ano:   <?php $ano ?><br /><br />
                Kilometragem: <?php $km ?><br /><br />
                Cor: <?php $cor ?><br /><br />
                Portas: <?php $portas ?><br /><br />
                Combustível: <?php $combustivel ?><br /><br />
                Câmbio: <?php $cambio ?><br /><br />
                Final da placa: <?php $final_placa ?><br /><br />
                Carroceria: <?php $carroceria ?>
            </div><!-- END of dadosItem -->

有什么帮助吗?

更新 02:

这是 carro_item 中的查询:

<?

require("admin/db/connect.php");

$sql = "SELECT * FROM tb_carros";

$limite = mysql_query("$sql");

$dados = array();
while ($sql = mysql_fetch_array($limite) ) {
    $dados[] = $sql;
}


?>

但是,很明显它会返回所有结果,例如 cars.php 页面。问题是如何将结果过滤到用户单击的链接的相同 ID?

我不知道如何编写 $sql = "SELECT * FROM tb_carros"; 行来做到这一点。

4

2 回答 2

1

您可以使用$_GETvariables

如果页面上的链接格式如下:

<a href="mypage.php?id=5">

然后在您的 php 页面中,您将能够通过全局$_GET数组访问该值。

$id = mysqli_real_escape_string($_GET['id']); // $id will have the value passed to it by the link

请注意不要让自己对SQL 注入开放,尽管通过清理参数或使用参数化查询。

参考

编辑:

To create the links formatted in the correct way, you'd first retrieve all of the ids you need and store them in an array. I'll use $ids as an example.

$ids = array(1, 50, 25, 62, ...); // This was populated from the database

// Loop through all ids and output link code for each one
foreach ($ids as $link_id) {
  echo '<a href="mypage.php?id=' . $link_id . '">Click me</a>';
}

Edit2:

In cars.php format the links like this:

<a href="/testesClientes/carro_item.php?id_carro=<?= $row['id'] ?>">

Edit3:

Your carro_item.php should look something like this:

<?php

  require("admin/db/connect.php");

  $id = (int)$_GET['id_carro'];

  $sql = "SELECT * FROM tb_carros WHERE id = $id";

  $result = mysql_query($sql);

  $row = mysql_fetch_array($result);

  // ...
?>

<!-- And your HTML should look something like this -->
<!-- ... -->

<div class="dadosItem">
  R$ <?= $valor ?><br /><br />
  Ano:   <?= $row['ano'] ?><br /><br />
  Kilometragem: <?= $row['km'] ?><br /><br />
  Cor: <?= $row['cor'] ?><br /><br />
  Portas: <?= $row['portas'] ?><br /><br />
  Combustível: <?= $row['combustivel'] ?><br /><br />
  Câmbio: <?= $row['cambio'] ?><br /><br />
  Final da placa: <?= $row['final_placa'] ?><br /><br />
  Carroceria: <?= $row['carroceria'] ?>
</div><!-- END of dadosItem -->

<!-- ... -->

Also, you should stray from using functions of the form mysql_* as they are deprecated. See Why shouldn't I use mysql_* functions in PHP? for more information.

于 2013-04-07T21:44:06.347 回答
0

PHP:

 $id = (int)$_GET['id'];

HTML:

<a href="page.php?id=<?= $id; ?>">

询问:

SELECT * FROM table WHERE id = $id;
于 2013-04-07T21:44:05.280 回答