Populating PHP page with MySQL info from a variable in the URL - php

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

Related

How to display page according to the value which is typed in URL?

My existing URL is:
https://example.com/folder-name/file-name.php?id=16(dynamic)
On the basis of above URL, I am displaying a page of a particular user according to the id.
file-name.php- In it, I have written a database queries on the basis of which I am displaying user details.
Now, I want the URL as:
https://example.com/name/age/listen(static value)/
Name and age is dynamic value
By accessing the name and age from URL, I want to display particular details by fetching from database.
You can get the parameters from URL by using $_SERVER[QUERY_STRING]. It will return all the parameters which you typed after site_url. Then you can use explode function to get parameters in an array and used it further for database queries.

PHP Rewrite URL or Mask or Redirect?

I'm trying to get domain.com/employee?display_county=sd&dept=sales&id=23 to display in the URL as domain.com/firstnamelastname while retaining the URL variables.
And also if someone goes directly to domain.com/firstnamelastname it would still have the original URL variables.
How would one accomplish this?
Edit: OK, so I'm starting to rethink this...
Let's say I have a page that displays a list of users.
Each user has a clickable link that directs to domain.com/employee?display_county=XX&dept=XXXXX&id=XX
How could I get the URL to display domain.com/firstnamelastname when a user is clicked and still bring up the correct user?
I was thinking maybe using a POST method to pass the id to the script that displays the user and then rewriting the URL to display the firstnamelastname related to the id, but then browsing directly to domain.com/firstnamelastname would leave the id variable empty...

How to create new webpages automatically based on user input?

This question may have been asked before but I couldn;t really find it.
What I want to do is, websites like pastebin.com, even stackoverflow, these generate new webpages based on user input showing that data from what I can understand.
I want to make it like that. User enters something, and he is given a permalink to share that information.
How to do this using PHP ?
EDIT: Here is an example
Like, I want it that
instead of having something like www.example.com/view.php?id=123
I want it like
www.example.com/view/123
This is impossible to be done with PHP, you can do this with .htaccess's url rewriting.
a good article about this can be found at: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
In this site , work some ajax function, when you add some data in textarea and click submit, called function , that saved current data in db, and return some json data , and then show you in some format.
Simply done like this:
Create a index.php containing a input field where the user inputs their data.
Post data to the server and create a random string which will be the users permalink.
Then insert the random string and data into a database.
In the index.php file say that if any uri is added after your domain like example.com/
it checks against the database if this code exists in db, and returns the result to the visitor.

php pages in mysql link without id

How I add pages for my website without create files for any page I want. To make understand let's say I need a details information page to read from mysql the rest of informaton of a specific ID BUT! in link i don't want to be with id
(site.com/details-product-idnumber)
i want to be only
(site.com/details-product-nameofproduct)
I know basicly this need to be rewrite from .htaccess but what I need to know about this function/system.
Another example is wordpress blog platform, when you add a post, in link it only the title of the ID post from mysql, what php code i need to use or what's the name of this function. Thanks.
may be you can use a unique_name (in your application a unique name for a product).. in your database table.. and just pass that unique_name in the link..
like..
site.com/details-product-unique_name
and your sql query should be like..
select * from product where unique_name = '$_GET["your_get_field"]';
let me know if you want any further guidance...

Is there anyway to carry a value in php forward to a second page?

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.

Categories