Hi, all
I've been banging my head on this one for about a day, now, so I figure it might be time to consult experts.
I've got a form, here, that has 3 fields (First, Last Name, and E-Mail address). It's supposed to auto-generate a username and password, insert into a table, and e-mail the address supplied on the form.
The script works, in that the user gets one e-mail without a problem, but the record insert duplicates itself, so three records of the same data are inserted, and I can't figure out where the loop is (or where PHP is sending an 'insert multiple records' command).
I put a counter around the insert statement, and, when I echo it back, it only trips once, so it doesn't seem to be a loop in PHP.
When I execute the statement in the SQL form in PHPMyAdmin, it does a single record insert, so I don't think it's with the query.
Anyone have any ideas/suggestions? My code is below (the three includes referred to do not execute any DB commands. My next move would be to strip the code down to bare minimum, and build up again from there, but I wanted to see if anyone here had any suggestions.
Thanks in advance!
<?PHP
session_start();
#error_reporting(-1);
include('includes/fnctns.php');
if(!($_SESSION["auth"]))
$_SESSION["auth"] = "n";
#auth_check($_SESSION["auth"]);
include('includes/alert.php');
require_once('includes/dbconnectionstuff.php');
IF ($_POST["Submit"] == "Create Admin")
{
$random_PW = generatePassword(8,1);
$username = strtolower($_POST["FirstName"][0]).strtolower($_POST["LastName"]);
$emailaddress = $_POST["Email"];
$counter = 0;
mysql_select_db($database_CMDB, $CMDB);
$createadminquery = "INSERT INTO siteadmins (username,password,FirstName,LastName,EMailAddress) VALUES ('$username','".md5($random_PW)."','$_POST[FirstName]','$_POST[LastName]','$_POST[Email]')";
$Result = mysql_query($createadminquery, $CMDB) or die(mysql_error());
$counter++;
echo "Counter: ".$counter;
if (mysql_query($createadminquery))
{
$_SESSION['Alert'] = "Admin account for ".$_POST["FirstName"]." ".$_POST["LastName"]." has been created successfully.";
$_SESSION['Alert'] .= "<BR>The user will receive an e-mail with their username and initial password shortly.";
$_SESSION['Alert_Type'] = "success";
$to = $emailaddress;
$subject = "Site Admin Setup";
$message = "Hello! This message is to inform you that your account with our website has been created.\r\n";
$message .= "You can now log onto the administration portion of the website (found at http://www.website.com/admin.php,\r\nand create news posts, events, and other stuch stuff.\r\n";
$message .= "Please log in, using the credentials supplied below. After logging in, you may change your password, if you like:\r\n";
$message .= "\r\n";
$message .= "Username: ".$username."\r\n";
$message .= "Password: ".$random_PW."\r\n";
$message .= "\r\n";
$message .= "If you have any trouble logging in, you can either request a password reset at the login page, or contact Jay at admin@website.com.";
$from = "no-reply@website.com";
$headers = "From: ".$from."\r\n";
$headers .= 'MIME-Version: 1.0' ."\r\n";
$headers .= 'Content-type: text/plain; charset=iso-8859-1' ."\r\n";
$headers .= "X-Mailer: PHP/ ". phpversion();
mail($to,$subject,$message,$headers);
}
unset($_POST);
}
?>
<HTML>
<HEAD>
<TITLE>Website Administration - Create Admin Account</TITLE>
</HEAD>
<BODY>
<TABLE WIDTH="85%" BORDER="1" ALIGN="center">
<TR>
<TD>
<FORM NAME="CreateAdmin" METHOD="post">
<TABLE WIDTH="100%">
<TR>
<TD COLSPAN="2" ALIGN="center">
Type the first name, last name, and e-mail address of the person you would like to add in the spaces below.
<BR>
Their username and initial password will be auto-generated for them, and their initial login credentials will be e-mailed to him/her.
</TD>
</TR>
<?PHP
if (mysql_query($createadminquery)&&$_POST["Submit"] == "Create Admin")
{ ?>
<TR>
<TD COLSPAN="2" ALIGN="center">
<?PHP include('includes/alert.php'); ?>
</TD>
</TR>
<?PHP
}
?>
<TR>
<TD ALIGN="right" WIDTH="50%">
First Name:
</TD>
<TD ALIGN="left" WIDTH="50%">
<INPUT TYPE="TEXT" MAXCHAR="20" NAME="FirstName" ID="FirstName">
</TD>
</TR>
<TR>
<TD ALIGN="right">
Last Name:
</TD>
<TD ALIGN="left">
<INPUT TYPE="TEXT" MAXCHAR="20" NAME="LastName" ID="LastName">
</TD>
</TR>
<TR>
<TD ALIGN="right">
E-Mail Address:
</TD>
<TD ALIGN="left">
<INPUT TYPE="TEXT" MAXCHAR="75" NAME="Email" ID="Email">
</TD>
</TR>
<TR>
<TD ALIGN="center" COLSPAN="2">
<INPUT TYPE = "Submit" Name = "Submit" VALUE = "Create Admin">
</TD>
</TR>
</TABLE>
</FORM>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>