Jump to content

Recommended Posts

Hi all,

 

I have a strange one. I have basically a message sending form. It posts to a table and then sends a notification email. I have set it up to run through three functions.

There are three versions / functions.

1, Select a user (from the list out of the DB)

2, Select a group (All users who have 'admin'=on)

3, Send to everyone.

 

1 and 2 are working fine. 3, no joy and I can't see why. It's probably something very simple.

 

Sends to all in a group

<?php
//messaging_group($_POST['group_dd'], $subject, $body);
function messaging_group($to, $subject, $body)
{
  //Get my user
  $username = $_SESSION['username'];
  
  //Setup my date
  $sent_date = date('U');
  
  
  //Setup my queries
  switch ($to) {
    case "s_admin":
        $qry = "SELECT username,email FROM users WHERE s_admin='on'";
        break;
    case "admin":
        $qry = "SELECT username,email FROM users WHERE admin='on'";
        break;
    case "mods":
        $qry = "SELECT username,email FROM users WHERE moder='on'";
        break;
    case "bronze":
        $qry = "SELECT username,email FROM users WHERE bronze='on'";
        break;
    case "silver":
        $qry = "SELECT username,email FROM users WHERE silver='on'";
        break;
    case "gold":
        $qry = "SELECT username,email FROM users WHERE gold='on'";
        break;
    default:
        $qry = "SELECT username,email FROM users WHERE night='$to'";
        break;
}
  
    $query = $qry;
    $result = mysql_query($query) or die(mysql_error());
      while($row = mysql_fetch_array($result)){
        $to = $row['username'];
        $email_to = $row['email'];
        mysql_select_db("discover_messaging");
        mysql_query("INSERT INTO messages (msg_to, msg_from, msg_subject, msg_body, read_flg, sent_date) VALUES ('$to', '$username', '$subject' , '$body', 'off', '$sent_date')") or die($error = 1);
        mysql_select_db("discover_discover");
        
        //Send them an email to notify of a new message
        $email_subject = "[PRIVATE MESSAGE] You have a new message";
        $email_body = "Hi, " . $to . " You have a new message, Log in to view it.";
        mail($email_to,$email_subject,$email_body,"From: Discovery ESU\n") or die($error = 1);
      }


  return $error;
}
?>

 

Send to everyone

<?php
//messaging_group($_POST['group_dd'], $subject, $body);
function messaging_all($subject, $body)
{
  //Get my user
  $username = $_SESSION['username'];
  
  //Setup my date
  $sent_date = date('U');
   
   $query = "SELECT username,email FROM users WHERE allowed='on' and email is not null";
    $result = mysql_query($query) or die(mysql_error());
      while($row = mysql_fetch_array($result)){
        $to = $row['username'];
        $email_to = $row['email'];
        mysql_select_db("discover_messaging");
        mysql_query("INSERT INTO messages (msg_to, msg_from, msg_subject, msg_body, read_flg, sent_date) VALUES ('$to', '$username', '$subject' , '$body', 'off', '$sent_date')") or die($error = 1);
        mysql_select_db("discover_discover");
        
        //Send them an email to notify of a new message
        $email_subject = "[PRIVATE MESSAGE] You have a new message";
        $email_body = "Hi, " . $to . " You have a new message, Log in to view it.";
        mail($email_to,$email_subject,$email_body,"From: Discovery ESU\n") or die($error = 1);
      }
}
?>

 

As I say the top one works. The bottom one won't.

It seems to do the insert for the first record that the query finds and then stops. It doesn't send the email.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/170639-weird-issue/
Share on other sites

The only thing I can pick out is " and email is not null" from your messaging all function. It might not be an incorrect thing, but it's the only thing I know little about and find different (by a lot) compared to the other functions.

 

Instead, you could make good practice of ensuring users have an email address, unless they cannot.

 

Another option would to be to embed a email "checking" function to see if there is an email in row['email'] and whether or not it is a valid format to determine if the email should be sent to that user.

 

 

Link to comment
https://forums.phpfreaks.com/topic/170639-weird-issue/#findComment-900048
Share on other sites

It will be a query error. use mysql_error()

or die($error = 1);

or die(mysql_error());

Also an issue can be switching databases. I would have approached this by building an array of data from the select query on database one. Connect to database 2 and loop / insert the data. You are swithing databases through the loop of a current result set. Bad idea

 

Link to comment
https://forums.phpfreaks.com/topic/170639-weird-issue/#findComment-900066
Share on other sites

Ok, I have added or die in there and come up with this message

 

Access denied for user 'user'@'localhost' to database 'discover_discover'

 

So it appears that it is updating the table but it can switch back to the discover table.

Why won't that work when the send to group is identical and will.

 

Also to neil.johnson. I have no clue how to do arrays. I'm a bit thick really.

Link to comment
https://forums.phpfreaks.com/topic/170639-weird-issue/#findComment-900078
Share on other sites

This is how you should of approached it however I fail to see why you are doing the following:

1. Having subject & body as function parameters but not using them.

2. Inserting the whole email body, subject, against each user as a record. This is not normalised. You should save the email subject, body as 1 record in a table and use a join table to join users who received the email.

 

<?php
function messaging_all($subject, $body) {
// Get my user
$username = $_SESSION['username'];  
// Setup my date
$sent_date = date('U');
// select user data	
$result = mysql_query("SELECT username, email FROM users WHERE allowed='on' AND !ISNULL(email)") or die(mysql_error());
$data = array();
while($row = mysql_fetch_assoc($result)){
	$data[$row['email']] = $row['username'];
}

// connect to other database, insert records and send emails
// open a different handle
$handleDb1 = mysql_connect("localhost","user","passwd");
mysql_select_db("discover_messaging",$handleDb1);
foreach($data as $email => $user) {
	mysql_query("INSERT INTO messages (msg_to, msg_from, msg_subject, msg_body, read_flg, sent_date) 
				 VALUES ('".mysql_real_escape_string($user)."', '".mysql_real_escape_string($username)."', '".mysql_real_escape_string($subject)."' , '".mysql_real_escape_string($body)."', 'off', '".mysql_real_escape_string($sent_date)."')", $handleDb1) or die(mysql_error());		

	$email_subject = "[PRIVATE MESSAGE] You have a new message";
	$email_body = "Hi, ".$user." You have a new message, Log in to view it.";
	mail($email,$email_subject,$email_body,"From: Discovery ESU\n");
}
mysql_close($handleDb1);
}
?>

Link to comment
https://forums.phpfreaks.com/topic/170639-weird-issue/#findComment-900095
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.