简体   繁体   中英

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.

MYSQL query would be something like;

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

where clicked id is the previous clicked car. i guess i have to assign an id to each previous car title.

I am quite lost, can someone please help me.

Read the PHP manual , it's very helpful.

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.

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.

Note that I used intval to keep SQL injection off my back. 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; , which, if not using intval , would make your query:

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,

With this, you will be able pass parameters in the URL, like this:

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

And then, you can get the value of the specified id with $_GET['id'] .

See more information here: 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.

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'] . You can then query the database as you have stated using the $id variable.

When you click a car, PHP needs to know what car you clicked. For example, the overview page is at www.example.com/cars . Make a link to the car detail page on every car: www.example.com/viewcar?id=1 . Here is 1 the id of the car in the database.

On the viewcar page, you can get the id with $_GET['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 .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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