Help with passing id and field info?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imimin
Forum Commoner
Posts: 38
Joined: Thu Oct 18, 2007 5:44 pm

Help with passing id and field info?

Post by imimin »

Could someone please explain to me how to pass the id through a url so on the "receiving" page I can echo the data in the field of the id ?

Thank you!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help with passing id and field info?

Post by requinix »

Code: Select all

<a href="page.php?id=123">Link</a>

Code: Select all

<?php // page.php
 
echo $_GET["id"]; // 123
imimin
Forum Commoner
Posts: 38
Joined: Thu Oct 18, 2007 5:44 pm

Re: Help with passing id and field info?

Post by imimin »

tasairis wrote:

Code: Select all

<a href="page.php?id=123">Link</a>

Code: Select all

<?php // page.php
 
echo $_GET["id"]; // 123
How would you point to the correct field with this code?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help with passing id and field info?

Post by requinix »

What field?
imimin
Forum Commoner
Posts: 38
Joined: Thu Oct 18, 2007 5:44 pm

Re: Help with passing id and field info?

Post by imimin »

Any field I want to specify.
imimin
Forum Commoner
Posts: 38
Joined: Thu Oct 18, 2007 5:44 pm

Re: Help with passing id and field info?

Post by imimin »

If I run this code like it is, it 's not pointing to a specific field, just a row, right? Or am I missing something?

Thanks for your help, just trying to learn.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help with passing id and field info?

Post by requinix »

Nothing is pointing anywhere. $id is just a number - you do whatever you want with it.

Have you read through the PHP manual recently?
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Re: Help with passing id and field info?

Post by Skara »

I think you're asking how an html form connects to php?

Code: Select all

<input type="text" name="somefield" />
when ^ is submitted, the url will be file.php?somefield=what_you_type

Code: Select all

echo $_GET['somefield'];
imimin
Forum Commoner
Posts: 38
Joined: Thu Oct 18, 2007 5:44 pm

Re: Help with passing id and field info?

Post by imimin »

What I am trying to do is send the id of the product to the next page and in that page load the product description (desc). What I have so far is:

Code: Select all

   <?php
         $cat = $_GET['cat'];
         $get_items = "SELECT * FROM poj_products WHERE cat='$cat'";
         $get_items = mysql_query($get_items);      
 
echo '<a href="'.$sitelocation.$item_row['url'].'?item_desc='.$item_row['id'].'>view details/order</a>';
 
?>
but url link is not echoed???
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help with passing id and field info?

Post by requinix »

Where did you (not) get $item_row from?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Help with passing id and field info?

Post by superdezign »

imimin wrote:but url link is not echoed???
No echo at all? If so, then your code is never reaching that area.

If that's not what you mean, then you are not using mysql_query() correctly. The function mysql_query() performs a query which gives you a handle to the results of the query, not the results themselves. You must use that handle in order to retrieve your data. This can be done with mysql_fetch_*() functions, where * is "array", "assoc", "object", etc. The PHP manual should tell you all that you need to know in regards to how those functions work.

PHP does not magically create variables for you. $item_row needs to be initialized to have any value beyond NULL. I'm assuming that you are looking for this:

Code: Select all

$item_row = mysql_fetch_assoc($get_items);
Also, your code is vulnerable to SQL injection, as you do not validate $_GET['cat'] before putting it into your query. The user could input any value at all into a query variable, and you must account for that.
Post Reply