Here is the code I have.

<?php 
include("db.php"); 

if (isset($_POST['username']) && isset($_POST['passwords']) && isset($_POST['email']))
 
{ 

$username = mysql_real_escape_string($_POST['username']); 
$email = mysql_real_escape_string($_POST['email']); 
 
$password = md5($_POST['passwords']); 
$ip = $_SERVER['REMOTE_ADDR'];

$sql3 = mysql_query("SELECT username FROM usersystem WHERE username = '$username'");

If (mysql_num_rows($sql3) >0) 

{ 
 echo( "&serverResponse=Select a different user name.  This one is already in use.");}

 
$sql2 = mysql_query ("SELECT email FROM usersystem WHERE email = '$email'");

If (mysql_num_rows ($sql2) >0)

{
 echo( "&serverResponse=This e-mail address is already registered.");
}
 
 
mysql_query("INSERT INTO usersystem (username, password, email, ipadd) VALUES ( '$username', '$password', '$email', '$ip')")
 or die (mysql_error());
}
 
 

If (mysql_num_rows($sql3) >0)) {
echo ("&serverResponse=Success");

$sendTo = $email;
$subject = "Registration Confirmation";

$headers = "From: " . "email@domain.com";
$message = "\nDear " . $username;
$message .= "\nThank you for registering.  Your log in information is as follows:";
$message .= "\nUsername: " . $username;
$message .= "\nPassword: " . $password;

mail($sendTo, $subject, $message, $headers);
}
else {
echo ("&serverResponse=There was an error, please try again.");
}


?>

If I remove everything below line 34, it will validate and register the user (but then no e-mail is sent). If I have anything below line 33 (even if I remove the bracket)it will not validate that the username and e-mail address are unique nor put out a server response. I'm fairly new to PHP. Can anyone tell me what is wrong with this script? I have been changing and altering and attempting to get it right for a while now and I can't seem to. :confused:

Recommended Answers

All 13 Replies

I cannot see the include of your mail-send-template, because the code itself looks ok.

Hi Wapa,

Thanks for your quick reply.

Mail-send-template? Is it necessary?

On other forms, where the results are not stored in a DB, just directly e-mailed, I do not need a mail-send-template. Is one required because I am also posting to a DB?

If so, can you recommend a good tutorial or example I could follow that will also show me how to put it in this code?

well, if you never used a mail-template that means that you can send mails directly vía the PHP-mail-function, but this depends of your hosting.

Do you have an access to the logs of the mailserver ?
Because it seems the the line

$headers = "From: " . "email@domain.com";

needs a correction.

It should be with brackets:

$headers = "From: " . "<email@domain.com>"; // here of course need to show up a real e-mail-adress.

Wapa,

I tried that. It didn't seem to help.

I am sure I have access to the logs but not exactly sure what to look for.


I did look at one of the other php files for a form that submits an e-mail to us. One of the headers looks like this:

"<" . $_POST["nemail"] . ">\r\n";

So I attempted this:

$headers = "From: " . "<"email@domain.com">\r\n";

(Yes I used the proper e-mail address to)

I also tried:

$headers = "From: <"info@victimofhell.com">\r\n";

It frustrates me because as long as that code is there it will not verify that there are no matching records for username or email in the database.


Is there a way to end the first php session and then start another? Perhaps separating them out will do the trick?

1.
$headers = "From: " . "<"email@domain.com">\r\n";

is wrong, because you cannot use \r\n in a e-mail-direction.

try simply : $headers = "From: " . "<".email@domain.com.">";

This should work.

Ad logfile: You should try to find if the php-mail attempted to make a connection to the mailserver and, if its the case, look for an error-number. You can verific the entry with the mailadress of the sender - in your case the e-mail-adress which you send in the $header.

uuppss.. I oversaw one thing:

in line 14 :

$sql3 = mysql_query("SELECT username FROM usersystem WHERE username = '$username'");

in line 16 :

If (mysql_num_rows($sql3) >0) ....

and you use the same recordset in line 37 again:

If (mysql_num_rows($sql3) >0)) {

This you cannot do ! You have to reset the recordset or build an other one.
So, the last mysql-num-query will not show any result.

Before you call again the $sql3 - recordset (because you wanto to search again through), you have to move the pointer at the beginning of the recordset:

mysql_data_seek ( $sql3, 0* )  *= your first data-entry in the database

OR

you define a new recordset ($sql4) and call it before the mail-function.

Line No. 37 is having Syntax error:

If (mysql_num_rows($sql3) >0)) {

Rewrite as

If (mysql_num_rows($sql3) >0) {

Other than that, everything else is ok!

Still a no go. But now it is doing nothing, even if there is no duplicate entry. Here is what I have.

<?php 
include("db.php"); 
 
if (isset($_POST['username']) && isset($_POST['passwords']) && isset($_POST['email']))
 
{ 
 
$username = mysql_real_escape_string($_POST['username']); 
$email = mysql_real_escape_string($_POST['email']); 
 
$password = md5($_POST['passwords']); 
$ip = $_SERVER['REMOTE_ADDR'];
 
$sql3 = mysql_query("SELECT username FROM usersystem WHERE username = '$username'");
 
If (mysql_num_rows($sql3) >0) 
 
{ 
 echo( "&serverResponse=Select a different user name.  This one is already in use.");}
 
 
$sql2 = mysql_query ("SELECT email FROM usersystem WHERE email = '$email'");
 
If (mysql_num_rows ($sql2) >0)
 
{
 echo( "&serverResponse=This e-mail address is already registered.");
}
 
 
mysql_query("INSERT INTO usersystem (username, password, email, ipadd) VALUES ( '$username', '$password', '$email', '$ip')")
 or die (mysql_error());

 
mysql_data_seek ( $sql3, 0* );
 
If (mysql_num_rows($sql3) >0) {
 
$sendTo = $email;
$subject = "Registration Confirmation";


$headers = "From: " . "<".email@domain.com.">";
$message = "\nDear " . $username;
$message .= "\nThank you for registering.  Your log in information is as follows:";
$message .= "\nUsername: " . $username;
$message .= "\nPassword: " . $password;

 
mail($sendTo, $subject, $message, $headers);
}
else {
echo ("&serverResponse=There appear to be an error.  Try again later.");
}
 
 
?>

Sorry: The asterisk I only put to explain the "0".

Of course it would be:
mysql_data_seek ( $sql3, 0);

You have to use your first original code only with that correction which i mentioned...
they it will work.
you have changed lot in the current code.

Still a no go. But now it is doing nothing, even if there is no duplicate entry. Here is what I have.

<?php 
include("db.php"); 
 
if (isset($_POST['username']) && isset($_POST['passwords']) && isset($_POST['email']))
 
{ 
 
$username = mysql_real_escape_string($_POST['username']); 
$email = mysql_real_escape_string($_POST['email']); 
 
$password = md5($_POST['passwords']); 
$ip = $_SERVER['REMOTE_ADDR'];
 
$sql3 = mysql_query("SELECT username FROM usersystem WHERE username = '$username'");
 
If (mysql_num_rows($sql3) >0) 
 
{ 
 echo( "&serverResponse=Select a different user name.  This one is already in use.");}
 
 
$sql2 = mysql_query ("SELECT email FROM usersystem WHERE email = '$email'");
 
If (mysql_num_rows ($sql2) >0)
 
{
 echo( "&serverResponse=This e-mail address is already registered.");
}
 
 
mysql_query("INSERT INTO usersystem (username, password, email, ipadd) VALUES ( '$username', '$password', '$email', '$ip')")
 or die (mysql_error());

 
mysql_data_seek ( $sql3, 0* );
 
If (mysql_num_rows($sql3) >0) {
 
$sendTo = $email;
$subject = "Registration Confirmation";


$headers = "From: " . "<".email@domain.com.">";
$message = "\nDear " . $username;
$message .= "\nThank you for registering.  Your log in information is as follows:";
$message .= "\nUsername: " . $username;
$message .= "\nPassword: " . $password;

 
mail($sendTo, $subject, $message, $headers);
}
else {
echo ("&serverResponse=There appear to be an error.  Try again later.");
}
 
 
?>

mysql_data_seek Syntax error.
No need of that line. Remove it.

Thanks all for your help. Here is what I ended up doing:

{ 
 
$username = mysql_real_escape_string($_POST['username']); 
$email = mysql_real_escape_string($_POST['email']); 
$password = md5($_POST['pword']); 
$ip = $_SERVER['REMOTE_ADDR'];
 
 
$sql2 = mysql_query("SELECT username FROM usersystem WHERE username = '$username'");
if (mysql_num_rows($sql2) >0) { 
die ("&serverResponse=Username taken.");} 

$sql3 = mysql_query("SELECT email FROM usersystem WHERE email = '$email'");
if (mysql_num_rows($sql3) >0) { 
die ("&serverResponse=E-mail already registered.");} 

 
mysql_query("INSERT INTO usersystem (username, password, email, ipadd) VALUES ( '$username', '$password', '$email', '$ip')") or die (mysql_error()); 
echo "&serverResponse=Account created.  Log in.";


 
} 


$sql4 = mysql_query("SELECT email FROM usersystem WHERE email = '$email'");
if (mysql_num_rows($sql4) >0) { 


$sendaddress = "me@mydomain.com";
$sendTo = $email;
$subject = "Registration Confirmation";
 
 
$headers = "From: " . $sendaddress;
$message = "\nDear " . $username;
$message .= "\nThank you for registering.  Your log in information is as follows:";
$message .= "\nUsername: " . $username;
$message .= "\nPassword: " . $_POST['pword'];

 
mail($sendTo, $subject, $message, $headers);
}
 
 
?>
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.