I'm dizzy trying to figure out what I should do, I will treasure whoever can help me!

When a user comes to the site they are automatically in their own $_SESSION, they are greeted by their username, it looks professional really nice... If the person is not a member they are redirected... I luv it!
I am using this script:

<?
    include 'db.php';

 // get the variables from home page
   $password = $_SESSION['password'];  
   $email_address = $_SESSION['email_address']; 

   $sql_check = mysql_query("SELECT username FROM members WHERE password='$password' AND
   email_address='$email_address'");  

   $username = mysql_fetch_array($sql_check, MYSQL_BOTH); //MYSQL_BOTH;
   $sql_check_num = mysql_num_rows($sql_check); 
   if($sql_check_num == 0){ 

   echo ("");
 } else {
   echo ucfirst($username['username']);
}

// Define post fields into simple variables 
$email_address = $_SESSION['email_address']; 
$password = $_SESSION['password']; 

/* Let's strip some slashes in case the user entered 
any escaped characters. */ 

$email_address = stripslashes($email_address); 
$password = stripslashes($password); 

/* Do some error checking on the form posted fields */ 

if((!$email_address) || (!$password)){ 
    echo '<STRONG><br><br>Enter required fields as indicated below
          <br>If you are a member!</STRONG><a href="login_form.php"><u> Login Here</u></a>
          <br><STRONG>To become a member!</STRONG><a href="join_form.php"><u> Join Here</u>
          </a><br><br />'; 
    if(!$email_address){ 
        echo "<font color='#000000'>Email address is a required field.</font><br />"; 
    } 
    if(!$password){ 
        echo "<font color='#000000'>Password is a required field.</font><br />";
}
       exit(); // if the error checking has failed, we'll exit the script! 
} 
?> 

Now I want to let my users message each other. I have created a second table just for the messages, it includes the user_id= the to username and from=who sent the message and message=the message.

I must keep the user in their session of course and this way I have their username, Now how do I use the select to verify the user and at the same time.... select that users messages by their username to the other table... But! on that 2nd table their username is under user_id...

And I cannot use username on both tables because on the message page that has the form in the input name= .... it does not allow it , I get an error if I try to use username.

I am trying to get the correct php script to work with mysql, I am trying to use: SELECT, JOIN OR UNION AND WHERE AS ONE... Is this possible?

I am trying something like this, but it's not working, can you see how I can script it to work?

 <?php
include 'db.php';
 // get the variables from home page
   $email_address = $_SESSION['email_address']; 
   $password = $_SESSION['password']; 
   $userame = $_SESSION['username'];  
   $user_id = $_SESSION['user_id']; 

$query = "SELECT messages.user_id, members.username ".
 "FROM messages, members".
  "WHERE members.username = members.email_address
     AND members.username = members.password
     AND messages.user_id = members.username"; 

$result = mysql_query($query) or die(mysql_error());

$sql_check_num = mysql_num_rows($sql_check); 
   if($sql_check_num == 0){ 
// Print out the contents of each row into a table 
while($row = mysql_fetch_array($result)){
  echo $row['username']. " - ". $row['user_id'];                
  echo "<br />";
}
?>

I need help! Thanks kindly , puddin

Recommended Answers

All 5 Replies

Rather than post all your code it would be better to post the schema of the tables that contain the data you need to access, and include any relationships contained between the tables, eg members.username = messages.user_id. This way we can determine the best query to use based on your database setup.

Thanks for your reply; I'm so stressed out...

Gee I do not exactly know what you mean, I am still learning the terminology, but I do know I did it like you suggested I think, see below:

<?php
include’db.php’;
$query = "SELECT messages.user_id, members.username ".
"FROM messages, members ".
"WHERE messages.user_id = members.username";
$result = mysql_query($query) or die(mysql_error());

// Print out the contents of each row into a table
while($row = mysql_fetch_array($result)){
echo $row. " - ". $row;
echo "<br />";
}
?>

Even though I have the script above this one confirming the user is in a $_SESSION
it is not understanding it is in a users session... It is just doing as the script states:

And it prints all the users’ names and messages.... do you get what I mean... I am at this for days I am like a mess to say it mild.

How can I correct it that it verifies it is in the users $_SESSION of that specific user and it SELECTS that specific users’ messages only... I need to write it in one script... it seems HELP!

Oh I hope you know the answer, Thanks so much, puddin

OK - it looks like you need to change your sql so that you are only selecting data relating to the logged in user. For this you can use the $_SESSION variable to filter the sql results.

Try:

<?php
include’db.php’;
$query = "SELECT messages.message ".
 "FROM messages ".
  "WHERE messages.user_id = '$_SESSION['username']'"; 
$result = mysql_query($query) or die(mysql_error());

// Print out the contents of each row into a table 
while($row = mysql_fetch_array($result)){
  echo $_SESSION['username']. " - ". $row['message'];                
  echo "<br />";
}
?>

This assumes that the data in the user_id field in the messages table is the same as the data held in the $_SESSION variable, eg both contain email addresses.

The output should be all of the messages that are linked to the $_SESSION value.

Thanks again for your reply:

My problem is... When I send the message, I have no problem getting them to go to the session users ID and displaying for them on the users message page, it looks Great! BUT!

The Mysql column only holds one message for each user at a time... How do I insert all in going messages, that the mysql will hold them in the same column for the same username?
Can I program the mysql column to hold all messages and than easily select it to release all the messages? I am using Mysql 4.0


Do you know how to make a session user message system that members can send and receive back and forth messages to each other? and view them as a member on the site like we do here.

Example: Like for instance you are a member you come in, your in your session the page says hello to you by username , and you click on the message page and you see oh! you have 3 messages... and all is great! You read them and you choose to delete or hold on to them!

Have you ever did this? How in the world do I construct that? I thought I had it and the bloody thing is not understanding it's in the session and it's saying hello to the user... Too much

That Join code would of worked perfect if only , it could be scripted to understand it's in the session, or If I can Insert all messages into one column for each username... So your help will be treasured...

Thank-you again very much, puddin

Forget above message.... I tried to send this message and edit above, but I timed out see below pkease.

Thanks again for your reply:

No I get error with your code, it's the sessions: Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING ,Buy I tried other methods and...

When I send the message, I can get the message to go into to the users session location username message etc... and it displays on the session users message page, it looks Great! BUT!

The Mysql column only holds one message for each user at a time... How do I insert all of the in going messages for the user and display them all?

HERE IS MY DATABASE LAYOUT:

id  //    email     //    username  // from   //  message    // userid    //  password  
1   //    lshaw.ca  //    Lucy      // John   //  Hi Lucy:   // Lucy      //  ''            
2   //    liho.com  //    Lisa      // Mike   //  Hi Lisa:   // Lisa      //  ''            

Than the id and members list goes on and on.... Now,

How do I program the mysql id usernames column to hold all messages for each user? I am using Mysql 4.0

NOTE: If ithe message does not go into the usernames id row and starts a new row , it does not have the email_address and etc... to know to select after correctly so I end up back like I was last nightm
It must go into the usersname id row - not list in the database on a new row....

Because it was listing on a new row, I tried to use two table and the Join syntax, but it cannot understand with that it's in a users session... It's very hard.

Here is my form:
The select is automatically inserting the persons username from the gallery photo page, you clicked on this members photo to send a message to them. The From you enter in your own username and the messages is the message section...

<form action="message2.php" method="POST">
To: <input type="text" name="user_id" value="<?php include 'db.php'; 
   // get the variables from home page
   $password = $_SESSION['password'];  
   $email_address = $_SESSION['email_address'];

        $sql_check = mysql_query("SELECT username FROM members WHERE id='2'");

        $username = mysql_fetch_array($sql_check, MYSQL_BOTH); //MYSQL_BOTH;
        $sql_check_num = mysql_num_rows($sql_check);
        if($sql_check_num == 0){ 

   echo ("");
 } else {
   echo ''. ucfirst($username['username']); 
}
?> 
" size="25"> 
<br>
From:<input type="text" value="from" size="25">
<br>
<textarea cols="30" rows="10" maxlenght="10" value="message"></textarea>
<br>
<input type="HIDDEN" name="$sessionname"  value="$sessionid">
<input type="submit" value= "Send">
</form>

Do you know how to make a session user message system that members can send and receive back and forth messages to each other? and view them as a member on the site like we do here.

Example: You are a member you come in, your in your session the page says hello to you by username , and you click on the message page and you see oh! you have 3 messages... and all is great! You read them and you choose to delete or hold on to them!

Have you ever did this? How in the world do I construct this, it's very hard to figure it out?
... I really treasure your help thank-you again very much, puddin

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.