0

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

4
  • 1
    Don't use $_SESSION['subjects'] = '$ckb'; - the single quotes around the variable will give you the literal string $ckb, rather than the value of the variable. Remove the single quotes there and see what happens. You'll also need to set $ckb again in your second page, if you're trying to echo its value. Commented Jan 31, 2015 at 23:18
  • maybe your problem is '$ckb' remove the singlle quotes Commented Jan 31, 2015 at 23:18
  • 3
    subjects in one place sujects in another Commented Jan 31, 2015 at 23:20
  • Fixed both "syntaxes" , however nothing is being passed or echoed to update2.php Commented Jan 31, 2015 at 23:29

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I think it's not answer. because is output $ckb (string). but the problem about output is empty.
It can't output $ckb as string just because $ckb variable is not defined in second file. I noticed 2 mistakes: quotes around variable in 1st file and missing assigning to $ckb in 2nd. Or question is incorrect. I'd answered the question.
he say echo $_SESSION['sujects']; not working. not echo $ckb
I tried with another $_SESSION['subjects'] = $studentid and tried to echo that, its passing nothing...
@Azizi, can you please update your answer so we can see your code after update. Show how exactly you assign variables in first file and in second file and how you output them in second file.
|
0

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.