lobfredd Posted May 11, 2012 Share Posted May 11, 2012 Hello On my current site i have a seperate php file for each product with a WHERE ID=X clause. How do i put the where clause in the URL? (if that is how its done) so that i only need one file Thanks Quote Link to comment https://forums.phpfreaks.com/topic/262398-php-url-field-commands-like-phpproductidx/ Share on other sites More sharing options...
scootstah Posted May 11, 2012 Share Posted May 11, 2012 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'"); Quote Link to comment https://forums.phpfreaks.com/topic/262398-php-url-field-commands-like-phpproductidx/#findComment-1344748 Share on other sites More sharing options...
lobfredd Posted May 11, 2012 Author Share Posted May 11, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262398-php-url-field-commands-like-phpproductidx/#findComment-1344754 Share on other sites More sharing options...
scootstah Posted May 11, 2012 Share Posted May 11, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262398-php-url-field-commands-like-phpproductidx/#findComment-1344758 Share on other sites More sharing options...
lobfredd Posted May 11, 2012 Author Share Posted May 11, 2012 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/262398-php-url-field-commands-like-phpproductidx/#findComment-1344765 Share on other sites More sharing options...
scootstah Posted May 11, 2012 Share Posted May 11, 2012 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 ) 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 } Quote Link to comment https://forums.phpfreaks.com/topic/262398-php-url-field-commands-like-phpproductidx/#findComment-1344785 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.