Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing a variable from one page to another 1

Status
Not open for further replies.

PollyJuice

Technical User
Jul 3, 2009
13
SE
I'm making a forum with php and mysql. I have a webserver and a database (WAMP).

Once a user is logged in, he/she can see the titles of the threads on a page called index_online_php. Each thread is a link to another page called enskildtrad.php, where I want to print the thread they have clicked on, with its title, author and date, and a little further down also the answers that have been made to this particular thread.

Here's how I have written the code on index_online.php:

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){

print "<tr><td width=\"50px\"><a href=\"enskildtrad.php?id=" . $row['threadID'] . "\">" . $row['threadID'] . "</a></td>";
print "<td width=\"100px\">" . $row['titel'] . "</td>";
}
print "</table>";


and on the page enskildtrad.php, I've written:


if (isset($_SESSION['userid'])) {

$tradid = (int)$_GET["threadID"];

$conn = mysql_connect("xx.xxx.xx.xxx", "xxxxxx", "xxxxxxxx") or die("Could not connect: " . mysql_error());

$db_selected = mysql_select_db("grafiket", $conn) or die("Can't use testdb : " . mysql_error());

$kvajry = "SELECT * FROM thread T WHERE T.threadID=$tradid";

$result = mysql_query($kvajry);

...

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){

print "<tr><td width=\"150px\" class=\"brodtext\"><img src=\"" . $row['avatar'] . "\">";
print "<br />" . $row['User_Name'] . "</td>";
print "<td width=\"450px\" class=\"brodtext\">" . $row['titel'] . " | " . $row['datum'] . "<hr>" . $row['text'] . "</td></tr>";
}
print "</table>";

But when I test it, all I get when I click a link is the table with Författare and Tråd and no values. I suspect there is something wrong with the line

$tradid = (int)$_GET["threadID"];

or possibly in how I've written the link. Any suggestions on how I can fix this? Pretty please?
 
What is
Code:
$tradid = (int)$_GET["threadID"];
expected to do? Look at using the floor function.

If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
It was supposed to take the threadid that had been clicked on and pass that to the new page, where I could compare the passed-on-value with the threadid in the database's table thread.

I saw that line on another site, and if I remove the (int) the result is that the error message claims

mysql_fetch_array(): supplied argument is not a valid MySQL result resource
 
Your link is passing a variable named just "id":

Code:
<a href=\"enskildtrad.php?[red]id[/red]=" . $row['threadID'] . "\">" . $row['threadID']

However you are expecting a variable named threadID inside the GET super global.


You need to either change the name in your link, or change the name in the $_GET variable so they both match.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thank you, vacunita, that works perfectly. One last thing then - when I want people to be able to answer the thread, I have a link on enskildtrad.php to a page called svar.html,l which in turn consists a form with action=svar.php... and I want this svar.php to also keep the id-variable "in mind", so I can print out all answers which answers the thread with this particular id.

However, I loose the id when I go through svar.html - what should I do then?
 
two methods:

store the id in a session variable or
store the id in a hidden field in the form.

the method that you choose will depend on the type of user you have and the likelihood of abuse.
 
You add the ID to your link like you have for enskildtrad.php


Or you can preserve any variables you need via SESSIONS.

Code:
session_start();
$_SESSION['myvar']="ID value";
session_close();

Then just look for $_SESSION['myvar'] in your other page.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
2 problems:

I can't write <a href=\"svar.html?id=\">, and
I already have a session at the top, to keep track of the logged-in user, and to sessions together on one page don't work.
 
You can have as many session variables as you need.
You only need to call session_start() once. But you can add as many variables as yo need to the $_SESSION array.


$_SESSION['myvar']="something";
$_SESSION['othervar']="Something else";
$_SESSION['thirdvar']="a totally different thing";
...



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I'm still doing something wrong. I've made a new page instead of svar.html, namely svara.php, and written the following:

in enskildtrad.php:
session_start();
if (isset($_SESSION['userid'])) {
$tradid = $_GET["id"];
$_SESSION['myvar']=$tradid;
...
print "<a href=\"svara.php?myvar=\">Svara</a></td>";

and in svara.php:
session_start();
if (isset($_SESSION['userid'])) {
$tradid = $_GET["myvar"];
...
print "<form id=\"inmatning\" method=\"post\" enctype=\"\" action=\"svar.php?tradid=\" name=\"inmatning\">";

and finally in svar.php:
session_start();
if (isset($_SESSION['userid'])) {

$svarpa = $_GET["tradid"];
$texten = $_POST["text"];
$datumet = date("Y-m-d H:i:s");
$user = $_SESSION['userid'];

and ofcourse each of them is finished by
}

else {
// if not set, send back to login
header("Location: login.html");
}

and the text written is inserted properly in the svar-table, but the script doesn't recognize the thread's id nor the user's id.

Where am I going wrong?
 
You can't use the session variable in the same page you set them unless you call session_close()

Other than that, I'm not entirely sure I understand the problem.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
How embarrassing... the variables worked perfectly fine, it was me, who had made one column int instead of varchar, and the variable I got was a string.

Now everything works perfectly.
 
@vacunita
you can't use the session variable in the same page you set them unless you call session_close()

are you sure?

this script
Code:
<?php
session_start();
$_SESSION['test'] = 'fooBar';
print_r($_SESSION);
?>

gives me the result

Code:
Array ( [test] => fooBar )
 
as you yourself have mentioned many times, you tend to get race conditions, in which the session hasn't had time to be written yet when you try to use it.

You may be able to do it when the sessions are small and the variables are not very large. but eventually you'll get to a point where the session hasn't finished being written when you try to use it.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
agreed that you get race conditions between page refreshes. but not within the same script.

the race condition occurs between the session_write_close() (or equivalent => script end) and the next session_start(). the principle being that the session_start() creates a file read event before the file_write event instantiated by the session_write_close() or equivalent.

but asaik you can use $_SESSION like any superglobal after session_start() has been called.
 
I see. Well you learn something new every day I guess.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
my apologies. i wrote that last post when i was very tired and i can see that it makes virtually no sense at all. i will write a proper FAQ on sessions and race conditions and post it in the next couple of days.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top