visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: Help with coding


By: mrhell U.S.A.  Date: 02/10/2003 00:00:00  English  Points: 50 Status: Answered
Quality : Excellent
I am trying to set up an automatic email sending program, that sends a 9 digit registration code.

This is my code so far, but it's full of errors and I don't know how to fix them.

<?PHP

$myEmail = $_GET['email']
$sender = "registration@mywebsite.com";
$subject = "Registration Code";
$headers= "From: ".$sender."\r\n";
$headers.= "Reply-To: ".$replyto."\r\n";
$headers.= "Cc: ".$cc."\r\n";
$body= "Verify your information\r\n\r\n";
$body=. "Click on the link below to activate your account: \r\n\r\n";
$body=. int(rnd*999999999);

mail($myEmail, $subject, $body, $headers);


mysql_connect (localhost);

mysql_select_db (testing);

mysql_query ("INSERT INTO (registration_code) VALUES ('$registration_code')");


?>
By: VGR Date: 02/10/2003 01:22:00 English  Type : Comment
first add some error checking... 8-)))


if (!mail($myEmail, $subject, $body, $headers)) echo "email was not sent !!!";
else {
mysql_connect (localhost) or die("could not connect!");
mysql_select_db (testing) or die("could not select DB!");
mysql_query ("INSERT INTO (registration_code) VALUES ('$registration_code')") or die("invalid query ! ".mysql_error());
}
By: LornaJane Date: 02/10/2003 01:24:00 English  Type : Comment
what sort of errors? does it give a parse error or does it not do what you wanted it to? where do the $cc and $replyto variables get set?

Post some more detail, such as error messages, and we'll help!
By: mrhell Date: 02/10/2003 01:28:00 English  Type : Comment
Parse error: parse error, unexpected T_VARIABLE in /home/virtual/site8/fst/var/www/html/scripts/mail.php on line 4

Thats the first error I'm getting and if line 4 is an error then I'm assuming the rest will be the same type of errors.
I'm very new to php so I don't really understand most of this coding as it was done by a friend.
And I am not sure why the $cc and $reply to are even in the code.
By: LornaJane Date: 02/10/2003 01:40:00 English  Type : Comment
Here is your code with a blow-by-blow narrative of what's happening, hopefully this will help you grasp what's going on!

<?PHP // the opening php tag

$myEmail = $_GET['email'] // sets the variable $myEmail to the value of email from the address bar (could have been passed there by a form)
$sender = "registration@mywebsite.com"; // assigns variables
$subject = "Registration Code";

// the next few lines, set the headers of the email, like to, from, cc etc.
$headers= "From: ".$sender."\r\n";
// I am concerned about $sender, this variable isn't set. If it is in the address bar then use $_GET['sender'] to refer to it. If not then post the page that submits to this one (I'm guessing a form submits to this page)
$headers.= "Reply-To: ".$replyto."\r\n";
$headers.= "Cc: ".$cc."\r\n"; // $cc and $replyto are the same as $sender, I don't think they are set

// the next lines are probably OK, just setting some words for the content of the email
$body= "Verify your information\r\n\r\n";
$body=. "Click on the link below to activate your account: \r\n\r\n";
$body=. int(rnd*999999999);

// mail() command sends email. I suggest you comment out this line and just echo the variables here (eg echo($myEmail))
mail($myEmail, $subject, $body, $headers);


mysql_connect (localhost); // you might need a username and password? And is the database set up and existing etc?
mysql_select_db (testing);
// mysql_query ("INSERT INTO (registration_code) VALUES ('$registration_code')");
// replace with
mysql_query ("INSERT INTO (registration_code) VALUES ('$registration_code')") or die(mysql_error()); // this will give you any error that occurs
// is $registration code set? try echoing it before the mysql_query to check

?>

OK I hope that makes it clearer and that you can make a little more progress yourself. Let me know how you get on and I'll help you over the next hurdle, whatever it is :)


By: mrhell Date: 02/10/2003 01:49:00 English  Type : Comment
Ok that's cleared a few things up but I'm still not sure about the sender variable.

The problem is, I'm sending data from flash into php to send to their email while adding the registration code into the database which I have setup.

Rather than putting in a username and password I have put this tag to save some time.

include("connect2db.php");

I hope you can help me some more.

Thanks.
By: mrhell Date: 02/10/2003 02:31:00 English  Type : Comment
I've tried playing around with the code a bit and everytime I take out a line it says:

Parse error: parse error, unexpected T_VARIABLE in /home/virtual/site8/fst/var/www/html/scripts/mail.php on line 6
or which ever line is next after what I have taken out so it looks like NOTHING works???
By: Paulh Date: 02/10/2003 02:57:00 English  Type : Comment
Probably already spotted but the line:

$myEmail = $_GET['email']

needs ; at the end, this will cause a syntax error otherwise. So the line should look like:

$myEmail = $_GET['email'];

Hope this helps.
By: mrhell Date: 02/10/2003 03:02:00 English  Type : Comment
This is my code so far;

<?PHP

include("connect2db.php");

$myEmail = $_GET['email'];
$sender = "registration@mywebsite.com";
$subject = "Registration Code";

$headers= "From: ".$sender."\r\n";
$headers.= "Reply-To: ".$replyto."\r\n";
$headers.= "Cc: ".$cc."\r\n";

$body= "Verify your information\r\n\r\n";
$body= "Click on the link below to activate your account: \r\n\r\n";
$body= int(rnd*999999999);

mail($myEmail, $subject, $body, $headers);

mysql_query ("INSERT INTO (registration_code) VALUES ('$registration_code')") or die(mysql_error());

?>

And this is what it prints when I got to the webpage;

Fatal error: Call to undefined function: int() in /home/virtual/site8/fst/var/www/html/scripts/mail.php on line 15
By: VGR Date: 02/10/2003 03:15:00 English  Type : Comment
of course Paulh spotted the first error. You could at least READ the error line printed by PHP...It's explicit... It gives you the error type (; expected) and the line number...
By: mrhell Date: 02/10/2003 03:21:00 English  Type : Comment
No need to get cockey! So can you help me with the rest of the code?
By: VGR Date: 02/10/2003 03:26:00 English  Type : Comment
have you tried my first suggestion ?

<?php

$myEmail = $_GET['email']; // may be unset
$sender = "registration@mywebsite.com";
$subject = "Registration Code";
$headers= "From: $sender\r\n"; // useless quoting here
$headers.= "Reply-To: $replyto\r\n"; // I suggest to use a variable $separator="\r\n" once for good :D
$headers.= "Cc: $cc\r\n";
$body= "Verify your information\r\n\r\n";
$body=. "Click on the link below to activate your account: \r\n\r\n";
$body=. int(rnd*999999999);

if (!mail($myEmail, $subject, $body, $headers)) echo "email was not sent !!!";
else {
echo "mail sent, DB update begins
";
mysql_connect (localhost) or die("could not connect!");
mysql_select_db (testing) or die("could not select DB!");
mysql_query ("INSERT INTO (registration_code) VALUES ('$registration_code')") or die("invalid query ! ".mysql_error());
echo "DB update passed OK
";
}

?>


By: mrhell Date: 02/10/2003 03:31:00 English  Type : Comment
OK i've tried that and this is what it prints

mail sent, DB update begins
could not select DB!
By: VGR Date: 02/10/2003 04:06:00 English  Type : Comment
well, I thought you had modified the code to hide someimplementation details, but if not, then your DB queries/commands are incorrect.

<A HREF="http://www.php.net/manual/en/function.mysql-select-db.php">http://www.php.net/manual/en/function.mysql-select-db.php</a>
bool mysql_select_db ( string database_name [, resource link_identifier])

so your line should be mysql_select_db('somedatabasename'); and not "testing" which is not a string

also, turn on error-reporting=E_ALL so that you would have seen the NOTICEs errors about undefined constants 'localhost' and 'testing' being considered as the strings "localhost" and 'testing', which turned OK for 'localhost' (but means you've set default user & password in php.ini which is VERY BAD in my humble opinion, and turned bad for 'testing' because I suppose that this database doesn't exist...

in a word as in one hundred, this is how your code should look IMHO :

if (!mail($myEmail, $subject, $body, $headers)) echo "email was not sent !!!";
else {
echo "mail sent, DB update begins
";
$linkID=mysql_connect ('localhost','username','password') or die("could not connect!");
mysql_select_db ('testing',$linkID) or die("could not select DB!");
$result=mysql_query ("INSERT INTO (registration_code) VALUES ('$registration_code')",$linkID) or die("invalid query ! ".mysql_error());
echo "DB update passed OK
";
}


By: mrhell Date: 02/10/2003 04:15:00 English  Type : Comment
You have to understand I'm very new to php, so overall what should my code look like, and anything that needs to be changed can you put comments next to it.
If you can do this I will give you an A grade.
Thanks.
By: VGR Date: 02/10/2003 04:27:00 English  Type : Answer
I dit it all I think ;-)

<?php

$myEmail = $_GET['email']; // may be unset
$sender = "registration@mywebsite.com";
$subject = "Registration Code";
$headers= "From: $sender\r\n"; // useless quoting here
$headers.= "Reply-To: $replyto\r\n"; // I suggest to use a variable $separator="\r\n" once for good :D
$headers.= "Cc: $cc\r\n";
$body= "Verify your information\r\n\r\n";
$body=. "Click on the link below to activate your account: \r\n\r\n";
$body=. int(rnd*999999999);

if (!mail($myEmail, $subject, $body, $headers)) echo "email was not sent !!!";
else {
echo "mail sent, DB update begins
";
$linkID=mysql_connect ('localhost','username','password') or die("could not connect!");
mysql_select_db ('testing',$linkID) or die("could not select DB!");
$result=mysql_query ("INSERT INTO registration_code VALUES ('$registration_code');",$linkID) or die("invalid query ! ".mysql_error());
echo "DB update passed OK
";
}

?>


note that the way it's written, your table has only one field. Else do :

INSERT INTO registration_code_this_is_the_table_name (the_reg_code_field) VALUES ('$registration_code');

or

INSERT INTO registration_code_this_is_the_table_name SET the_reg_code_field='$registration_code';

I think that perhaps you mistook the registretion_code FIELD for the TABLE name :D


Do register to be able to answer

EContact
browser fav
page generated in 116.631980 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page