Jump to content

php URL field commands, Like .php?productID=X


lobfredd

Recommended Posts

You'll have to construct your links to contain the querystring. When you receive items from the database, you'll have to loop through and dynamically create the links so they have the proper ID.

 

To use the querystrings from the URL you'll need to use the $_GET superglobal array. The key will be whatever you put in the URL. For example if your link is http://example.com/products.php?id=7 then you would access it with $_GET['id'].

 

Make sure, though, that you NEVER allow user input in your database queries. For example this is bad:

SELECT * FROM products WHERE id=$_GET['id'];

 

You always need to escape your user input to prevent SQL injection. In the case of numerical ID's, though, we can cheat a little by simply casting the value to an integer. Any bad characters will simply be removed, and you don't have to worry about it. So this would be okay:

$id = (int) $_GET['id'];

$result = mysql_query("SELECT * FROM products WHERE id='$id'");

Link to comment
Share on other sites

Thanks, got it working!

 

At last i want something like this as an Add to cart button.

I have a query which lists the products in my product table.

Currently i have a seperate php file to add something to the cart for every product..

I want something like this: add.php?id=x, and then it adds the product from my product table to another table (my cart table), Like copy whatever row with that specific id to another table (not all the colums ofc).

 

Thanks.

Link to comment
Share on other sites

It would be as simple as running the INSERT query on the add.php page, using the ID from the query string.

$id = (int) $_GET['id'];

mysql_query("INSERT INTO cart (user_id, product_id) VALUES (1, '$id')");

 

That's the gist of it. You would want to make sure that $_GET['id'] is in fact populated, that the product actually exists, and that it's not already in the cart.

Link to comment
Share on other sites

I want it to actually add the name and price of the product into my cart table based on whatever product got x as id in my product table.

or can i just bind the id to the title and price from my cart.php page? (it is just a query listing whatever that is in the cart table based on user logged in)

If so, how? and how do i check if the product actually exists? (sorry just wanna learn :P)

Link to comment
Share on other sites

I want it to actually add the name and price of the product into my cart table based on whatever product got x as id in my product table.

 

You shouldn't do that because then you are duplicating data in your database. What if you change the price? Then you have to update it in a whole bunch of places, which is a lot slower. Instead, you can just store the product ID - from there you can use a JOIN to reference the products table based on the product ID.

 

As a rough example let's say you have these three tables:

products
-------------
id : int(11) : unsigned : primary key : auto_increment
name : varchar(70)
price : float


users
-------------
id : int(11) : unsigned : primary key : auto_increment
username : varchar(30)


cart
-------------
user_id : int(11) : unsigned : primary key
product_id : int(11) : unsigned : primary key

 

So you see, the "cart" table just holds two ID's: one for the user, and one for the product. This way if you update the product the changes take place anywhere that the product is referenced from the "cart" table - and you only need to update it in one place.

 

You could take it a step further (if you are using INNODB) and use Foreign Keys in the cart table. Foreign Keys will allow you to cascade changes to the referenced table. For example, if you delete a product from the "products" table, that item will automatically be removed from the "cart" table.

 

When you want to see the price and such you will use a SQL JOIN to get the "products" and/or "users" tables.

 

and how do i check if the product actually exists? (sorry just wanna learn :P)

 

You would run a SELECT query to the "products" table with the product ID in a WHERE clause and then count the returned rows. Since the ID is unique, you should get one row returned. If you get no rows returned you know that the product does not exist. You can do this with the mysql_num_rows function, or, use a COUNT() query. Either of these will work:

 

$id = (int) $_GET['id'];

$result = mysql_query("SELECT id FROM products WHERE id='$id'");

if (mysql_num_rows($result) == 1) {
// the product exists
}

 

$id = (int) $_GET['id'];

list($num_rows) = mysql_fetch_row(mysql_query("SELECT COUNT(id) FROM products WHERE id='$id'"));

if ($num_rows == 1) {
// the product exists
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.