Azizi
Azizi

Reputation: 151

Pass PHP fetch variable to another page

I have a form that submits to firstpage.php, this page includes the code to insert all form values into the database and check for duplicate entries, if the entry is a duplicate , display the duplicate entry using the following php code

 $checkstudentID = mysqli_query
($dbcon, "SELECT studentid from courses WHERE studentid = '$studentid'");
if(mysqli_num_rows($checkstudentID) > 0){
if ($stmt = mysqli_prepare($dbcon, "SELECT ckb from courses WHERE studentid = ?")) {

    mysqli_stmt_bind_param($stmt,"s",$studentid); 
    mysqli_stmt_execute($stmt);
    mysqli_stmt_bind_result($stmt, $ckb);
    mysqli_stmt_fetch($stmt);

        printf("<br /><center><h1>Your Student ID is</h1> <h2>%s.</h2><h1> Subjects Registered :  %s\n </h1>", $studentid, $ckb );

    mysqli_stmt_close($stmt);
}

mysqli_close($dbcon);


        die("  <p> The Student ID  <strong>$studentid </strong>already exists. <a href=\"update.html\">Update</a></p>");

the page update.html includes an update form that submits to update.php

how can I pass the the single fetched row variable (subjects registered/$ckb) to update.html ?

I tried the following so far:

at the firstpage.php I started a session

session_start();
$_SESSION['subjects'] = '$ckb';

and at the update.html > renamed to update2.php and added the following at the top of the page

 <?php
session_start();
echo $_SESSION['sujects'];
?>

and at the input field the value="<?php echo $ckb;?>"

What am I missing ?

Please note, that the variable I want to pass is the subjects registered related to the student id checked in firstpage.php file meaning this :

printf("<br /><center><h1>Your Student ID is</h1> <h2>%s.</h2><h1> Subjects Registered :  **%s**\n </h1>", $studentid, $ckb );

but its either completely wrong or I'm just passing the wrong variable

Upvotes: 0

Views: 972

Answers (2)

Azizi
Azizi

Reputation: 151

In answer to my question I found an easy and effective by passing the variables through the url.

Meaning...

In my firstpage.php, the href links to my update2.php page became as follows:

<a href=\"update2.php?studentid=$studentid&ckb=$cc\">Update</a>

The $studentid and $cc variables are previously defined in my code where I "get" them from the input fields of the form.

In update2.php, the page which I would like to pass the variables to I inserted the following code

<?php
$studentid= $_GET['studentid'];
$cc = $_GET['ckb'];
?>

Which allowed me to use the variables throughout the rest of the php code, where for my case I wanted them to be the "values" of a new form input field, as shown below :

<input name="newcourses" type="text" id="newcourses" maxlength="70" value="<?php echo $cc?>"" />

I recommend anyone who wants a more clear idea and read more about other methods to pass variables across php pages to check this out >> Pass PHP fetch variable...

Upvotes: 0

Sharikov Vladislav
Sharikov Vladislav

Reputation: 7269

Remove quotes in:

$_SESSION['subjects'] = '$ckb';

So it will be:

$_SESSION['subjects'] = $ckb;

And update 2nd file to this:

 <?php
session_start();
$ckb = $_SESSION['subjects'];
?>
....
<input type='text' value="<?php echo $ckb;?>" />

Note: also, you wrote sujects in second file, its ok in my code example.

Upvotes: 1

Related Questions