当我在php代码中添加一个transaction时,MySQL的自动增量值跳2

当我在php代码中添加一个transaction时,MySQL的自动增量值跳2,php,mysqli,transactions,auto-increment,Php,Mysqli,Transactions,Auto Increment,我有一张有病人信息的桌子,还有一张是他们的电话号码。 它们之间的关系,是1:n1对多。我提出了一个限制:我希望所有患者至少有一个电话号码。所以,我在我的代码中放了一个事务,如下所示 // Beggining of transaction mysqli_query($dbc,'begin'); $sql1 = " INSERT INTO `dental_clinic`.`patient` (`idpatient` ,`surname` ,`name` ,`middle_name` ,`a

我有一张有病人信息的桌子,还有一张是他们的电话号码。 它们之间的关系,是1:n1对多。我提出了一个限制:我希望所有患者至少有一个电话号码。所以,我在我的代码中放了一个事务,如下所示

// Beggining of transaction 
mysqli_query($dbc,'begin'); 
$sql1 = "   INSERT INTO  `dental_clinic`.`patient` (`idpatient` ,`surname` ,`name` ,`middle_name` ,`address` , `town`,  `birth_date` , `occupation` , `email` ,`p_comments`)    VALUES (NULL ,  '$surname',  '$name',  '$middle_name',  '$address',  '$town',  '$birth_date',  '$occupation',  '$email', '$other');
        ";
$execute_sql1 = mysqli_query($dbc, $sql1);  
$last_id =  mysqli_insert_id($dbc); 

//  Put some variables to the insert queries of telephone.  
$sql2_1 = " INSERT INTO `dental_clinic`.`telephone`(`patient_idpatient`, `phone`) VALUES ('$last_id', '$phone') ";    
$sql2_2 = "INSERT INTO  `dental_clinic`.`telephone`(`patient_idpatient`, `phone`) VALUES ('$last_id', '$phone2')";
$sql2_3 = "INSERT INTO  `dental_clinic`.`telephone`(`patient_idpatient`, `phone`) VALUES ('$last_id', '$phone3')";

// Checking the first field of telephone 
if (!empty($phone)){  
    $execute_sql2_1 = mysqli_query($dbc, $sql2_1);  
} else {
    $execute_sql2_1 = false;  
}

//  Checking the second field of telephone
if (!empty($phone2)){  
    $execute_sql2_2 = mysqli_query($dbc, $sql2_2);  
} else {
    $execute_sql2_2 = true;  
}

//  Checking the third field of telephone
if (!empty($phone3)){   
    $execute_sql2_3 = mysqli_query($dbc, $sql2_3);  
} else {  
    $execute_sql2_3 = true;  
}  

//  Checking the insert commands to execute toggether
if ($execute_sql1 && $execute_sql2_1){  
    if ($execute_sql2_1 && $execute_sql2_2){  
        if ($execute_sql2_1 && $execute_sql2_3){  
            mysqli_query($dbc, 'commit');  
            echo 'The patient personal details inserted succesfully!';  
            echo 'The primary telephone inserted succesfully! ';  
            header("Location: new_medical_history.php");  
        } else {  
            mysqli_query($dbc, 'rollback');  
            echo 'Error, on 3rd phone! ';  
        }  
    } else {  
        mysqli_query($dbc, 'rollback');  
        echo 'Error, on 2nd phone! ';    
    }  
} else {  
    mysqli_query($dbc, 'rollback');  
    echo 'Error, on patient personal details or on primary telephone! ';  
}  

//  Ending connection
mysqli_close($dbc);
两张桌子、病人和电话的形式相同

当我成功地添加一个新患者时,autocrement idpatient值会跳两个1,3,5,7,依此类推。但当我对交易进行评论时

 // mysqli_query($dbc,'begin);  
 // mysqli_query($dbc, 'commit');  
 // mysqli_query($dbc, 'rollback');  
然后,自动增量值变为正常值7,8,9,10。。等等


你能告诉我为什么会这样吗?我的代码有问题吗?我想保留该交易,这样就不会添加没有主要电话号码的患者。

我发现您的逻辑很难理解,因此我使用try-catch重写了您的代码,我相信它满足同样的要求,并且更容易理解。这还会导致2倍的跳跃吗

// fixme: consider using prepared statements to avoid sql injection attacks

mysqli_query($dbc,'begin');

try {
    if(!mysqli_query($dbc, "INSERT INTO `dental_clinic`.`patient` (`idpatient` ,`surname` ,`name` ,`middle_name` ,`address` , `town`,  `birth_date` , `occupation` , `email` ,`p_comments`) VALUES (NULL, '$surname', '$name', '$middle_name', '$address', '$town', '$birth_date', '$occupation', '$email', '$other')")) throw new Exception('Error, on patient personal details!');
    $last_id =  mysqli_insert_id($dbc); 

    if(empty($phone)) throw new Exception('Error, primary phone required!');

    if(!empty($phone) && !mysqli_query($dbc, "INSERT INTO `dental_clinic`.`telephone`(`patient_idpatient`, `phone`) VALUES ('$last_id', '$phone')")) throw new Exception('Error, on primary telephone!');
    if(!empty($phone2) && !mysqli_query($dbc, "INSERT INTO `dental_clinic`.`telephone`(`patient_idpatient`, `phone`) VALUES ('$last_id', '$phone2')")) throw new Exception('Error, on 2nd phone!');
    if(!empty($phone3) && !mysqli_query($dbc, "INSERT INTO `dental_clinic`.`telephone`(`patient_idpatient`, `phone`) VALUES ('$last_id', '$phone3')")) throw new Exception('Error, on 3rd phone!');

    mysqli_query($dbc, 'commit');  

    // these will not be seen if you immediately redirect
    //echo 'The patient personal details inserted succesfully!';  
    //echo 'The primary telephone inserted succesfully! ';  

    header("Location: new_medical_history.php");  
} catch(Exception $e) {
    mysqli_query($dbc, 'rollback');  
    echo $e->getMessage();
}

//  Ending connection
mysqli_close($dbc);

如果评论不是希腊语的话会很有帮助…这似乎是一种非常复杂和不完整的用户输入验证方法…你使用什么存储引擎?好的,我把它翻译成了英语。我使用InnoDB。我有一个表格,每个表格都有一个字段personal,我有3个字段可以添加到电话表中。我不介意现在检查数据的有效性。当我解决这个问题时,我会这样做。请注意mysql不会在回滚时重置自动增量值,请查看浏览器是否没有两次发送数据,或者您可以在这个问题中添加测试用例和sql create。。。