Php 如何显示MySQL中的特定ID-新手
我开发了一个PHP页面,显示MySQL表中的所有iTen。现在,我想在用户单击此页面上的一个iTen时显示一个特定项目。如何获取此特定ID以及如何配置Php 如何显示MySQL中的特定ID-新手,php,mysql,templates,Php,Mysql,Templates,我开发了一个PHP页面,显示MySQL表中的所有iTen。现在,我想在用户单击此页面上的一个iTen时显示一个特定项目。如何获取此特定ID以及如何配置 现在,当用户点击一个项目时,我想打开carro_item.php页面,页面上加载了该项目的数据 数据库中的参考ID为ID_carro 我尝试了许多类型的代码,但都不起作用。即使我将完整url放在浏览器上,也不会加载数据: 这是carro_item.PHP的PHP: <? require("admin/db/connect.php");
现在,当用户点击一个项目时,我想打开carro_item.php页面,页面上加载了该项目的数据
数据库中的参考ID为ID_carro
我尝试了许多类型的代码,但都不起作用。即使我将完整url放在浏览器上,也不会加载数据:
这是carro_item.PHP的PHP:
<?
require("admin/db/connect.php");
$id = (int)$_GET['id_carro'];
$sql = "SELECT * FROM tb_carros WHERE id = $id";
?>
以及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 ?>
<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 -->
R$
Ano:
公里数:
Cor:
门户:
燃烧水平:
坎比奥:
最后一场比赛:
角质层:
有什么帮助吗
更新02:
这是carro_项中的查询:
<?
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”;行执行此操作。PHP:
$id = (int)$_GET['id'];
HTML:
你可以利用
如果页面上的链接格式如下所示:
<a href="mypage.php?id=5">
小心不要通过清理参数或使用参数化查询而让自己受到影响
参考资料
$ids
作为示例
$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>';
}
Edit3:
您的carro_item.php
应该如下所示:
<a href="/testesClientes/carro_item.php?id_carro=<?= $row['id'] ?>">
<?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 -->
<!-- ... -->
R$
Ano:
公里数:
Cor:
门户:
燃烧水平:
坎比奥:
最后一场比赛:
角质层:
此外,您应该避免使用mysql.*形式的函数,因为它们已被弃用。有关更多信息,请参阅。谢谢,但我也需要链接来获取身份证。例如,我开发的页面将返回'n'结果:item01、item02、itemN。此结果将由客户管理。他将从数据库中包括和排除ITEN。所以我不知道链接ID是5还是100。你知道我该怎么做吗?@AdrianoMachado-当将html输出到页面时,你只需循环浏览所有想要点击的ID即可。看我的编辑。哎呀,我试了很多,但都没用。我用部分代码预测了这个问题。@AdrianoMachado-表中的所有列名是什么?是否有
id
?如何分配carro_item.php
中使用的变量?(如$valor
,$ano
等)@AdrianoMachado-参见我的编辑2。您能否在carro_item.php
中发布执行查询的代码,以及如何分配变量?
$id = mysqli_real_escape_string($_GET['id']); // $id will have the value passed to it by the link
$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>';
}
<a href="/testesClientes/carro_item.php?id_carro=<?= $row['id'] ?>">
<?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 -->
<!-- ... -->