forked from aemunahmar/DBProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONtoDB.php
More file actions
76 lines (63 loc) · 2.12 KB
/
JSONtoDB.php
File metadata and controls
76 lines (63 loc) · 2.12 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
<?php
// Create connection
$dbconnect = mysqli_connect("127.0.0.1", "root", "", "user_accounts");
//Check if connection worked
/*if($dbconnect){
echo 'Connected';
}
else {
echo 'Failed\n';
}*/
//Read the JSON file
$jsondata = file_get_contents('Employee.json');
//Decode JSON data
$data = json_decode((utf8_encode($jsondata)), true);
var_dump($data);
if(is_array($data))
{
echo "is array";
foreach($data as $row)
{
///////////FOR EMPLOYEES//////////////
$i = 0; $digits = 5;
//Fetch details of product
$fname = $row["fname"];
$lname = $row["lname"];
$email = $row["email"];
$password = $row["password"];
$position = $row["position"];
$location = $row["location"];
switch ($position) {
case "Wholesale Manager":
$pin = "W";
break;
case "Local Manager":
$pin = "L";
break;
case "Sales Representative":
$pin = "S";
break;
default:
echo "Invalid position";
}
while($i < $digits){
//generate a random number between 0 and 9.
$pin .= mt_rand(1, 9);
$i++;
}
//echo $pin;
//Inserting fetched data
$query = "INSERT INTO employee (emp_no, fname, lname, email, password, location) VALUES ('$pin', '$fname', '$lname', '$email', '$password', '$location')";
if(!(mysqli_query($dbconnect, $query)))
{
die('Error : Query Not Executed. Please Fix the Issue! ' . mysqli_error($dbconnect));
} else
{
echo "Data Inserted Successully!!!";
}
}
}else
{
echo "not array";
}
?>