简体   繁体   English

PHP 获取上一个链接ID

[英]PHP get previous link ID

I am fetching all records from a db table into a page using;我正在使用以下方法将数据库表中的所有记录提取到页面中;

SELECT * FROM table_name;

The page displays a summary of car details:该页面显示汽车详细信息的摘要:

汽车清单

Now the idea is to create another page that will load the full content of each car.现在的想法是创建另一个页面来加载每辆车的全部内容。 The issue is that I have no idea how to do it.问题是我不知道该怎么做。 I have searched on the inte.net but i dont get any results and i am new to php.我在 inte.net 上搜索过,但没有得到任何结果,而且我是 php 的新用户。

MYSQL query would be something like; MYSQL 查询类似于;

SELECT * FROM table_name WHERE id = {$clicked-id}

where clicked id is the previous clicked car.其中 clicked id 是之前点击的汽车。 i guess i have to assign an id to each previous car title.我想我必须为每个以前的汽车名称分配一个 ID。

I am quite lost, can someone please help me.我很迷茫,有人可以帮助我吗?

Read the PHP manual , it's very helpful.阅读PHP 手册,很有帮助。

Basically the link should be something like:基本上链接应该是这样的:

<a href="/detail.php?id=<?= $car_id ?>"> car info .. </a>

So you pass the clicked id in the id parameter.因此,您在id参数中传递点击的 id。

Then, in your detail page, you retrieve it like this:然后,在您的详细信息页面中,您可以像这样检索它:

<?php
  $car_id = $_GET['id'];
  $query = 'SELECT * FROM cars WHERE id = '.intval($car_id);
?>

I'm using $_GET here because the parameter came in the URL. Again, check out the PHP manual , it's very helpful with this early stuff.我在这里使用$_GET因为参数来自 URL。再次查看PHP 手册,它对早期的东西很有帮助。

Note that I used intval to keep SQL injection off my back.请注意,我使用了intval避免 SQL 注入 You should research about it if you don't know what it means.如果你不知道它是什么意思,你应该研究它。 The basic idea here is to prevent a user from calling something like detail.php?id=1;DROP TABLE cars;这里的基本思想是防止用户调用类似detail.php?id=1;DROP TABLE cars;类的东西。 , which, if not using intval , would make your query: ,其中,如果不使用intval ,将使您的查询:

SELECT * FROM cars WHERE id = 1;DROP TABLE cars;

Which would destroy your data.这会破坏您的数据。

You want to look into $_GET variable in PHP. This "special" variable allows you to get parameters passed in the URL,你想查看 PHP 中的$_GET变量。这个“特殊”变量允许你获取在 URL 中传递的参数,

With this, you will be able pass parameters in the URL, like this:这样,您就可以在 URL 中传递参数,如下所示:

http://yoursite.com/showcar.php?id=5

And then, you can get the value of the specified id with $_GET['id'] .然后,您可以使用$_GET['id']获取指定id的值。

See more information here: http://www.php.net/manual/en/reserved.variables.get.php在此处查看更多信息: http://www.php.net/manual/en/reserved.variables.get.php

Okay, so here is the markup that is rendered from your second car on the list:好的,这是从列表中的第二辆车呈现的标记:

<table cellspacing="0" cellpadding="0" border="0">
    <tbody>
      <tr>
        <td valign="top" rowspan="3" style="padding: 8px 15px 0px 0px;"><img src=
        "car-images/2_main.jpg" class="car-list" /></td>

        <td valign="top" colspan="4">
          <h2>MG ZR</h2>
        </td>
      </tr>

      <tr>
        <td valign="top" colspan="4">3 Door Hatchback, Yellow, Petrol, Manual, Drivers
        airbag, 1 owner from new, Folding rear seats, Sports seats, Trip computer, Front
        electric windows, Alarm.service history,ABS,cd player,power steering,remote
        central locking</td>
      </tr>

      <tr>
        <td valign="bottom">
          <h4 class="car-info">Fuel</h4>Petrol
        </td>

        <td valign="bottom">
          <h4 class="car-info">Miles</h4>85000
        </td>

        <td valign="bottom">
          <h4 class="car-info">Year</h4>2006
        </td>

        <td valign="bottom">
          <h4 class="car-info">Price</h4>&Acirc;&pound;7500
        </td>
      </tr>
    </tbody>
</table>

Lets assume you want to click on the image to view the details.假设您要单击图像以查看详细信息。 You will need to wrap an anchor tag around the image, for example:您需要在图像周围包裹一个锚标记,例如:

...
<a href="deatils.php?carId=2"><img src="car-images/2_main.jpg" class="car-list" /></a>
...

That carId=2 needs to come from whatever code is generating your <div id="car-list"> markup. carId=2需要来自生成<div id="car-list">标记的任何代码。

Then of course you will need a page to receive that detail in the URL named details.php (per my example), and parse the carId=2 like this $id = $_GET['carId'] .然后,您当然需要一个页面来接收 URL 中名为 details.php 的详细信息(根据我的示例),并像这样解析carId=2 $id = $_GET['carId'] You can then query the database as you have stated using the $id variable.然后,您可以按照您使用$id变量声明的方式查询数据库。

When you click a car, PHP needs to know what car you clicked.当你点击一辆车时,PHP 需要知道你点击的是什么车。 For example, the overview page is at www.example.com/cars .例如,概览页面位于www.example.com/cars Make a link to the car detail page on every car: www.example.com/viewcar?id=1 .链接到每辆车的详细信息页面: www.example.com/viewcar?id=1 ?id=1。 Here is 1 the id of the car in the database.这是 1 汽车在数据库中的 ID。

On the viewcar page, you can get the id with $_GET['id'] .viewcar页面上,您可以使用$_GET['id']获取 id。 Use this value in your query:在您的查询中使用此值:

SELECT * FROM `cars` WHERE id = $_GET['id'];

NOTE Beware that this solution illustrates the concept, but should not be used because this is very vulnerable to SQL injection .注意请注意,此解决方案说明了概念,但不应使用,因为它很容易受到SQL 注入的攻击。

Simple.简单的。 When you build the link to the "Detail view" of a car, add a parameter, for example:当您建立到汽车“详细视图”的链接时,添加一个参数,例如:

<a href="detailview.php?carid=12">Detail view</a>

Now in the detailView.php, you can make the SQL Query using the paramenter, like this现在在 detailView.php 中,您可以使用参数进行 SQL 查询,如下所示

<?php
    $query = "SELECT * FROM table_name WHERE id = ".$_GET["carid"];
    ...
?>

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

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