-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlpha_table.php
More file actions
189 lines (182 loc) · 8.79 KB
/
Copy pathAlpha_table.php
File metadata and controls
189 lines (182 loc) · 8.79 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
include "connect_sql.php";
include "SQLRelative.php";
if (isset($_POST['name'])){
date_default_timezone_set("Asia/Taipei"); //change time zone to Taipei https://www.php.net/manual/en/timezones.php
$date = new DateTime('now');
$date = $date->format('Y-m-d-H-i-s'); //date format
$productname = $_POST['name'];
$quantity = $_POST['quantity'];
$material = $_POST['material'];
$weight = $_POST['weight'];
$tx = $_POST['tx'];
$company = $_POST['company'];
$img_name = ImageCheck($date);
// [0] => Company [1] => ProductName [2] => Quantity [3] => Material [4] => Weight [5] => Contract [6] => Image [7] => Date
$arr = array($company,$productname,$quantity,$material,$weight,$tx,$img_name,$date);
// print_r($arr);
$tablename = "profile";
$backupTable = "profile_backup";
$sql = "CREATE TABLE IF NOT EXISTS $tablename (ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Company VARCHAR(100),ProductName VARCHAR(100),Quantity INT,Material VARCHAR(100),Weight FLOAT,Contract VARCHAR(100),Image VARCHAR(100),Date DATETIME)";
// echo $sql;
if($conn -> query($sql) == false) echo "Failed to create table ".$tablename."<br/>";
// else echo "Table create successfully!<br/>";
$fieldname = GetFieldName($servername, $username, $password, $db_name, $tablename);
// print_r($fieldname);
//[0] => ID [1] => Company [2] => ProductName [3] => Quantity [4] => Material [5] => Weight [6] => Contract [7] => Image [8] => Date
// if(!Duplicate($servername,$username,$password,$db_name,$tablename,$fieldname,$company)){
$sql = "INSERT INTO $tablename(";
for($i=1;$i<count($fieldname);$i++){
if($i != count($fieldname)-1)
$sql .= $fieldname[$i].",";
else
$sql .= $fieldname[$i].") VALUES(";
}
$sql .= "\"$company\",\"$productname\",$quantity,\"$material\",$weight,\"$tx\",\"$img_name\",\"$date\")";
// echo $sql;
if($conn -> query($sql) == false) {
echo "Failed to insert values! <br/>";
header("refresh:1; url=./self.html", true, 301);
exit();
}
// }
// else{
// // echo "Duplicate! can't insert values! <br/>";
// $backupTable = "profile_backup";
// CreateBackupTable($conn,$backupTable);
// UpdateTable($servername,$username,$password,$db_name,$tablename,$backupTable,$company,$arr);
// header("refresh:1; url=./self.html", true, 301);
// exit();
// }
// // Close connection
mysqli_close($conn);
header("refresh:1; url=./self.html", true, 301);
exit();
}
function ImageCheck($date){
# 檢查檔案是否上傳成功
if ($_FILES['product_picture']['error'] === UPLOAD_ERR_OK){
$filetype = $_FILES['product_picture']['type'];
if(!file_exists('uploads')) mkdir('uploads',0777,true); //0777 is already the default mode for directories and may still be modified by the current umask.
$upload_folder = 'uploads/'.$date;
if(!file_exists($upload_folder)) mkdir($upload_folder,0777,true); //if not exists , create one
if(IsImage($filetype)){
# 檢查檔案是否已經存在
if (file_exists($upload_folder . $_FILES['product_picture']['name'])) echo '檔案已存在。<br/>';
else{
$file = $_FILES['product_picture']['tmp_name'];
$dest = $upload_folder . "/". $_FILES['product_picture']['name'];
move_uploaded_file($file, $dest); //將檔案移至指定位置
}
}
}
else{
echo '錯誤代碼:' . $_FILES['product_picture']['error'] . '<br/>';
return false;
}
return $_FILES['product_picture']['name'];
}
function IsImage($filetype){
$acceptable_file_ext = "image";
if(strpos($filetype,$acceptable_file_ext) !== false) return true;
return false;
}
function Duplicate($servername,$username,$password,$db_name,$tablename,$fieldname,$companyname){
$conn = mysqli_connect($servername,$username,$password,$db_name);
// print_r($fieldname);
// [0] => ID [1] => Company [2] => ProductName [3] => Quantity [4] => Material [5] => Weight [6] => Contract [7] => Image [8] => Date
$sql = "SELECT * FROM ".$tablename." WHERE $fieldname[1] = \"$companyname\"";
// echo $sql;
$arr = array();
if($res = mysqli_query($conn, $sql)){
while($row = mysqli_fetch_array($res)){
for($i=0,$j=0;$i<count($fieldname);$i++){
// echo "<td>" . $row[$ProductInfoName[$i]] . "</td>";
$id = $row[$fieldname[0]];
if($i>0) $arr[$id][$j++]=$row[$fieldname[$i]];
}
}
}
// print_r($arr);
$keys = array_keys($arr);
// print_r($keys);
if(count($keys)>0) return true;
return false;
}
function UpdateTable($servername,$username,$password,$db_name,$tablename,$backupTable,$company,$info){
$conn = mysqli_connect($servername,$username,$password,$db_name);
$fieldname = GetFieldName($servername, $username, $password, $db_name, $tablename);
// print_r($fieldname);
//[0] => ID [1] => Company [2] => ProductName [3] => Quantity [4] => Material [5] => Weight [6] => Contract [7] => Image [8] => Date
$sql = "SELECT * FROM $tablename WHERE $fieldname[1] = \"$company\"";
// echo $sql;
$arr = array();
if($res = mysqli_query($conn, $sql)){
while($row = mysqli_fetch_array($res)){
for($i=0,$j=0;$i<count($fieldname);$i++){
// echo "<td>" . $row[$ProductInfoName[$i]] . "</td>";
$id = $row[$fieldname[0]];
if($i>0) $arr[$id][$j++]=$row[$fieldname[$i]];
}
}
}
// print_r($arr);
//[key] => {[0],[1],....}
// [0] => leaflu [1] => leaflu [2] => 1 [3] => bamboo [4] => 123 [5] => 0x282BA262A8d9452F55c8c7373486920Aa9ff90f2
// [6] => 1010152_913705305313905_769581291051537142_n.jpg [7] => 2019-05-13 20:35:56
$keys = array_keys($arr);
// print_r($keys);
if(count($keys)>0){
//INSERT INTO BACKUP TABLE
$sql = "INSERT INTO $backupTable (";
for($i=1;$i<count($fieldname);$i++){
if($i != count($fieldname)-1)
$sql .= $fieldname[$i].",";
else
$sql .= $fieldname[$i].") VALUES(";
}
for($i=0;$i<count($fieldname)-1;$i++){
switch($i){
case 0: case 1: case 3: case 5: case 6:
$sql .= "\"".$arr[$keys[0]][$i]."\",";
break;
case count($fieldname)-2:
$sql .= "\"".$arr[$keys[0]][$i]."\")";
break;
default:
$sql .= $arr[$keys[0]][$i].",";
}
}
// echo $sql;
if($conn -> query($sql) == false) die("Failed to insert to backup table ".$backupTable."<br/>");
// DELETE OLD VALUES
$sql = "DELETE FROM $tablename WHERE $fieldname[1] = \"$company\";";
if($conn -> query($sql) == false) die("Failed to delete row : company = ".$company."<br/>");
$sql = "INSERT INTO $tablename (";
for($i=1;$i<count($fieldname);$i++){
if($i != count($fieldname)-1)
$sql .= $fieldname[$i].",";
else
$sql .= $fieldname[$i].") VALUES(";
}
for($i=0;$i<count($fieldname)-1;$i++){
switch($i){
case 0: case 1: case 3: case 5: case 6:
$sql .= "\"".$info[$i]."\",";
break;
case count($fieldname)-2:
$sql .= "\"".$info[$i]."\")";
break;
default:
$sql .= $info[$i].",";
}
}
// echo $sql;
if($conn -> query($sql) == false) die("Failed to update values <br/>");
}
}
function CreateBackupTable($conn,$tablename){
$sql = "CREATE TABLE IF NOT EXISTS $tablename (ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Company VARCHAR(100),ProductName VARCHAR(100),Quantity INT,Material VARCHAR(100),Weight FLOAT,Contract VARCHAR(100),Image VARCHAR(100),Date DATETIME)";
if($conn -> query($sql) == false) die("Failed to create table ".$tablename."<br/>");
}
?>