-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.php
More file actions
81 lines (56 loc) · 2.02 KB
/
Copy pathpractice.php
File metadata and controls
81 lines (56 loc) · 2.02 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
<?php
if(!empty($_FILES['file']['name']))
{
$name = $_FILES["file"]["name"];
$connect = new PDO("mysql:host=localhost:3307;dbname=testing;", "root", "", array(
PDO::MYSQL_ATTR_LOCAL_INFILE => true,
));
$total_row = count(file($_FILES['file']['tmp_name']));
$file_location = str_replace("\\", "/", $_FILES['file']['tmp_name']);
//$query_5='truncate table customer_table';
//$statement = $connect->prepare($query_5);
//$statement->execute();
$query_1 = '
LOAD DATA LOCAL INFILE "'.$file_location.'" IGNORE
INTO TABLE customer_table
FIELDS TERMINATED BY ","
LINES TERMINATED BY "\r\n"
IGNORE 1 LINES
(@column1,@column2,@column3,@column4)
SET customer_first_name = @column1, customer_last_name = @column2, customer_email = @column3, customer_gender = @column4
';
$statement = $connect->prepare($query_1);
$statement->execute();
$query_2 = "
SELECT MAX(customer_id) as customer_id FROM customer_table
";
$statement = $connect->prepare($query_2);
$statement->execute();
$result = $statement->fetchAll();
$customer_id = 0;
foreach($result as $row)
{
$customer_id = $row['customer_id'];
}
$first_customer_id = $customer_id - $total_row;
$first_customer_id = $first_customer_id + 1;
$query_3 = 'SET @customer_id:='.$first_customer_id.'';
$statement = $connect->prepare($query_3);
$statement->execute();
$query_4 = '
LOAD DATA LOCAL INFILE "'.$file_location.'" IGNORE
INTO TABLE order_table
FIELDS TERMINATED BY ","
LINES TERMINATED BY "\r\n"
IGNORE 1 LINES
(@column1,@column2,@column3,@column4,@column5,@column6,@column7)
SET customer_id = @customer_id:=@customer_id+1, product_name = @column5, product_price = @column6, order_date = @column7
';
$statement = $connect->prepare($query_4);
$statement->execute();
$output = array(
'success' => 'Total <b>'.$total_row.'</b> Data imported'
);
echo json_encode($output);
}
?>