1

I need to simply take data from one page, upon a condition, and echo it on another page. Here is my code and my other attempts which do not work.

<?php
session_start;

/* connect to database */
$db = mysql_connect("localhost", "root", "root");
if (!$db)
    die('could not connect');

mysql_select_db('androidp2p')
    or die("could not select database");

/* variables */
$from = mysql_real_escape_string($_POST['from']); 
$to = mysql_real_escape_string($_POST['to']);
$message = mysql_real_escape_string($_POST['message']);

/* conditional code */
if ($_POST['to'])
{
    /* user wants to send a message */
    $query = "INSERT INTO messages (fromuser, touser, message) VALUES ('$from', '$to', '$message')";

    mysql_query($query)
        or die("\n\ndatabase error!\n". mysql_error());

    echo "ok. Messages have been saved.";
}
else 
{
    /* user wants to retrieve his messages*/
    $query = "SELECT * FROM messages WHERE touser='$from'";

    /* echo1 */
    echo $query;

    $result = mysql_query($query)
        or die("\n\ndatabase error!\n". mysql_error());

    $mailbox = array();

    while($row = mysql_fetch_assoc($result)){
    $mailbox[] = $row;
    }

    /* echo2 */
    echo "{ \"mailbox\":".json_encode($mailbox)." }";

    $name = "{ \"mailbox\":".json_encode($mailbox)." }";

    /* echo3 */
    echo $name;

    $_SESSION['myValue']=$name;
}

?>

And on the next php page, called test.php:

<?php

session_start;
echo $_SESSION['myValue'];

?>

My logcat shows that I am getting data in all three echos, but still nothing is being displayed on test.php(i.e. page2).

I also tried:

Attempt 1: In page 1:

session_start();
$_SESSION['myValue']=$name; 

And in page2 used:

session_start();
echo $_SESSION['myValue'];

Attempt 2: In page 1:

session_start();
$_SESSION['name'] = $name ; 

And in page2 used:

session_start();
$name = $_SESSION['name'];

Attempt 3: In page 1:

header("Location: test.php?name=".$name); 

And in page2 used:

$name = $_GET['name'] ;

But nothing is being displayed on test.php(In this last attempt3 I got a bunch of warnings and maybe errors). What am I doing wrong?

This is my logCat:

04-13 14:46:22.564: I/RESPONSE(4591): SELECT * FROM messages WHERE touser='r' 04-13 14:46:22.564: I/RESPONSE(4591): { "mailbox":[{"id":"117","fromuser":"qw","touser":"r","message":"zx","timestamp":"2013-04-13 01:30:59"}] }

Now above I want you to see that touser is r. This entry into the db is what I want, i.e. all entries where the field "touser" contains the value "r".

Now, this is my test.php page:

{ "mailbox":[{"id":"123","fromuser":"r","touser":"","message":"","timestamp":"2013-04-13 13:41:23"},{"id":"122","fromuser":"r","touser":"","message":"","timestamp":"2013-04-13 13:30:53"}] }

As you would see, it returned all entries where the fromuser was r. Why do the two contradict each other??

Keegs
  • 95
  • 3
  • 11
  • Did you verify if $name was set in header("Location: test.php?name=".$name); ? – T.A.C. Commandeur Apr 13 '13 at 16:55
  • 1
    If you are running in local host on your local machine, check to see if have a firewall/antivirus blocking cookie creation at the localhost domain. I had taht once when my session code didnt work. Just an FYI – Ozzy Apr 13 '13 at 16:58
  • First up, `session_start;` means nothing, it is a function and needs to be called like `session_start();` – Hanky Panky Apr 13 '13 at 17:08
  • What does the "Bunch of Warnings" say? – AbsoluteƵERØ Apr 13 '13 at 17:16
  • @HankyPanky - I just added the () in session_start; and test page went from being blank to outputing this: { "mailbox":[] }. I made a silly mistake, but why does it say the array is empty?? – Keegs Apr 13 '13 at 17:16
  • @Ozzy - I disabled firewall, and didn't work. – Keegs Apr 13 '13 at 17:19
  • @user762805 - how do you verify if the header was set with $name? – Keegs Apr 13 '13 at 17:21

1 Answers1

1

Not entirely sure what you are asking, but perhaps this will help.

In the beginning of your /* conditional code */

Use an isset condition....

<?php
session_start();

/* connect to database */
$db = mysql_connect("localhost", "root", "root");
if (!$db)
    die('could not connect');

mysql_select_db('androidp2p')
    or die("could not select database");

/* variables */
$from = mysql_real_escape_string($_POST['from']); 
$to = mysql_real_escape_string($_POST['to']);
$message = mysql_real_escape_string($_POST['message']);

/* conditional code */
if (isset ($_POST['to']))
{
    /* user wants to send a message */
    $query = "INSERT INTO messages (fromuser, touser, message) VALUES ('$from', '$to', '$message')";

    mysql_query($query)
        or die("\n\ndatabase error!\n". mysql_error());

    echo "ok. Messages have been saved.";
}
else 
{
    /* user wants to retrieve his messages*/
    $query = "SELECT * FROM messages WHERE touser='$from'";

    /* echo1 */
    echo $query;

    $result = mysql_query($query)
        or die("\n\ndatabase error!\n". mysql_error());

    $mailbox = array();

    while($row = mysql_fetch_assoc($result)){
    $mailbox[] = $row;
    }

    /* echo2 */
    echo "{ \"mailbox\":".json_encode($mailbox)." }";

    $name = "{ \"mailbox\":".json_encode($mailbox)." }";

    /* echo3 */
    //echo $name; no need to echo

    $_SESSION['myValue']=$name;
}

?>
Derple
  • 865
  • 11
  • 28
  • Ok, one error. This code is adding an entry into the database as well. I only want it to do the echo on the next page. – Keegs Apr 13 '13 at 18:02
  • the INSERT statement is inserting into the database, essentially handling the data. If you dont need the data in the db then you need to remove the db related bits. If its a conditional thing then you would need to define the conditions slightly better, as currently no matter what is in the data fields when the 'to' field has been set the data base action will occur.. add another if statement within the if (isset($_POST['to'])) { //HERE } refining your action. – Derple Apr 13 '13 at 18:08
  • Hmm, I'll try playing around with that and see if it works how I want it too. – Keegs Apr 13 '13 at 18:16
  • Is it possible to do this: else if (isset ($_POST['to']) = null) – Keegs Apr 13 '13 at 18:18
  • 1
    no, that would be incorrect syntax. But to do what youre trying to do there would be "if (!isset ($POST['to'])) {}" which means if is not set do {blah}... you could even put a else on that to make "else if (!isset ($POST['to'])) { //do stuff }" – Derple Apr 13 '13 at 18:20
  • Ok yes, now that is the complete solution for me thanks a mil! – Keegs Apr 13 '13 at 18:33
  • Alright...one more complaint I didn't see earlier @Stuart - The Logcat shows me (correctly) all messages where the variable touser = variable I posted in the $from. But on the php page, somehow it shows me (incorrectly) all the messages where the variable fromuser = variable I posted in the $from. I can't believe I didn't recognize this before..more help please. – Keegs Apr 13 '13 at 18:52
  • not quite sure what you mean, but the bit of code here that i think youre on about is the select statement * $query = "SELECT * FROM messages WHERE touser='$from'"; * if you could elaborate i might be able to help, but im very unclear on what you mean sorry – Derple Apr 13 '13 at 18:56
  • Right right right, of course..I will edit my question to show you the log cat to help me explain. Thanks btw. – Keegs Apr 13 '13 at 18:59
  • I asked another question about this next problem, because I thought maybe it should be asked in another question. [Link](http://stackoverflow.com/questions/15992497/why-doesnt-the-php-page-show-the-same-thing-in-the-logcat) – Keegs Apr 13 '13 at 20:27