我通過JavaScript從前端表單收集數據並將其傳遞到我的PHP腳本以保存到數據庫。有三個表格必須全部填充,否則無(有錯誤)。我將以下文本牆作爲我當前的腳本(大大簡化爲便於閱讀),但我知道不會 - 列名稱拼寫錯誤,應該引發錯誤,但是沒有,另外兩個表保存。交易疑難解答
任何人都可以指出我要去哪裏錯了嗎?
//SAVE TO DB
$conn -> beginTransaction();
$errors = array();
//record edit
foreach($edits as $edit)
{
try
{
$q = $conn -> prepare(" INSERT INTO edits (col1, col2) VALUES (?,?) ");
$q -> execute(array($a, $b));
}
catch(PDOException $e)
{
$conn->rollback();
$errors['Edits'][] = $e->getMessage();
}
}
//record action
foreach($records as $id => $record)
{
if($haveExitingRecords)
{
//update existing record
try
{
$q = $conn -> prepare(" UPDATE records SET col1 = ? WHERE ID = ? ");
$q -> execute(array($a, $b));
}
catch(PDOException $e)
{
$conn->rollback();
$errors['Updates'][] = $e->getMessage();
}
}
else
{
//insert new record
try
{
$q = $conn -> prepare(" INSERT INTO records (col1) VALUES (?) ");
$q -> execute(array($a));
}
catch(PDOException $e)
{
$conn->rollback();
$errors['Inserts'][] = $e->getMessage();
}
}
//also record where x...
if($x !== 1)
{
try
{
//check for existing record
$q = $conn -> prepare(" SELECT ID FROM ifCases WHERE col1 = ? ");
$q -> execute(array($a));
$prev = $q -> fetch(PDO::FETCH_COLUMN);
if(!$prev)
{
$q = $conn -> prepare(" INSERT INTO ifCases (col1, col2) VALUES (?, ?) ");
$q -> execute(array($a, $b));
}
}
catch(PDOException $e)
{
$conn->rollback();
$errors['ifCases'][] = $e->getMessage();
}
}
else
{
try
{
//check for existing record
$q = $conn -> prepare(" SELECT ID FROM ifCases WHERE col1 = ? AND col2 = ? ");
$q -> execute(array($a, $b));
$prev = $q -> fetch(PDO::FETCH_COLUMN);
if($prev > 0)
{
try
{
//delete case
$q = $conn -> prepare(" DELETE FROM ifCases WHERE col1 = ? AND col2 = ? ");
$q -> execute(array($ $a, $b));
}
catch(PDOException $e)
{
$conn->rollback();
$errors['Delete ifCase'][] = $e->getMessage();
}
}
}
catch(PDOException $e)
{
$conn->rollback();
$errors['ifCases'][] = $e->getMessage();
}
}
}
if(empty($errors))
{
$conn->commit();
echo '{"success":"ok"}';
}
else
{
echo json_encode($errors);
}
你在連接上設置錯誤模式? '$ conn-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);' – meda