Jump to content

[SOLVED] Get data by id


demonforce

Recommended Posts

Hi, i am working for a school project atm.

 

It is that i need to keep up with a log each day, so i decited to make it in php.

 

the index is simple, it shows the id from the action, name, date, and a short description.

 

now i want that when i clock the name, i go to a page where you can see more information about it.

 

how can i make it like show.php?=ect....

 

i searchet the internet, but i dont understand it. if someone could help me with it, i bould be verry thanksfull :)

 

 

greetz demonforce

Link to comment
https://forums.phpfreaks.com/topic/67259-solved-get-data-by-id/
Share on other sites

i guess u want a profile type thing?

 

make ur url like show.php?ID=#

 

in your code, use the $_GET function, to retrieve that number, and your mysql queries should be like, SELECT * From `tablename` WHERE id=ID

the code :

$result = @mysql_query("SELECT * FROM users WHERE id= ID");

$temp = @mysql_fetch_object($result);

$user = $temp;

 

now, you look at your columns in ur mysql,

each value can be called up to the screen by doing this:

 

echo"$user->ID"; would echo their ID, $user->name is their name, etc

Link to comment
https://forums.phpfreaks.com/topic/67259-solved-get-data-by-id/#findComment-337396
Share on other sites

To expand what uwannadonkey said, im giving a simple example:

 

index.php

<?php
$query = @mysql_query("SELECT * FROM logs") or die(mysql_error());
$counter = 0;
while($values = mysql_fetch_array($query)){
     echo $counter . " <a href=\"logs.php?id={$values['id']}\">{$values['name']}<br />";
     $counter++;
}
?>

 

logs.php

<?php
if(isset($_GET['id'])){
     if(!is_numeric($_GET['id'])){
           die('The page doesnt exists');
     }
     $id = mysql_real_escape_string($_GET['id']);
     $query = @mysql_query("SELECT * FROM logs WHERE id='$id'") or die(mysql_error());
     $values = mysql_fetch_array($query);
     echo $values['name'] . "<br />";
     echo $values['action'] . "<br />";
     echo $values['date'] . "<br />";
     echo $values['desciption'] . "<br />";
}
?>

 

Its a simple example which shows what u want to achieve. In index.php i showed all the records of the table where each record is a html anchor that links to logs.php and passed its id in a url variable. In logs.php i used GET to retrieve that variable, sanitized it and made a query filtered with only that id. Guess its a lot more clear now...

Link to comment
https://forums.phpfreaks.com/topic/67259-solved-get-data-by-id/#findComment-337445
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.