Jump to content

[SOLVED] Using IDs


whiskedaway

Recommended Posts

I'm really hoping someone can help me. I have been searching for a few days on information regarding IDs in PHP -- that is, when you get a URL that looks like /index.php?id=46 or whatever the ID number is.

 

Basically, what I want to do is call a particular MySQL statement depending on its ID within the database, but I only want to use one page to call each different one. I'm assuming that you do it using this ID function, but I can't find anything that tells me how to do it. Everytime I do a search throughout forums and search engines, I come back with a million entries that are referencing web addresses that have nothing to do with what I'm searching for, or parts of words, such as slendid. Can anyone point me in the right direction, or explain to me how it works?

 

Thanks so much in advance.

Link to comment
https://forums.phpfreaks.com/topic/125785-solved-using-ids/
Share on other sites

No you won't, it's know as a GET statement (from a form is a POST). Basically anything after the ? is passed a a key value pair to the php interpreter, you can have multiple sets by delimiting with &. You can call the variables whatever you want, id is just easily descriptive (honestly). So:

 

<a href="login.php?name=me&pass=password">login</a>

 

not the best example but it illustrates the point. Next you need to get the variable, it's held in an array called $_GET (see reserved variables).

 

if(isset($_GET['name']))
{
echo "name: ".$_GET['name']."<br>";
}

if(isset($_GET['pass']))
{
$pass = $_GET['pass'];
echo "pass: ".$pass."<br>";
}

Link to comment
https://forums.phpfreaks.com/topic/125785-solved-using-ids/#findComment-650433
Share on other sites

well, someone answered, but I hate to waste code:

<?php
/* Example only. You'll need to re-work to suit your needs */
if (isset($_GET['id']) && is_numeric($_GET['id'])){
$id = $_GET['id'];
$sql = "SELECT * FROM `table_name` WHERE `id`='$id' LIMIT 1;";
}
else{
$id = null;
$sql = null;
}
if ($id != null && $sql != null){
//do the query, manipulate the data, etc, etc, etc.
}
else{
//redirect them to the search/whatever, because they didn't pick an ID, or they
//tried scripting against you.
header("location:index.php");
exit();
}

Link to comment
https://forums.phpfreaks.com/topic/125785-solved-using-ids/#findComment-650434
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.