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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mail system -> create an array with errorenous nick 1

Status
Not open for further replies.

iranor

Programmer
Joined
Jun 17, 2004
Messages
174
Location
CA
I have this code in my mail system :

case "compose2":
$tomsg=str_replace("<","",$tomsg);
$tomsg=str_replace(">","",$tomsg);
$subject=str_replace("<","",$subject);
$subject=str_replace(">","",$subject);
$message=str_replace("<","",$message);
$message=str_replace(">","",$message);
$currentdate = date("Y/n/d g:i:s A");


$users_list=explode(';',$_POST['tomsg']);
$usersign = mysql_fetch_array(mysql_query("SELECT sign FROM players WHERE player_id='$pid'"));

if(isset($AttachSign)){
$fullmessage = "$message\n\n".$usersign['sign']."";
}else{
$fullmessage = "$message";
}
$request[0] = mysql_query("INSERT INTO mail (author, subject, date, content) VALUES('$pid','$subject', '$currentdate', '$fullmessage')");
foreach($users_list as $users){
$request[1] = mysql_query("SELECT id FROM mail WHERE author='$pid' AND date='$currentdate'");
$results[1] = mysql_fetch_array($request[1]);
$request[3] = mysql_query("SELECT player_id FROM players WHERE username='$users'");
$results[3] = mysql_fetch_array($request[3]);
mysql_query("INSERT INTO mail_link (player_id, message_id) VALUES('".$results[3]['player_id']."','".$results[1]['id']."')");
CleanMailBox($results[3]['player_id']);
}
header("Location: mail.php");
break;

As you can see , the player can add multiple user that will receive the message while using ; to separate users.

Here I need to check if

1) The user exists , because I don't want to add a blank entry in database.
2) Show to the user who is sending the message the usernames that didn't exists.

LIke , if I send a message to nick1 , nick2 , nick3 and nick4 , while nick4 is the only valid , I want to send the message to the user who is valid , so nick4 , and I want to print on the screen: The following message could not be delivered to the following person : nick1 , nick2 , nick3.

Can you help me to modify my request to add this?
 
A "lingering" issue:

Please add error checking to each line that performs a mysql_query. You will not be able to track down errors if you don't handle them.

To your question:
Write a function checkUser() that looks for the ecistence of the username.
Code:
function checkUser($userName){
   $SQL = "SELECT * FROM userTable WHERE username = '".$userName."'";
   $result = mysql_query($SQL) OR die("User lookup failed: ".$mysql_error());
   # check num_rows
   if (mysql_num_rows($result)==0){
      return false;
   } else {
      $row = mysql_fetch_assoc($result) OR die("Could not get user data: ".mysql_error());
      return $row;
   }
}
Use the function to check out each username. Add the failed checks into an array. Print out the array contents in your message of failed lookups.
 
THis is working , but how can I make an array from this :

foreach($users_list as $users){

$SQL = "SELECT username FROM players WHERE username = '$users'";
$result = mysql_query($SQL) OR die("User lookup failed: ".$mysql_error());
$row = mysql_fetch_assoc($result);

if (mysql_num_rows($result) != 0){
$results[1] = mysql_fetch_array(mysql_query("SELECT id FROM mail WHERE author='$pid' AND date='$currentdate'"));
$results[3] = mysql_fetch_array(mysql_query("SELECT player_id FROM players WHERE username='$users'"));
mysql_query("INSERT INTO mail_link (player_id, message_id) VALUES('".$results[3]['player_id']."','".$results[1]['id']."')");
CleanMailBox($results[3]['player_id']);
}
else
{
echo "Message not delivered to : $users";
}
}

If I enter 2 errorenous nick , it shows 2 times Message not delivered to : user

I want it to show it one time separate by ,

So... how can I make an array from the users?
 
Code:
foreach($users_list as $users){
 
 $SQL = "SELECT username FROM players WHERE username = '$users'";
   $result = mysql_query($SQL) OR die("User lookup failed: ".$mysql_error());
      $row = mysql_fetch_assoc($result);
      
      if (mysql_num_rows($result) != 0){
        $results[1] = mysql_fetch_array(mysql_query("SELECT id FROM mail WHERE author='$pid' AND date='$currentdate'"));
    $results[3] = mysql_fetch_array(mysql_query("SELECT player_id FROM players WHERE username='$users'"));
    mysql_query("INSERT INTO mail_link (player_id, message_id) VALUES('".$results[3]['player_id']."','".$results[1]['id']."')");
    CleanMailBox($results[3]['player_id']);
      } 
      else 
      {[COLOR=green]
  $failedUsers[] = $users;
[/color]
       }
  }
[COLOR=green]
if (is_array($failedUsers)){
   echo "The message could not be delivered to ".implode(',',$failedUsers)";
}[/color]

 
Thanks!! that's exactly what I was looking for ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top