Jump to content

[SOLVED] MASS PM


unknown1

Recommended Posts

Hello all, I'm trying to send mass private messages to users in my database but keep getting an error... and was hoping someone could help me out.

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''john ','Support Staff ','October 20, 2009, 9:32 pm ','Test Message','Test mess' at line 1

 

Not sure if it's a problem cycling through all the users or what the issue is.

 

$getmembers = mysql_query("SELECT * FROM user"); 	
$num_rows= mysql_num_rows($getmembers);
while($r =mysql_fetch_array($getmembers)){ 
$subject = $_POST['subject'];
$message = $_POST['message'];
$from="Support Staff";
$date = date("F j, Y, g:i a");
$to=$r['Fname'];
$spm = mysql_query("INSERT INTO `private_msg` (`to`,`from`,`date`,`subject`,`content`) VALUES '$to ','$from ','$date ','$subject','$message')") or die(mysql_error());
}													

 

Thanks in advance!!

Link to comment
Share on other sites

it helps when you post what the error is, but its probably this

$spm = mysql_query("INSERT INTO `private_msg` (`to`,`from`,`date`,`subject`,`content`) VALUES '$to ','$from ','$date ','$subject','$message')"

 

which should be

$spm = mysql_query("INSERT INTO `private_msg` (`to`,`from`,`date`,`subject`,`content`) VALUES ('$to ','$from ','$date ','$subject','$message')"

 

missing opening '(' after VALUES

Link to comment
Share on other sites

it helps when you post what the error is, but its probably this

$spm = mysql_query("INSERT INTO `private_msg` (`to`,`from`,`date`,`subject`,`content`) VALUES '$to ','$from ','$date ','$subject','$message')"

 

which should be

$spm = mysql_query("INSERT INTO `private_msg` (`to`,`from`,`date`,`subject`,`content`) VALUES ('$to ','$from ','$date ','$subject','$message')"

 

missing opening '(' after VALUES

 

Thanks for that it was an issue but the biggest problem now is the message does NOT get sent to users... no error at all. Is this the correct way to cycle through the users in the database... or should I be doing something totally different?

Link to comment
Share on other sites

If the query is not generating an error then you should check the DB to see if the records were inserted as you expected. However, I think the problem is that when you define the "to" in the values you are appending an additional space (as well as in $from and $date)

VALUES ('$to ','$from ','$date ','$subject','$message')
//          ^        ^        ^

 

However, having looping queries is terribly inefficient. I suggest you generate one query of all the records to be added and then just run that one query. Besides there is no point in assigning the result of a query to $spm if you aren't going to use it. And you are reassigning the same values over and over to $subject, $message, $from and $date. Why?

 

$subject = $_POST['subject'];
$message = $_POST['message'];
$from    = "Support Staff";
$date    = date("F j, Y, g:i a");
$values  = array();

$getmembers = mysql_query("SELECT * FROM user");	
while($user = mysql_fetch_array($getmembers))
{ 
    $values[] = "('{$user['Fname']}', '{$from}', '{$date}', '{$subject}', '{$message}')";
}													

$query = "INSERT INTO `private_msg` (`to`,`from`,`date`,`subject`,`content`)
          VALUES " . implode(",\n", $values);
mysql_query($query) or die(mysql_error());

Link to comment
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.