Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • passing id from function to another in php?

    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 569
    Answer it

    I have PHP MySQL functions and I need to pass the id number from one function to another and each function are called in different page. I'm using the URL to pass the variable. I don't know if that is possible but can I use session to pass the id in a safe way so the users cannot manipulate the URL variable?

    function 1 :

    function select_all_majors() {
                global $connection;
        try {
            $output  = "";
            $output .= "<ul  class=\"\">";
            $sql = "SELECT * FROM `majors`";
            $statement = $connection -> prepare($sql);
            $statement -> execute();
            while ($results = $statement -> fetch(PDO::FETCH_ASSOC)) {
                $major_id   = $results["major_id"];
                $major_name = $results["major_name"];
                $output .= "<li>";
                $output .= "<a href=\"subjects.php?major_id = ";
                $output .= urlencode($major_id) ;
                $output .= "\">";
                $output .= htmlentities($major_name);
                $output .= "</a>";
                $output .= "</li>";
            }
            $output .= "<ul>";
                 echo $output;
                 return $results;
        } catch(PDOException $error) {
    
            $sql_error = $error -> getMessage();
            echo "<h4>" . $sql_error . "</h4>";
        }
    
    }

    function 2 :

    function find_subjects_for_major() {
    
        global $connection ;
        //SELECT * FROM `subjects` WHERE `subjects`.`major_id` = 2
        try {       
            $output = "";
            $query = "SELECT * FROM `subjects`";
            $query.= " WHERE `subjects`.`major_id` = 2";
            $statement = $connection->prepare($query);
            $output  .= "<ul>";
            $statement->execute();
            while($results = $statement->fetch(PDO::FETCH_ASSOC)){
    
            $language_id   = $results["subject_id"];
            $language_name = $results["subject_name"];
            $output   .="<li>";
            $output   .="<a href =\"courses.php\">"; //?id={$language_id}//test.php?languege_id="{echo $result["language_id"];}
            $output   .= htmlentities($language_name)."</li>";
            $output   .="</a>";
            $output   .="</li>";
        }
        $output  .= "</ul>";
        echo $output ;
        return $results;
    
        } catch(PDOException $error) {
            $sql_error = $error -> getMessage();
            echo "<h4>" . $sql_error . "</h4>";
        }
    }

     

 2 Answer(s)

  • Hello Nasiya,

    I just go through your code and i realized that you have two different functions on different pages. If you want to call any function then it should include in current page. If you are calling any function in other page then you can store return values of that function in cookie or in session. If you define both function in different files and include these functions in third file then you can use global variable as well to pass the values.

    Thank You

    Deepak Verma

  • first rows of both files must be:
    1. <?php
    2. session_start()
    than you can use $_SESSION["id"] = $id; from function 1 (in function 1)
    in second function $id = $_SESSION["id"]; or you can use POST (same as GET but invisible in url adress)
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: