开发者

How to create member 'profiles' in PHP?

开发者_JAVA技巧

What is the best way to create member profiles in PHP? For instance, some websites read in the address bar: profile.php?id=11233 and then profile.php?id=13563. How is this actually done? As of now, I am saving such types of URL's in MySQL, but where will I write the actual code? Will I have to write the code in profile.php? Or will I have to make separate files for each id? How does it work?


Will I have to write the code in profile.php?

Yes. Visitors will be visiting profile.php and there will be a default variable set named $_GET['id'] that has the value in the URL, like 11233 or 13563. You can then query the database for the user with that id and display the proper information.


If there is more than one variable, like profile.php?id=123&type=cake, then you will have two variables: $_GET['id'] = 123, $_GET['type'] = 'cake'. It is stored in $_GET because GET is the method used to access the page. Find out more at http://php.net/get
There is also another common method, called POST. This is used when forms are submitted with method=POST. In that case, the information will be stored in the $_POST array.


Yes, in those example URLs, the database id is being sent as a parameter which would be parsed by PHP.

The basic steps would be

  • get database id (from what's called the $_GET superglobal in this case). Make sure it's an integer, using intval() or (int)
  • set up your database connection (I use PDO)
  • build a query, something like select * from profile where id=?.
  • execute the SQL query
  • check your results
  • if you have valid results, print out the information in the profile

The details of how to do each of those steps is the meat of the matter, of course. I'm sure folks around here would be more than happy to answer questions about how to connect to and set up a database, and use the results in PHP.

A simple example database would be set up something like

CREATE TABLE profile(
   id int NOT NULL AUTO_INCREMENT,
   user_name varchar(32),
   bio varchar(1024),
   favorite_flavor enum('chocolate','vanilla','fruity'),
   image_id int default null,
   primary key(id)
   );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜