I've created a mail message to be sent based on a select statement within a while argument. The problem I'm having is when I add additional fields. The mail message sends just fine with the following concatenation and coding:

while ($row = mysql_fetch_assoc($result)) 

{
 $to = "abstrand@yahoo.com";
 $subject = "New Potential Client";
 $message = 'New Contact Information:'.'
'.$row["FIRST"].' '.$row["LAST"].'
'.$row["AGE"].' '.$row["GENDER"].'
'.$row["EMAIL"].'
'.'Cell '.$row["CELL"].' '.'Home '.$row["HOME"].'
'.$row["CITY"].'
'.$row["LOCATION"].' '.$row["TIMEBLOCK"].'
'.$row["PREFCTCT"].'
'.$row["COMMENT"];

 if (mail($to, $subject, $message)) {
   echo("<p>Message successfully sent! \r </p>");

  } else {
   echo("<p>Message delivery failed...  \r </p>");
  }

}

However, once I add a few more fields, the whole thing falls apart:

while ($row = mysql_fetch_assoc($result)) 

{
 $to = "abstrand@yahoo.com";
 $subject = "New Potential Client";
 $message = 'New Contact Information:'.'
'.$row["FIRST"].' '.$row["LAST"].'
'.$row["AGE"].' '.$row["GENDER"].'
'.$row["EMAIL"].'
'.'Cell '.$row["CELL"].' '.'Home '.$row["HOME"].'
'.$row["CITY"].'
'.$row["LOCATION"].' '.$row["TIMEBLOCK"].'
'.$row["PREFCTCT"].'
'.'Fitness Goals: '.'
'.'Lose weight: '.'$row["LOSE"].' 
'.'Gain muscle: '.'$row["GAIN"].'
'.'Lean and tone: '.'$row["LEAN"].'
'.'Injury Rehabilitation: '.'$row["REHAB"].'
'.'Sports specific training: '.'$row["SPORTS"].'
'.'Overall fitness: '.'$row["OVERALL"].'
'.$row["COMMENT"];

 if (mail($to, $subject, $message)) {
   echo("<p>Message successfully sent! \r </p>");

  } else {
   echo("<p>Message delivery failed...  \r </p>");
  }

}

Any thoughts on why an additional field blows the whole thing up? Is there a limit? I've even tried just putting in a simple extra echo field with 'hi' and it dies.

Thanks in advance.

Recommended Answers

All 8 Replies

Hi,

You have a parse error in your code. You should remove the quotes before of $row variables as like mentioned above of string 'Fitness Goals'.

I think this code works fine,

while ($row = mysql_fetch_assoc($result)) 

{
 $to = "abstrand@yahoo.com";
 $subject = "New Potential Client";
 $message = 'New Contact Information:'.'
'.$row["FIRST"].' '.$row["LAST"].'
'.$row["AGE"].' '.$row["GENDER"].'
'.$row["EMAIL"].'
'.'Cell '.$row["CELL"].' '.'Home '.$row["HOME"].'
'.$row["CITY"].'
'.$row["LOCATION"].' '.$row["TIMEBLOCK"].'
'.$row["PREFCTCT"].'
'.'Fitness Goals: '.'
'.'Lose weight: '.$row["LOSE"].' 
'.'Gain muscle: '.$row["GAIN"].'
'.'Lean and tone: '.$row["LEAN"].'
'.'Injury Rehabilitation: '.$row["REHAB"].'
'.'Sports specific training: '.$row["SPORTS"].'
'.'Overall fitness: '.$row["OVERALL"].'
'.$row["COMMENT"];


 if (mail($to, $subject, $message)) {
   echo("<p>Message successfully sent! \r </p>");

  } else {
   echo("<p>Message delivery failed...  \r </p>");
  }

}

Why this???

All this concatenations ???

from line 6-14. Writing code like this means you want a bug.

you could do all that in a very simple like this.

$message ="   New Contact Information

$row[FIRST]
$row[LAST]

$row[AGE]
$row[GENDER]

$row[EMAIL]
Cell $row[CELL]
 Home $row[HOME]

$row[CITY]

$row[LOCATION]
$row[TIMEBLOCK]

$row[PREFCTCT] 
$row[COMMENT]
Fitness Goals: 
Lose weight: $row[LOSE] 
Gain muscle: $row[GAIN]
Lean and tone: $row[LEAN]
Injury Rehabilitation: $row[REHAB]
Sports specific training: $row[SPORTS]
Overall fitness: $row[OVERALL]
$row[COMMENT]
";

This works and its easier. Arrays from $_POST,$_GET, and from Mysql_fetch_array can work like this without the '' and "".

Thanks guys. This helps a lot. Richieking that makes this a lot easier. Much obliged.

You are welcome good friend

:)

commented: Great concise answer +0

Why this???

All this concatenations ???

from line 6-14. Writing code like this means you want a bug.

you could do all that in a very simple like this.

$message ="New Contact Information

$row[FIRST]
$row[LAST]

$row[AGE]
$row[GENDER]

$row[EMAIL]
Cell $row[CELL]
 Home $row[HOME]

$row[CITY]

$row[LOCATION]
$row[TIMEBLOCK]

$row[PREFCTCT] 
$row[COMMENT]
Fitness Goals: 
Lose weight: $row[LOSE] 
Gain muscle: $row[GAIN]
Lean and tone: $row[LEAN]
Injury Rehabilitation: $row[REHAB]
Sports specific training: $row[SPORTS]
Overall fitness: $row[OVERALL]
$row[COMMENT]
";

This works and its easier. Arrays from $_POST,$_GET, and from Mysql_fetch_array can work like this without the '' and "".

By removing the quotes from those associative array keys EVERY single one of those will throw a Notice. You simply don't notice this because you either have error reporting turned off or suppressed. Bad practice to get into.

http://php.net/manual/en/language.types.array.php

Scroll down to the section called "Why is $foo[bar] wrong?"

This is wrong when you have constants declare with the same name. the concepts is, why do you declare constants using small letters?

If there is no constants declared, no issues.

Note that it is inside a system quote.

$foo="$ba[dd] $s[de]";

They become a string and are passed in a special way.
However with ' ' quote inside that also works the same. This technique takes the concatenation away.

No errors suppressed. try it yourself on your server and put all error report on.

i always have them on.

Explore ;)

Its a best practice to declare constants in uppercase, but there is no requirement to do it this way. Why introduce a dependency on something so trivial.

The only place that works is in a double quoted string. It doesn't work anywhere else. It doesn't save any considerable code to do it this way.

<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);

//Comment me out to see notices
//Define COMMENT as 'NOT' so when $row[COMMENT] is called...
//We get the value of $row['NOT'] instead...
define('COMMENT', 'NOT');

$row = array(
	'COMMENT' => 'comment',
	'NOT' => 'not comment',
);

echo "$row[COMMENT]".PHP_EOL; //comment
echo "{$row[COMMENT]}".PHP_EOL; //not comment
echo $row[COMMENT].PHP_EOL; //not comment
echo sprintf('%s', $row[COMMENT]).PHP_EOL; //not comment

echo $row["COMMENT"].PHP_EOL; //comment (swapped quotes from around string to array key)
echo "{$row['COMMENT']}".PHP_EOL; //comment
echo $row['COMMENT'].PHP_EOL; //comment (duplicate of 1)
echo sprintf('%s', $row['COMMENT']).PHP_EOL; //comment
comment
not comment
not comment
not comment
comment
comment
comment
comment

There are so many way to cross the river.

Its there something wrong with a different way??

I dont get you?

Yep, that is why they are in double quotes.
Thats the kill friend ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.