/
deleteDB.php
70 lines (47 loc) · 1.73 KB
/
deleteDB.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
require_once("./lib/dbconfig.php");
$son_table = explode(",",$_POST['son']);
$db = new PDO("mysql:host=localhost;dbname=".$dsn,$user,$password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$sql = "SELECT * FROM ".$_POST['main']." WHERE isDel=1 AND modifyDatetime<:date ";
$stmt1 = $db->prepare($sql);
$stmt1->bindParam(":date",$_POST['date']);
try{
$db->beginTransaction();
$row1 = $stmt1->execute();
if(!$row1){
throw new Exception('error execute stmt1');
}else{
echo 'execute stmt1 success<br>';
}
echo "------------------<br>";
echo "刪除中...<br>";
echo "------------------<br>";
while($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)){
for($i=0;$i<count($son_table);$i++){
$sql = "DELETE FROM ".$son_table[$i]." WHERE ".$_POST['s_id']."=:id ";
$stmt2 = $db->prepare($sql);
$stmt2->bindParam(':id',$row1[$_POST['m_id']]);
$row2 = $stmt2->execute();
if(!$row2){
throw new Exception('error execute stmt2');
}
}
}
echo 'execute stmt2 success<br>';
$sql =" DELETE FROM ".$_POST['main']." WHERE isDel=1 AND modifyDatetime<:date";
$stmt3 = $db->prepare($sql);
$stmt3->bindParam(':date',$_POST['date']);
$row3 = $stmt3->execute();
if(!$row3){
throw new Exception('error execute stmt3');
}
else{
echo 'execute stmt3 success<br>';
}
$db->commit();
}
catch (Exception $e){
$db->rollBack();
echo $e->getMessage();
}
?>