doujia6503 2013-01-18 09:19
浏览 41
已采纳

如何让多个用户访问后端并保存数据? [关闭]

I'm hoping someone can help explain this process in layman's terms, since I'm still "new" and learning.

I've built a basic content management system with a backend, and I'm trying to create a system where dozens of users can access a service, save data to their account, and recall that data at a later time if they choose.

Thus far I've got the login, registration, and forms (to save the data) complete. I'm just at a point where I don't know how to save this data in MySQL (using PHP by the way) so that dozens of users can all save data vs. just one.

Example being: Dozens of users access WordPress and can create posts, while not being able to see the other users posts on their account.

I understand how to setup one account to save data, just not multiple.

Any advice would be greatly appreciated.

P.S. If anyone has any guides/tutorials/recommended articles to read, I'd be incredibly grateful!

  • 写回答

1条回答 默认 最新

  • douxin8610 2013-01-18 09:31
    关注

    I think you are missing a fairly important part in how PHP works.

    Each separate user uses a different instance of your application. For example, if I enter and login into the application, and you do the same, we will have (for example), the variable $_SESSION set, but I will have $_SESSION['idUser'] = 3, and you will have $_SESSION['idUser'] = 5. They will not know of each others existence.

    So basically, you need to think your application for a single user, with the appropriate restrictions. Meaning that, for example, if you want to create the edit profile page, you would use $idUser = $_SESSION['idUser'] in your SQL queries to select/update/insert the appropriate information.

    Here's a sample query:

    // view users profile page
    $idUser = intval($_SESSION['idUser']);
    $query = "SELECT * FROM `users` WHERE id = {$idUser}";
    
    // edit profile checks/database update
    $idUser = intval($_SESSION['idUser']);
    $userData = $_POST['user']; // let's say all your inputs have a name="user[...]"
    // !!! validation for the user data !!!
    $query = "UPDATE `users` SET col1='{$userData[col1]}', col2='{$userData[col2]}' WHERE id = {$idUser}";
    

    Hope this helps, let me know if you need further clarifications.

    PS: Don't forget to properly escape your data before using it in SQL queries, and use either mysqli or PDO extensions (do NOT use mysql, since it is deprecated, and will be removed in a future version of PHP).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入