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)