dqc22586 2014-12-19 18:30
浏览 59
已采纳

如何使用数据库在PHP脚本之间传递变量

I have 1 HTML document (it is .php, but I'll refer to it as HTML doc) with 3 different fill-in-forms which each has their own PHP script for inserting the data into a database. Each form has its own script and its own table in the database.

I need an ID or username I can give the user on the first form's submission, and then store the ID/username variable and insert it into form 2 and in form 3. So what I need is to have a variable pass from one script to two other scripts.

I tried giving a ID/username in the first script, then in the second script I just collect it via SQL from the table. It doesn't seem to work.

Script 1: $personID = 'a1';

Script 2: $today = date("Y/m/d"); $personID = mysql_query("SELECT personID FROM reviewpi WHERE date >= '$today' ");

Please help?

  • 写回答

2条回答 默认 最新

  • dongshanjin8947 2014-12-19 19:06
    关注

    Your query tries to get the ID, based on today's date. That is not a good approach at all, what if you had multiple persons that day?

    Since you are just trying to carry the id over from page to page, a session would be the right tool.

    Set the session:

    session_start(); 
    $_SESSION['personID'] = 'a1';
    

    Get the session:

    session_start(); 
    $personID = $_SESSION['personID'];
    

    If you find your self needing to get the data of that person. Simply retirieve the ID and query your DB again.

    SELECT * FROM reviewpi WHERE personID = ?
    


    Please note:
    • You need to stop using mysql, it's deprecated
    • Use mysqli or PDO(recommended)
    • Use prepared statement to avoid SQL injection attacks
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀