Код: Выделить всё
require ('connection/mysql.php'); // Connects to the database
include ('sessionShared.php');
require ('logger.php');
$intMaterialID = $_POST['intMaterialID'];
$numWeightProc = $_POST['numWeightProc'];
$intnumrows = $_POST['numrows'];
$intCurrentRow = 1;
$companyid = $_SESSION['companyid'];
$conn_wp->begin_transaction();
try {
for ($i = 1; $i 0) {
$query_getlastid = "SELECT PKproductsID, PKproductsUnitID FROM tblwasteproducts WHERE FKUnitID = $companyid ORDER BY PKproductsID DESC";
$resultlastid = $conn_wp->query($query_getlastid);
if (!$resultlastid) {
$newlastid = 1; // When unit has no clients
} else {
$rstlastid = $resultlastid->fetch_assoc();
$newlastid = $rstlastid['PKproductsUnitID'] + 1;
}
if ($arrProductModified[$i] < 1) {
// Add a normal material
$query_rstProductAdd = "INSERT INTO tblwasteproducts (PKproductsUnitID, FKUnitID, FKMaterialID, prod_weight, prod_weightleft, prod_category, ProductDescription) VALUES ($newlastid, $companyid, $intMaterialID, $intProductWeight, $intProductWeight, $arrProductCat[$i], '$arrProductDescription[$i]')";
} else {
// Add a modified material
$query_rstProductAdd = "INSERT INTO tblwasteproducts (PKproductsUnitID, FKUnitID, FKMaterialID, prod_weight, prod_weightleft, prod_category, FKMaterialModifiedID, ProductDescription) VALUES ($newlastid, $companyid, $intMaterialID, $intProductWeight, $intProductWeight, $arrProductCat[$i], $arrProductModified[$i], '$arrProductDescriptionModified[$i]')";
}
if (!$conn_wp->query($query_rstProductAdd)) {
throw new Exception("Error inserting product data: " . $conn_wp->error);
}
}
}
// Update the proc table - remove material
$intTakenLeft = $numWeightProc;
$query_rstProcMaterial = "SELECT PKProcID, PKProcUnitID, FKUnitID, wproc_material, wproc_weight FROM tblwasteprocessing WHERE wproc_material = $intMaterialID AND wproc_weight > 0 AND FKUnitID = $companyid";
$rstProcMaterial = $conn_wp->query($query_rstProcMaterial);
while ($dataProcMaterial = $rstProcMaterial->fetch_assoc()) {
$intTempWeight = $dataProcMaterial["wproc_weight"];
if ($intTempWeight > 0 && $intTakenLeft > 0) {
if ($intTempWeight < $intTakenLeft) {
$intTakenLeft -= $intTempWeight;
$query_rstProductUpdate = "UPDATE tblwasteprocessing SET wproc_weight = 0 WHERE PKProcID = " . $dataProcMaterial["PKProcID"];
} else {
$query_rstProductUpdate = "UPDATE tblwasteprocessing SET wproc_weight = $intTempWeight - $intTakenLeft WHERE PKProcID = " . $dataProcMaterial["PKProcID"];
$intTakenLeft = 0;
}
if (!$conn_wp->query($query_rstProductUpdate)) {
throw new Exception("Error updating processing table: " . $conn_wp->error);
}
}
}
$query_rstMaterial = "SELECT wm_description FROM tblwastematerials WHERE PKID = $intMaterialID";
$rstMaterial = $conn_wp->query($query_rstMaterial);
$dataMaterial = $rstMaterial->fetch_assoc();
$conn_wp->commit();
} catch (Exception $e) {
$conn_wp->rollback();
error_log("Transaction failed: " . $e->getMessage());
}
// Close connection
$conn_wp->close();
?>
Я пробовал SELECT...FOR UPDATE, но ничего не получалось.
Я пытался использовать флаг, используя сеанс глобальный, но он в основном блокирует пользователя.
У меня возникла идея создать столбец в таблице «учетные записи» (в котором хранится информация о пользователе) с флагом «isProcessing», который станет равным 1, когда транзакция находится в процессе выполнения и 0, когда она завершается.
Заранее спасибо.
Подробнее здесь: https://stackoverflow.com/questions/790 ... in-php-5-6
Мобильная версия