Since the subscriber table auto- inc number will append 1 even though the transaction error occur and the roll back does not help. Then it can not add the information in list_ sub table since the id is not consistent ,so I use
alter tableid auto inc =1
to minus the auto inc number and i put it in the catch block without another try- catch. But is it common practice? Thankyou
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_AUTOCOMMIT, FALSE);
$conn->beginTransaction();
try {
$email = $_POST['Email'];
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
$query="INSERT INTO subscriber (Email,FirstName,LastName,CreateDate) VALUES (?,?,?,CURDATE())";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $email , PDO::PARAM_STR);
$stmt->bindParam(2, $FirstName, PDO::PARAM_STR);
$stmt->bindParam(3, $LastName, PDO::PARAM_STR);
$stmt->execute();
$conn->commit();
}
catch(PDOException $e)
{
$conn->rollBack();
$query="ALTER TABLE subscriber AUTO_INCREMENT = 1";
$stmt = $conn->prepare($query);
$stmt->execute();
die ($e->getMessage()."<a href='addSub.php'>Back</a>");
}
$conn->beginTransaction();
try {
$userID = $_SESSION['username'];
$query="INSERT INTO list_sub (SubID,ListID) VALUES ('',$_SESSION[ListID])";
$stmt = $conn->prepare($query);
$stmt->execute();
$conn->commit();
}
catch(PDOException $e)
{
$conn->rollBack();
die ($e->getMessage()."<a href='addSub.php'>Back</a>");
}
$conn = null;}
Table:Subsciber
1 SubID int(11) No None AUTO_INCREMENT Change Drop More
2 Email varchar(100) utf8_general_ci No None Change Drop More
3 FirstName varchar(100) utf8_general_ci Yes NULL Change Drop More
4 LastName varchar(100) utf8_general_ci Yes NULL Change Drop More
5 CreateDate date No None Change Drop More
6 UpdateDate date Yes NULL Change Drop More Check All / Uncheck All With selected: Browse Change Drop Primary Unique Index
Table:list_sub
1 SubID int(11) No None AUTO_INCREMENT Change Drop More
2 ListID int(11) No None Change Drop More
Table:list
1 ListID int(11) No None AUTO_INCREMENT Change Drop More
2 ListName varchar(100) utf8_general_ci No None Change Drop More
3 FromName varchar(100) utf8_general_ci No None Change Drop More
4 ReplyTo varchar(100) utf8_general_ci No None Change Drop More
5 Subject varchar(100) utf8_general_ci No None Change Drop More
6 IsRemindSub tinyint(1) No None Change Drop More
7 IsRemindUnSub tinyint(1) No None Change Drop More
8 CreateDate date No None Change Drop More
9 Reminder varchar(100) utf8_general_ci No None Change Drop More
I think it is bad design to have a meaningful auto-increment field, for reasons such as the one that you describe. It is not always possible to ensure that numbers will be allocated the way you want them to be.
In this case the imperative seems to be that subscriber number 16 (say) is the 16th subscriber. But do the subscriber numbers absolutely have to be contiguous in this way? My concern is that you are adding extra complexity to your code, in order to satisfy this rule. I value simplicity above all else.