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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号