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

报告相同问题?