-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.php
63 lines (54 loc) · 1.87 KB
/
init.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
<?php
// Collect data from input.csv.
function getInfo($file) {
$itemAttributes = fgetcsv($file, 1000, ",");
return $itemAttributes;
}
$userFile = fopen("input.csv", "r") or die("<br>Error opening file."); // Open input.csv.
$counter = 0;
while (!feof($userFile)) {
$lineNumber[$counter] = getInfo($userFile); // This 2D array will populate the table in DB.
$counter++;
}
fclose($userFile);
// Create connection
$conn = new mysqli('localhost', 'root', '');
// Check connection
if ($conn->connect_error) {
die("<br>Connection failed: " . $conn->connect_error);
}
// Create database
$createDB = "CREATE DATABASE IF NOT EXISTS assignment8";
if ($conn->query($createDB) === TRUE) {
echo "<br>Database created successfully";
} else {
echo "<br>Error creating database: " . $conn->error;
}
// Select Database
$selectDB = mysqli_select_db($conn, "assignment8");
// sql to create table
$createTable = "CREATE TABLE IF NOT EXISTS ITEMS (
ItemNumber INT(3) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Artwork VARCHAR(45) NOT NULL UNIQUE,
Medium VARCHAR(45),
Artist VARCHAR(45),
Price VARCHAR(45),
Description VARCHAR(100)
)";
if ($conn->query($createTable) === TRUE) {
echo "<br>Table created successfully";
} else {
echo "<br>Error creating table: " . $conn->error;
}
// Insert values.
foreach ($lineNumber as $row) {
if ($row != NULL) {
$insertValues = "INSERT INTO ITEMS (Artwork, Medium, Artist, Price, Description)
VALUES ('$row[1]', '$row[2]', '$row[3]', '$row[4]', '$row[5]');";
}
if ($conn->query($insertValues) === TRUE) {
echo "<br>New record created successfully";
}
}
$conn->close();
?>