Jump to content

[SOLVED] $_SESSION vs. $_POST


wattsup88

Recommended Posts

Ok I was just wondering how to achieve this goal... I need to pass a row returned from mysql to a variable then to another page after its clicked on... I have tried two methods... The $SESSION method and the $_POST method... Both return the last value of the row and not the actual row im clicking on... Here is the code for the $_POST method

post1.php:

[code]
<form name="viewjobs" action="post2.php>
<?php
$result = mysql_query("SELECT * FROM jobs ORDER BY id");


//query displayed
while ($row = mysql_fetch_array($result))
  {
    $id = $row['id'];

print "<table border=\"1\">";
print "<tr>";
print "<td width=\"200\"><input type=\"hidden\" name=\"id\" value=\"$id\"><input type=\"submit\" value=\"View Job\">";
print "<td width=\"200\">$id";
print "</td>";
print "</table>";

}
?>
</form>
[/code]

post2.php:

[code]<?php

echo $_POST['id'];

?>[/code]

It posts the value "8" as that is the last value of the table... The next method does the same thing... However, keep in mind that I am trying to make the value of the row "clicked on" appear on the next page...Here is the $_SESSION method used...

session1:

[code]<a href="session2.php">Click Here</a>
<?php
$result = mysql_query("SELECT * FROM jobs ORDER BY id");
//query displayed
while ($row = mysql_fetch_array($result))
  {
    $id = $row['id'];


print "<table border=\"1\">";
print "<tr>";
print "<td width=\"200\">$_SESSION['id'] = "$id";
print "<td width=\"200\">$id";

print "</td>";
print "</table>";

}
?>[/code]

session2:

[code]<?php
echo $_SESSION['id'];
?>[/code]

As i stated earlier it returns the value "8"... My best guess is that the variable is overritten every time a value from the database is returned and the last value written is the last value in the table... If anyone has a method that would be functional that would be fantistic... i know i posted something about this before but now its a little more specific so you can understand...

Thanks in advance

-Jake
Link to comment
Share on other sites

You need to set your session like this:

[code=php:0]
$_SESSION['id'] = $id;
[/code]

On a line by itself in your code you are just displaying $_SESSION and whatever $id is equal to.

To display all rows that the query found you can do:

[code]
<?php

$result = mysql_query("SELECT * FROM jobs ORDER BY id");

echo "<table>";

//query displayed
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $id = $row['id'];
    // Create multi-dimension array to store all ids
    $_SESSION['id'][$i] = $id;

  // Display IDs in a table
    echo " <tr>
                <td width=\"200\">{$id}</td>
              </tr>";
    $i++;
}
echo "</table>";
?>
[/code]

Not sure if it is totally correct, but the idea is there :)
Link to comment
Share on other sites

just to clarify, you are only getting the last row because you keep assigning row info to the same variable and then overwriting it on each iteration of the loop.  You need to do like in sharkbait's example, where you create a new element in an array each iteration: the part where he has $i and it increments each time.  Or you could just leave that off and do this:

$_SESSION['id'][] = $id;

because php knows to create the next element without you explicitly specifying it (like with the $i)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.