Webmaster Key - Discussion Forums


Welcome, Guest. Please login or register.
Did you miss your activation email?
February 09, 2012, 06:23:22 AM

Login with username, password and session length
Welceome to Forums!

Important information for guests and new members:

In order to understand the full benefits of becoming an active member of this forum, please review the following information on guest and new member restrictions. These forum changes have been prompted by an overwhelming and unreasonable amount of bot postings and incoherent guest spam messages. We wish to prevent these events from happening in the future and make our community a more comfortable place for all of our members.

For guests:

Guests are not allowed to open new topics, polls, or posts attachments.
If you wish to open up new discussions on this forum, we encourage you to register.

For new members:

New members with less than five posts are not allowed to modify additional profile information such as avatars, contact information, biographies, and signatures. However, new members are encouraged to post their own topics or reply to topics initiated by other members. Become active on the forums and 5 posts should be an easy task!

We are a diverse community with members from all over the world. We encourage new ideas and interesting conversation. Do not be afraid to post webmaster/computer-related questions or problems, as our active members are always willing to help when they are able. Interested? Join us.

+ Webmaster Key Forums
|-+ Webmaster Corner
| |-+ Site Design and Web Authoring
| | |-+ Coding Talk
| | | |-+ get method in the query string
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] Go Down Stumble Upon! Digg It! del.icio.us! Add to Technorati! ReddIt!  Send this topic Print
Author Topic: get method in the query string  (Read 3296 times)
stu
Key Keeper
Member
**
Posts: 83



WWW
« on: August 05, 2009, 12:29:09 PM »

Hi

I have been trying to write a simple script that can retrive products from the database on the index.php page and then pass these via the query string to show the product details on the items page item.php. I want to use one item.php page to show the details. I know this can be done but i'm not sure how. I have tried reading up on this but not getting very far. I have wrote roughly what i have. I wounder if someone could help me a little. Once i have the basic principle i will be able to modify the rest to meet my needs but i do need to get the basics working first.

Thank you in advance for your help.


Index.php

// This is the main page that will call the products name and id from the database table.


<?php
// Create the connection and select the DB
$link = mysql_connect("localhost","username","password");

if ($link) {
   mysql_selectdb("database",$link);
    }

$qry = "SELECT * FROM products ORDER BY name";
$result=mysql_query($qry);
while ($newArray = mysql_fetch_array($result)) {
$id = $newArray['id'];
$name = $newArray[name];
echo "<a href='item.php?$id'>$name</a>";
echo "<br />";
}
?>

// the above code should now list the products in the database in name order
// this should look like this in the page
// product 1
// product 2
// product 3
// each product will be a link to the item.php page and contain the product id in the query string

item.php


// this page would now be the page to can use the item id that has been passed in the query string to find the
// product information from the database

<?php
// Create the connection and select the DB
$link = mysql_connect("localhost","username","password");

if ($link) {
   mysql_selectdb("database",$link);
    }

$result = mysql_query("SELECT id, name, price FROM products WHERE id='id passed via query string'");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row = mysql_fetch_row($result);   

echo"$row[0]";
echo "<br />";
echo "$row[1];
echo "<br />";
echo "row[2]";

?>


thats the code i have so far how do i get the product id form the query string? I know this can be done

Report to moderator   Logged

Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #1 on: August 06, 2009, 11:07:53 AM »

Hi Stu,

you should assign an array key to the value that you pass in the query string (don't just pass a value). This is good coding practice I think.

So in your index page code have:

Code:
echo "<a href='item.php?pid=$id'>$name</a>";

Here I have added the key "pid" which helps me recognise that it refers to a Product ID.

Then in your item.php code you retrieve the PID as follows:

Code:
$pid = $_GET['pid'];
if (!isset($pid)) die("No product ID was specified!");
$pid = (int) $pid; // Force the ID value to a type of Integer to avoid problems of SQL injection attacks

The query string values are placed into a global array called $_GET. And in PHP, an array is an associative array comprised of key->value pairs so it's best to specify a key in this case for easy coding and easy debugging.
Report to moderator   Logged

Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #2 on: August 06, 2009, 11:17:36 AM »

p.s. here is a cool way to display the contents of an array when you are debugging PHP scripts:

Code:
<pre>
<?php
print_r
($a);
?>

</pre>

This HTML + PHP code prints out the contents of the array $a to the web page. But delete the code from your site as soon as you finish debugging.
Report to moderator   Logged

stu
Key Keeper
Member
**
Posts: 83



WWW
« Reply #3 on: August 09, 2009, 08:35:26 AM »

Hi


Thank you very much, works a treat, Now looking back at the scripts that were sort of showing this i understand them. Its always they way,

Again thank you.

Report to moderator   Logged

Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #4 on: August 09, 2009, 10:01:01 AM »

No problem  Grin

Very often, a tiny thing can hold us back from major progress!
Report to moderator   Logged

stu
Key Keeper
Member
**
Posts: 83



WWW
« Reply #5 on: August 09, 2009, 03:54:29 PM »

Hi

Is there a way to pass two varibales in the query string?

i've tried a few ways but none seem to work, I think its ment to be like this

echo "<a href='item.php?pid=$id;cat=toys'>$name</a>";

then added the extra line on the item page to retrive the value (line 2)

$pid = $_GET['pid'];
$cat = $_GET['cat'];
if (!isset($pid)) die("No product ID was specified!");
$pid = (int) $pid; // Force the ID value to a type of Integer to avoid problems of SQL injection attacks


sorry to be a pain.

thanks
Report to moderator   Logged

Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #6 on: August 10, 2009, 01:02:04 AM »

Code:
echo "<a href='item.php?pid=$id&cat=toys'>$name</a>";

Use an ampersand character to separate the items.
Report to moderator   Logged

Pages: [1] Go Up Stumble Upon! Digg It! del.icio.us! Add to Technorati! ReddIt!  Send this topic Print 
+ Webmaster Key Forums
|-+ Webmaster Corner
| |-+ Site Design and Web Authoring
| | |-+ Coding Talk
| | | |-+ get method in the query string

Jump to:  
« previous next »


Our Partners
RelmaxTOP Ranking System Web Hosting RelmaxTOP Ranking System
Staff Sites
12Noon[12Noon Gallery] Andy[Urgentclick]
Tamuril[Tamuril's Digital Art Exhibit] Sensovision
Powered by MySQL Powered by PHP We are hosted by Relmax Inc. |Our Privacy Policy | Sitemap
Powered by SMF 1.1.9 | SMF © 2006-2009, Simple Machines LLC
Forum design by Tamuril © 2005.
Valid XHTML 1.0! Valid CSS!