phppup Posted May 18, 2018 Share Posted May 18, 2018 I am having a LOOP problem but cannot seem to resolve it. Maybe some better skilled eyes can re-organize and remedy this. In its presented form, I receive 4 blank emails to '[email protected]' which is connecting to my table 'x' which holds 213 records (of which I am querying records with ID>=210. The problem is that the content is missing. At best, I've been able to get the content to display in the first email sent [iD: 210] and then the next email (which would be for ID 211 is blank. No other emails follow, so I assume the loop is being broken. Please assist. echo "HELLLOOOO <br />"; $email_to = "[email protected]"; $email_subject = " email subject line"; $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $email = $_POST['email']; $telephone = $_POST['telephone']; $query = "SELECT * FROM x WHERE id>='210' "; $result = mysqli_query($link, $query); if (mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { @mail($email_to, $email_subject, $email_message, $headers); echo "A message was sent to " .$row['first_name']. " >>> ID: " .$row['id']. "<br />" ; } $headers = 'From: '.$row['email']."\r\n". 'Reply-To: '.$row['email']."\r\n" . 'X-Mailer: PHP/' . phpversion(); //mail($email_to, $email_subject, $email_message, $headers); $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "ID: ".clean_string($row['id'])."\n"; $email_message .= "First Name: ".clean_string($row['first_name'])."\n"; $email_message .= "Last Name: ".clean_string($row['last_name'])."\n"; $email_message .= "Email: ".clean_string($row['email'])."\n"; $email_message .= "Telephone: ".clean_string($row['telephone'])."\n\n"; echo "end <br/>"; /* $headers = 'From: '.$row['email']."\r\n". 'Reply-To: '.$row['email']."\r\n" . 'X-Mailer: PHP/' . phpversion(); // @mail($email_to, $email_subject, $email_message, $headers); */ } echo "Thank you for contacting us. We will be in touch with you very soon.<br />"; Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/ Share on other sites More sharing options...
taquitosensei Posted May 18, 2018 Share Posted May 18, 2018 You've got alot of things out of order here. You're creating your headers after you send the e-mail. You've got the ending bracket for your database loop before the headers. This may not be perfect. But it should get you closer. echo "HELLLOOOO <br />"; $email_to = "[email protected]"; $email_subject = " email subject line"; $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $email = $_POST['email']; $telephone = $_POST['telephone']; $query = "SELECT * FROM x WHERE id>='210' "; $result = mysqli_query($link, $query); if (mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { // Let's create the headers // $headers = 'From: '.$row['email']."\r\n". 'Reply-To: '.$row['email']."\r\n" . 'X-Mailer: PHP/' . phpversion(); $email_message = "Form details below.\n\n"; $email_message .= "ID: ".clean_string($row['id'])."\n"; $email_message .= "First Name: ".clean_string($row['first_name'])."\n"; $email_message .= "Last Name: ".clean_string($row['last_name'])."\n"; $email_message .= "Email: ".clean_string($row['email'])."\n"; $email_message .= "Telephone: ".clean_string($row['telephone'])."\n\n"; @mail($email_to, $email_subject, $email_message, $headers); echo "A message was sent to " .$row['first_name']. " >>> ID: " .$row['id']. "<br />" ; } function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } echo "end <br/>"; } echo "Thank you for contacting us. We will be in touch with you very soon.<br />"; Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558476 Share on other sites More sharing options...
phppup Posted May 18, 2018 Author Share Posted May 18, 2018 Same problem exists, just a different way of getting there. Your solution gives me field names in email message, but no variable info. Thanks for the effort, but I'm still stuck on this one. Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558477 Share on other sites More sharing options...
Barand Posted May 18, 2018 Share Posted May 18, 2018 Does table "x" have columns called first_name last_name email telephone ? Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558478 Share on other sites More sharing options...
phppup Posted May 18, 2018 Author Share Posted May 18, 2018 Yes, table x has those columns. As indicated initially in my second paragraph, I have managed to receive 1 email that has complete message content. My problem at that point, is that only one additional email is dispatched, and it has a totally blank message. I am convinced that there is an issue with my loop but I don't see it. Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558479 Share on other sites More sharing options...
Barand Posted May 18, 2018 Share Posted May 18, 2018 Are you posting your actual code? The first code you posted sent all four emails before any content was put into the $email_message variable, so it's hard to see how you managed to get even one correct email. Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558480 Share on other sites More sharing options...
phppup Posted May 18, 2018 Author Share Posted May 18, 2018 That is it. Can it be juggled or repaired to function as desired? Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558481 Share on other sites More sharing options...
taquitosensei Posted May 18, 2018 Share Posted May 18, 2018 comment out the line that actually sends the e-mail then do a print_r on the $row to make sure you're actually getting the info you're expecting //@mail($email_to, $email_subject, $email_message, $headers); print_r($row); Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558482 Share on other sites More sharing options...
Barand Posted May 18, 2018 Share Posted May 18, 2018 That is it. Can it be juggled or repaired to function as desired? That is what taquitosensei's code did for you. Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558483 Share on other sites More sharing options...
phppup Posted May 18, 2018 Author Share Posted May 18, 2018 I will check it again tomorrow and see if there are any abnormalities. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558484 Share on other sites More sharing options...
Barand Posted May 18, 2018 Share Posted May 18, 2018 And can you you post the output from the print_r($row) that he suggested you put in. We can't see your screen so you need to show the results if want us to diagnose the fault. Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558486 Share on other sites More sharing options...
phppup Posted May 19, 2018 Author Share Posted May 19, 2018 Will do. While on the subject, where would I include COUNT and "echo $count" in order to get a final total of number of emails sent? Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558494 Share on other sites More sharing options...
Barand Posted May 19, 2018 Share Posted May 19, 2018 Inside the loop - I would iincrement a counter when the call to mail() is successful. Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558495 Share on other sites More sharing options...
phppup Posted May 19, 2018 Author Share Posted May 19, 2018 Can you show me the correct formatting of the code, please. Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558496 Share on other sites More sharing options...
Barand Posted May 19, 2018 Share Posted May 19, 2018 $count = 0; while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { // create header and message here if ( mail($email_to, $email_subject, $email_message, $headers) ) { ++$count; echo "A message was sent to " .$row['first_name']. " >>> ID: " .$row['id']. "<br />" ; } } echo "$count messages were sent<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558498 Share on other sites More sharing options...
phppup Posted May 19, 2018 Author Share Posted May 19, 2018 Substituted //@mail($email_to, $email_subject, $email_message, $headers);print_r($row); and got no results. Although I've not used print_r and am unsure of what to expect I have been monitoring my live email. the results are as follows: My initial 'bad' script sends ONLY one email with a blank message. The repaired script sends no emails but displays my "HELLO" message only. The repaired script with an invalid database name sends no emails but displays my "HELLO" and "Thank you...." messages. Clearly, the items in the loop are not making contact correctly. HELP, please. Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558502 Share on other sites More sharing options...
gizmola Posted May 19, 2018 Share Posted May 19, 2018 Post the exact code you have right now, so we can verify that you've implemented the diagnostics correct. On the face of it, having no output indicates that the query is either failing entirely or generating no results, so that explains why you would have no email. Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558503 Share on other sites More sharing options...
phppup Posted May 19, 2018 Author Share Posted May 19, 2018 I copy and pasted the exact code from taquitosensei's post. Then went back to code from my original post. Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558504 Share on other sites More sharing options...
Barand Posted May 19, 2018 Share Posted May 19, 2018 Then went back to code from my original post. Why? We told you it's wrong. Or do you think if you run it enough times it may work? Why haven't you got error reporting turned on? You should be getting a screenful of warnings and notices from that code. From what you have told us so far, I think the fault is with the data (database, table, connection or the query). Each print_r() should output something like Array ( [ID] => 1 [firstname] => John [last_name] => Doe [email] => [email protected] [telephone] => 1236549087 ) showing the column names with their values. Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558505 Share on other sites More sharing options...
phppup Posted May 19, 2018 Author Share Posted May 19, 2018 Even my limited knowledge has allowed me to have confidence in the db and table that I am using. I have a separate script with the same or similar connections to the same table and it successfully echoes all variables in a table. My sense is that there is a disorder in the format of my email or a misplaced } . Either way it appears that the code is allowing a break in the loop (which actually seems NOT to be looping, but rather only running once(at best). Sadly, my original code still comes closest to a successful outcome (since it at least Manfred to send one email. LoL Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558506 Share on other sites More sharing options...
Barand Posted May 19, 2018 Share Posted May 19, 2018 (edited) You have more confidence in your db than I have. I have created a test table CREATE TABLE `x` ( `id` int(11) NOT NULL AUTO_INCREMENT, `first_name` varchar(45) DEFAULT NULL, `last_name` varchar(45) DEFAULT NULL, `email` varchar(45) DEFAULT NULL, `telephone` varchar(45) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; and put a couple of records into it +----+------------+-----------+--------------+-----------+ | id | first_name | last_name | email | telephone | +----+------------+-----------+--------------+-----------+ | 1 | John | Doe | [email protected] | 123 | | 2 | Jane | Doe | janed.gm.com | 234 | +----+------------+-----------+--------------+-----------+ This code, which is basically what taquitosensei posted echo "HELLLOOOO <br />"; $email_to = "[email protected]"; $email_subject = " email subject line"; $query = "SELECT * FROM x WHERE id>='1' "; $result = mysqli_query($link, $query); if (mysqli_num_rows($result) > 0) { $count = 0; while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { // Let's create the headers // $headers = 'From: '.$row['email']."\r\n". 'Reply-To: '.$row['email']."\r\n" . 'X-Mailer: PHP/' . phpversion(); $email_message = "Form details below.\n\n"; $email_message .= "ID: ".clean_string($row['id'])."\n"; $email_message .= "First Name: ".clean_string($row['first_name'])."\n"; $email_message .= "Last Name: ".clean_string($row['last_name'])."\n"; $email_message .= "Email: ".clean_string($row['email'])."\n"; $email_message .= "Telephone: ".clean_string($row['telephone'])."\n\n"; // if ( mail($email_to, $email_subject, $email_message, $headers) ) { // temporarily suppress email if (true) { echo '<pre>', print_r($row, 1), '</pre>'; echo "<pre>$email_message</pre>"; ++$count; echo "A message was sent to " .$row['first_name']. " >>> ID: " .$row['id']. "<br />" ; } } echo "$count emails sent <br/>"; echo "Thank you for contacting us. We will be in touch with you very soon.<br />"; } function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } gave this result HELLLOOOO Array([id] => 1[first_name] => John[last_name] => Doe => [email protected][telephone] => 123)Form details below.ID: 1First Name: JohnLast Name: DoeEmail: [email protected]Telephone: 123A message was sent to John >>> ID: 1Array([id] => 2[first_name] => Jane[last_name] => Doe => janed.gm.com[telephone] => 234)Form details below.ID: 2First Name: JaneLast Name: DoeEmail: janed.gm.comTelephone: 234A message was sent to Jane >>> ID: 22 emails sent Thank you for contacting us. We will be in touch with you very soon. Edited May 19, 2018 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558507 Share on other sites More sharing options...
phppup Posted May 19, 2018 Author Share Posted May 19, 2018 I had decided not to let this script bother me, but I couldn't keep away from it. Then I saw your reply. Created a new file with my connection info and WHAM-O, I got a printout of 223 records (not Ordered By) with the printed message stop of each one and all data fields for each. THANK YOU. I notice that you "suppressed email" and I don't want to damage this code. If you can guide me to enable it I can give it a real run. I will compare it with the other messes that I have to see where my mistakes were. Thank you for the lesson. Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558508 Share on other sites More sharing options...
phppup Posted May 19, 2018 Author Share Posted May 19, 2018 Also, noticed that you left function clean_string at the bottom of script. Does this make it ineffective? Do I actually need it anyway (if variables are being validated before inserting into db? Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558509 Share on other sites More sharing options...
Barand Posted May 19, 2018 Share Posted May 19, 2018 Uncomment the if (mail() ) line and remove the if (true) line. You also need to change your WHERE clause back to your value. You can now remove the debugging print lines too. Also, noticed that you left function clean_string at the bottom of script. Does this make it ineffective? Do I actually need it anyway (if variables are being validated before inserting into db? That is where function definitions should go (or at the top), not in the middle of the flow of code logic. Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558510 Share on other sites More sharing options...
phppup Posted May 19, 2018 Author Share Posted May 19, 2018 Tested live and all confirmation messages printed as desired. Then waited for email. And waited. And worried. And waited. Then remembered a little thing called a spam folder. ROFLMAO. Emails were received and message contents corresponds accordingly. Thanks again. (I'll wait till next week to compare and educate.) Quote Link to comment https://forums.phpfreaks.com/topic/307289-send-email-from-database-problem/#findComment-1558511 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.