由于订阅者表自动编号将附加1,即使发生了事务错误且回滚没有帮助。然后它无法在list_子表中添加信息,因为id不一致,所以我使用
alter tableid auto inc = 1
减去汽车公司的编号,我把它放在catch区块而没有另外的try-catch。但这是常见做法吗?三江源
$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;}
表: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
表:list_sub
1 SubID int(11) No None AUTO_INCREMENT Change Drop More
2 ListID int(11) No None Change Drop More
表:表
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
答案 0 :(得分:0)
我认为有一个有意义的自动增量字段是不好的设计,原因就像你描述的那样。并不总是能够确保按照您希望的方式分配数字。
在这种情况下,命令似乎是订户号码16(比如说)是第16个订户。但是用户号码绝对必须以这种方式连续吗?我担心的是,您为代码添加了额外的复杂性,以满足此规则。我非常重视简单性。
答案 1 :(得分:0)
如果你想保持顺序只是为了保持顺序.....它主要是美学的东西。别担心。您的代码将更容易编码和阅读。
如果你出于某种原因需要某种全球唯一标识符,那么也许你会想要这种东西....但即使这样你也不需要它。