如何创建会员“个人资料”在 PHP 中?

发布于 2024-09-09 08:03:17 字数 196 浏览 11 评论 0原文

在 PHP 中创建会员资料的最佳方法是什么?例如,某些网站在地址栏中读取:profile.php?id=11233,然后读取profile.php?id=13563。这实际上是如何完成的?截至目前,我正在 MySQL 中保存此类类型的 URL,但是我将在哪里编写实际代码呢?我必须在 profile.php 中编写代码吗?或者我必须为每个 id 制作单独的文件?它是如何运作的?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吲‖鸣 2024-09-16 08:03:17

我必须把代码写在
profile.php?

是的。访问者将访问 profile.php,并且会有一个名为 $_GET['id'] 的默认变量集,其值位于 URL 中,例如 1123313563。然后,您可以在数据库中查询具有该 ID 的用户并显示正确的信息。


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.

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.

醉殇 2024-09-16 08:03:17

是的,在这些示例 URL 中,数据库 ID 作为参数发送,由 PHP 解析。

基本步骤是

  • 获取数据库 ID(在本例中来自所谓的 $_GET 超全局)。确保它是一个整数,使用 intval() 或 (int)
  • 设置数据库连接(我使用 PDO
  • 构建一个查询,例如select * from profile where id=?
  • 执行 SQL 查询,
  • 检查结果
  • (如果有有效结果),打印出配置文件中的信息

当然,如何执行每个步骤的详细信息是问题的核心。我确信这里的人们会非常乐意回答有关如何连接和设置数据库以及如何在 PHP 中使用结果的问题。

一个简单的示例数据库将被设置为类似

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)
   );

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)
   );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文