Reputation: 95
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??
Upvotes: 1
Views: 5217
Reputation: 863
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;
}
?>
Upvotes: 1