unknown1 Posted October 21, 2009 Share Posted October 21, 2009 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!! Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted October 21, 2009 Share Posted October 21, 2009 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 Quote Link to comment Share on other sites More sharing options...
unknown1 Posted October 21, 2009 Author Share Posted October 21, 2009 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? Quote Link to comment Share on other sites More sharing options...
unknown1 Posted October 21, 2009 Author Share Posted October 21, 2009 I'm not 100% sure but I'm assuming that the loop while($r =mysql_fetch_array($getmembers)){ } is not enough to get the message to all users... I don't know 100% but I can't think of any other reason it wont work... Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 21, 2009 Share Posted October 21, 2009 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()); Quote Link to comment Share on other sites More sharing options...
unknown1 Posted October 21, 2009 Author Share Posted October 21, 2009 Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 21, 2009 Share Posted October 21, 2009 Is your problem soved? If so, marke the topic solved. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.