I can't even seem to make commit and rollback work properly any more, even through they worked about 2 days ago. This code is designed to get some basic details from a form.
The admin chooses a number of students to add, for example 3. They enter 3 emails, first names and last names and the script takes the data. It then validates it. If incorrect it presents the form again for changes. When the data is correct it adds it using inserts to the db and commits after each correct add.
--Things I've Done To Sort this Problem Out--
The whole thing worked the other day, but now it doesn't. I do an insert, then a commit, and it doesn't commit, even though the insert works. I've tested the inserts directly to the db and they work. I'm getting no errors about this. Is I do an if(mysqli_commit($mysqlcon){echo "fish";} it returns true, meaning the commits are okay. If I do mysqli_affected_rows($mysqlcon) for the inserts they return true as well. The reason I know that there is no data added to the db is because when I check using "select * from students", I get nothing. I've also checked the apache and mysqli services in windows, I don't know what else to do.
Why is it suddenly doing this? I've turned off autocommit obviously so that I can do transactions. The whole script is here but I've broken it up into 3, the second bit is the most important:
<?php
$header_title = "Admin Panel - Add a class of Students";
include_once("admin_header.php");
$message=array();
$emailErrorMessage=array();
$activationCodes=array();
//Get back the number of students to be added
//This allows us to correctly judge what the text boxes are called and how many.
$numberOfStudents = $_POST['numberOfStudents'];
//these tell me whether there have been previous errors
$fnameError=false;
$fnameError=false;
$emailError=false;
$insertSuccesses = 0;
//Get back all of the emails entered
for($c=1;$c<=$numberOfStudents;$c++)
{
//email addresses
if(empty($_POST["student".$c]))
{
if(!$emailError)
{
array_push($message,"You forgot to enter an email address for one or many students, please re-check the fields.");
$emailError=true;
}
}
else
{
$emails[$c]=$_POST["student".$c];
// Check for an email address
//Note, this regular expression was copied from the following text, as it is a common regular expression for checking email addresses:
//Ullman,L (2005) PHP and MySQL for Dynamic Web Sites: Visual QuickPro guide 2nd edn. Peachpitt Press.
if(!eregi ("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,6}$", stripslashes(trim($emails[$c]))))//Ullman,L (2005)
{
array_push($message,"The email address for student ".$c." was not in the correct format.".$c);
$emails[$c]="";
}
}
//first names
if(empty($_POST["fname".$c]))
{
if(!$fnameError)
{
array_push($message,"You forgot to enter a first name for one or many students, please re-check the fields.");
$fnameError=true;
}
}
else
{
$fnames[$c]=$_POST["fname".$c];
//Check the last name.
if(!eregi("^[[:alpha:].' -]{2,50}$", stripslashes(trim($fnames[$c]))))
{
array_push($message,"You failed to enter the correct format for student ".$c."'s first name, it must be between 2 and 50 characters and contain only letters.");
$fNames[$c] = "";
}
}
//Last names
if(empty($_POST["lname".$c]))
{
if(!$lnameError)
{
array_push($message,"You forgot to enter a last name for one or many students, please re-check the fields.");
$lnameError=true;
}
}
else
{
$lnames[$c]=$_POST["lname".$c];
//Check the last name.
if(!eregi("^[[:alpha:].' -]{2,50}$", stripslashes(trim($lnames[$c]))))
{
array_push($message,"You failed to enter the correct format for student ".$c."'s last name, it must be between 2 and 50 characters and contain only letters.");
$lNames[$c] = "";
}
}//end else
}//end for loop
//END OF VALIDATION
//If there are errors print them and then reprint the form.
if(isset($message[0]))
{
//otherwise print the error messages out and provide the form again for the user.
echo "<h2>There were errors in the submission. Please read the details below and try again.</h2>";
foreach($message as $me => $m)
{
echo "<span style = \"color:#333399;margin-bottom:100px;\">".($me+1).") ".$m."<p /></span>";
}
?>
<form method = "post" action = "add_group_students_rows.php">
<h4>Please enter an email address for each student (for example bill@gates.com): </h4><br />
You should have obtained an email address from each student which is used to email their activation code:
<p />
<?php
for($i=1;$i<=$numberOfStudents;$i++)
{
echo "<div style = \"width:100%;clear:both;margin-bottom:20px;\"><div style = \"float:left;clear:none;\">Email ".$i.":</div><div style = \"float:right;clear:right;margin-right:200px;\"><input type = \"text\" size=\"45\" name = \"student".$i."\" value = \"".$emails[$i]."\" /></div>\n";
echo "<div style = \"float:left;clear:none;\">First name ".$i.":</div><div style = \"float:right;clear:right;margin-right:200px;\"><input type = \"text\" size=\"45\" name = \"fname".$i."\" value = \"".$fnames[$i]."\" /></div>\n";
echo "<div style = \"float:left;clear:none;\">Last Name ".$i.":</div><div style = \"float:right;clear:right;margin-right:200px;\"><input type = \"text\" size=\"45\" name = \"lname".$i."\" value = \"".$lnames[$i]."\" /></div></div> <p /> \n";
}
//Send the number of students so we can get back emails.
echo "<input type = \"hidden\" name = \"numberOfStudents\" value = \"".$numberOfStudents."\" />";
?>
<p />
<input type = "submit" value = "Add these Accounts" name = "submit" />
</form>
<?php
}//end of if
else
{
//Calculate the activation codes required, now stored in $activationCodes[]
$activationCodes = getActivationCodes($numberOfStudents);
//Add the students to the system.
foreach($emails as $key => $value)
{
echo $activationQuery = "insert into students(activationCode,activated,email,fname,lname) values(".$activationCodes[$key].",0,\"".$emails[$key]."\",\"".$fnames[$key]."\",\"".$lnames[$key]."\")";
$activationResult=mysqli_query($mysqlcon,$activationQuery);
if($activationResult)
{
echo "commit<p />";
mysqli_commit($mysqlcon);
}//end of else.
else
{
//remove the previously added rows.
mysqli_rollback($mysqlcon);
//If this is true then the query failed.
array_push($message,"An insert query into the database failed, please check the query in the script and the database itself.");
}//end else
}//end for each
//end doing inserts.
//START SENDING EMAILS TO STUDENTS
$subject = "Your activation code!";
$emailMessage = "Hi, you've received this message because we've just added you to the Assessment Support system.\nBelow you will find your activation code, which you will need to add yourself to the system properly.\nOnce you've done this you will be able to check homework and modules you've been set, homework and module grades and also see graphs of your current progress.\n\n";
$headers = 'From: webmaster@assessmentsupport.com' . "\r\n" .
'Reply-To: nelnels <webmaster@assessmentsupport.com>' . "\r\n" .
'X-Mailer: PHP/'.phpversion();
//The fun bit, send each of these users an email with their code in.
for($e=1;$e<=sizeOf($emails);$e++)
{
//send the emails
$success=mail($emails[$e],$subject,$emailMessage."Your activation code:".$activationCodes[$e]."\n\nYou can now visit the Assessment Support system and activate using this code.",$headers);
//If the mail function failed, this means the email was not accepted for delivery.
if(!$success)
{
//remove the previously added rows.
//mysqli_rollback($mysqlcon);
array_push($emailErrorMessage,"The email to '".$emails[$e]."' failed. Please contact this user, informing them of their error.");
}
}//end for
}//end of else
//Print out the errors.
echo "<p />";
foreach($emailErrorMessage as $em=>$e)
{
echo $e."<p />";
}
//If true then the script is a success.
if(!isset($message[0]))
{
echo "<h2>All students successfully added!</h2><p />";
for($e=1;$e<=sizeOf($emails);$e++)
{
echo "Student ".$e.") \"".$emails[$e]."\" was given the code of <strong>".$activationCodes[$e]."</strong><br />";
}
echo "<p />You can now choose to print these codes to be given to students or rely on the emails which have just been sent.<p />";
echo "If you see any email failed to send errors, this is usually due a lack of an outgoing email client or because an incorrect email address was given, and is not down to the code itself.";
}
else
{
//print out the errors
foreach($message as $me =>$m)
{
echo "<div style = \"color:blue;text-align:left;\">".($me+1).") ".$m."</div><br />";
}
}
include_once("admin_footer.php");