Suppose I have a website
www.game.com
Now on this website there is a list of players and when I click on a specific player it takes me to:
www.game.com/stats.php?id=1
Say I go back and click a different player and it takes me to
www.game.com/stats.php?id=2
I need a way for parse_url to extract the values '1' and '2' uniquely when those players are clicked.
If i type out parse_url('http://www.game.com/stats.php?id=2')
This will not work. I need 1 statement that works dynamically to extract that id number and store it in a variable on the stats.php page whenever a players name is clicked.
When a new stats page is loaded the variable is just updated with the new ID and information from my database can be pulled by it.
Thoughts?
These pieces of information appended to the URL are called GET parameters. In PHP, you can access them using the $_GET array:
$id = $_GET['id'] // -> 1 or 2
or you can use $_REQUEST, which includes GET and POST parameters and cookies.
Both of these arrays are superglobals, so you can use them in functions without declaring them as global.
Use PHP GET:
$id = $_GET['id'];
http://www.php.net/manual/en/reserved.variables.get.php
Related
Say I have two files, fileone.php and filetwo.php.
fileone.php generates a list of links, items from a mySQL database, every one of these items have an unique integer ID (1,2,3,4...).
filetwo.php contains a PHP function that uses this ID to create a page displaying the item you clicked in fileone.php. Depending on what item you click in fileone.php you will see a different image, text and more in filetwo.php.
My problem:
How do I at all send the ID from fileone.php to filetwo.php? Do I use forms with the $_GET variable? This is purely a hypothetical situation and I have no code to show, I just need input on how to approach this.
Yes, you could just pass the ID via $_GET
fileone.php...
1
2
3
filetwo.php
if(isset($_GET['id']))
{
//do somethign with $_GET['id']
}
You should look into PDO or mysqli to receive and sanitize the input. do NOT use mysql_ functions as they are deprecated.
Yes, you should use $_GET variable.
Your HTML code in fileone.php will look something like this:
Item 1
Item 2
Item 3
And in filetwo.php you get itemId value with $_GET['itemId'] and output image, text, etc.
Yes, depdending on the sensitivity of the information and wether you care if users may alter the address to see a different id or not, you would use $_GET, otherwise $_POST, and if you are dealing with very sensitive stuff like user states or a shopping cart, then use $_SESSION.
So, for $_GET you would have a link to filetwo.php?id=3
Over on filetwo.php you would connect to the database based on $_GET['id'] and develop your view like that.
I have created a php site, and previously it was listing only products with defined values. I have now changed it to include an array of products for example all products WHERE id = "spotlights"
and this works great so it means I can add new products just to the database, but I still have to add the second page manually. e.g going from the product div on the main page, through to www.example.com/spotlight_1.php
Is there anyway in PHP to carry the data from my index.php e.g. the ID through to the next page? so that I can have a template product.php page, and I can use a database pull to echo the product information required.
So on index.php i click on the product with ID="1" and on the product.php page, it loads the relevant data for product 1.
I can write the php SQL/mySQL calls myself, its just the way to carry accross a value from the previous page which I dont understand
Regards
Henry
p.s.
all the IDs and things are stored in the database already as 1 to 3digit values e.g. 3 or or 93 or 254
Any advice as always is greatly appreciated
Regards
Henry
im not sure if im understading this correctly but you can pass the variable from one page to another using GET variables, or in other words, using the query string in the URL.
So, in index.php you will have links like this:
Product 1
In the second page (product.php) you can get this variable using this code:
$product_id = $_GET['p'];
And then query your database like this:
$query = "SELECT * FROM products WHERE id = '" . $product_id . "'";
$result = mysql_query($query);
Reference: http://www.w3schools.com/php/php_get.asp
Note: Be careful with the way you query your database, the previous code is only a demonstration of how to retrieve the info, but is not a secure solution. I recommend you to check PDO (http://php.net/manual/es/book.pdo.php).
You can use GET if you do not care other users to see your variables, or you can use POST to not let the users what your variables are (useful for password submissions)
One thing to mention is that if you use GET (url?key=value) you need to encode the value using PHP's utf8 enconde function, if you use POST, you don't have to worry about this.
There are a number of ways. You can put the data into session variables, you can POST the data to the 2nd page or pass the data via GET as URL parameters, or you can even save the data in a browser cookie.
Which approach you might use will likely depend on the security requirements for the data and whether you want to be able to access that data strictly via URL (in case of URL parameters).
You can either keep passing the value forward in the request scope until you no longer need it (commonly passed in the url ?var=value), or you can also use sessions if this needs to live longer than the request scope.
I'm trying to create unique urls for user's of a site I'm designing. Basically I want the url to look like this:
http://www.example.com/page.php?user_id=3
Or whatever user_id they are assigned. Basically I want user to be able to go to that url and it generates the page with the info (first name, last name, etc.) of the user from the row in the MySQL table with that user id number. I know that creating the URL would be done by $_GET but this is sort of backwards from that. I want them to be able to go to the URL and the pages gets the variables from the URL.
Sorry if this is basic, I'm having trouble searching for an answer because it always gives me the answer on how to submit a form TO the URL not FROM the URL. Thanks for all the help.
$user_id = $_GET['user_id'];
you could use this to get the variable.And then search the database for the matching id and get get all the details.
Since you want to contain all the details in you form.You could keep adding the other elements like this.
http://www.example.com/page.php?user_id=3&first_name=Max&last_name=Payne
Using $_GET[which element you want the value of]
Accessing the get variables on the page is really simple!.In your case as you want to access the user id,you can get from $_GET['user_id'] and fire the respective query to fetch the user details to populate the page.For more reference you could check here
http://php.net/manual/en/reserved.variables.get.php
I have access for only to a front end of a webpage.
This webpage has fields like
Name
Age
Number
For the above i have to update Number field.
Similarly there are hundreds of similar webpages with the these fields which are needed to be updated.
For example URL of this webpage is https://stackoverflow.com/id=xx
In the URL the value xx of the 'id' is unique for each webpage.
I have these values xx of the 'id' and the corresponding age which needs to be updated in the webpages iteratively and be saved.
What are ways for doing the above?
have them displayed in a single page or paginate it instead of having to view them one per page, one item per line, put the values that would be changeable in a input type=text with a corresponding submit button beside each.
well you could get url parameters by using $_POST['id'], $_GET['id'] or $_REQUEST['id'] but try to validate the values first to avoid sql injections.
if you want to get the whole url itself you could use
$_SERVER['SERVER_NAME']
$_SERVER is default variable on php it has a lot of uses just try to visit this link http://php.net/manual/en/reserved.variables.server.php.
I hope it helps. :)
I have a form which collects user info including name, location, url, email address etc etc AND company logo AND appropriate catagories they wish to be associated with (using a checkbox array in $_POST).
I then display a preview of the page (using $_SESSION and hidden form fields to hold the $_POST data) which their data will appear on BEFORE storing it in MySQL with an 'approve' button which links to the PHP page which has the MySQL INSERT query.
The problem i'm having is the preview page in between entry and submission to the database is having trouble with the logo input and the checkbox array input:
$_SESSION['clogo'] = $_POST['clogo'];
$_SESSION['cat[]'] = $_POST['cat[]'];
I get an 'undefined index' error when i try to preview these, however all other standard input is fine, and is then passed along to the insert mysql query on user approval. In addition, if i skip the 'preview page' and go straight to the insert PHP file from the original form, there is no problem, and all data is entered into the database correctly.
Can anyone help
You don't want this:
$_SESSION['cat[]'] = $_POST['cat[]'];
But this:
$_SESSION['cat'] = $_POST['cat'];
I.e., 'cat' is the index of the _POST array, and the element that it refers to is another array.
I'm guessing $_POST['cat'] is an array in itself. (I'm guessing this because of the [] following cat in your example.) If this is the case, then in your example, you're setting $_SESSION['cat[]'] equal to a variable name that doesn't exist. Try setting $_SESSION['cat'] equal to $_POST['cat'].
Remember to start the session using the start_session function. Otherwise nothing will be saved in the session.
Also worth noting is that when using some third party application they might remove all session variables. Wordpress is a good example.
$_POST[] array must be re-initialize, as you are posting to preview page and then php insert script page...
When you go directly from form to php script page ,POST have variables in it, so it must be working fine.
'cat[]' would not be the index. The index would be 'cat' which should be an array.