forked from AKIRA85/TPC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentsInitialise.php
More file actions
141 lines (110 loc) · 2.98 KB
/
Copy pathStudentsInitialise.php
File metadata and controls
141 lines (110 loc) · 2.98 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
<?php
/*
* File: StudentsInitialise.php
*
* This script initialises the students TABLE
*
*
*=====================================
*/
{ // Secure Connection Script
$dbSuccess = false;
$dbConnected = mysql_connect('localhost','root','');
if ($dbConnected) {
$dbSelected = mysql_select_db('tpc',$dbConnected);
if ($dbSelected) {
$dbSuccess = true;
}
}
// END Secure Connection Script
}
if ($dbSuccess) {
{ // Table Definition
$tableName = "students";
$CSVfilename = "csvStudent";
$tableField = array(
'name',
'rollno',
'email',
'mobileNo',
'programme',
'cpi',
'password'
);
$numFields = 7;
$createTable_SQL = "
CREATE TABLE tpc.students
( name VARCHAR( 250 ) NOT NULL, rollno VARCHAR( 8 ) NOT NULL PRIMARY KEY,
email VARCHAR( 50 ) NULL, mobileNo VARCHAR( 20 ) NULL,
programme VARCHAR( 50 ) NULL,
cpi FLOAT DEFAULT '0', password VARCHAR( 50 ) NULL
)";
echo $createTable_SQL.'<br>';
}
// =======^^^^^^^^^^^^^^^^^^^^^^^========= End of Definition Part ======^^^^^^^^^=====
{ // read CSV data file
$file = fopen($CSVfilename, "r");
$i = 0;
while(!feof($file))
{
$thisLine = fgets($file);
$tableData[$i] = explode(",", $thisLine);
$i++;
}
fclose($file);
$numRows = sizeof($tableData);
}
echo '$numRows : '.$numRows.'<br />';
echo '$tableField[$numFields-1] : '.$tableField[$numFields-1].'<br />';
{ // DROP table
$drop_SQL = "DROP TABLE $tableName";
if (mysql_query($drop_SQL)) {
echo "DROP ".$tableName."' - Successful.";
} else {
echo "DROP $tableName - Failed.";
}
}
echo "<br /><hr /><br />";
{ // CREATE table
if (mysql_query($createTable_SQL)) {
echo "'CREATE ".$tableName."' - Successful.";
} else {
echo "'CREATE ".$tableName."' - Failed.";
}
}
echo "<br /><hr /><br />";
$table_SQLinsert = "INSERT INTO ".$tableName." (";
//$table_SQLinsert .= "x";
foreach($tableField as $tableFieldName) {
$table_SQLinsert .= $tableFieldName;
if($tableFieldName <> $tableField[$numFields-1]) {
$table_SQLinsert .= ", ";
}
}
$table_SQLinsert .= ") VALUES ";
$indx = 0;
while($indx < $numRows) {
$table_SQLinsert .= "(";
foreach($tableField as $key => $tableFieldName) {
$table_SQLinsert .= "'".$tableData[$indx][$key]."'";
if($tableFieldName <> $tableField[$numFields-1]) {
$table_SQLinsert .= ", ";
}
}
$table_SQLinsert .= ") ";
if ($indx < ($numRows - 1)) {
$table_SQLinsert .= ",\n";
}
$indx++;
}
{ // Echo and Execute the SQL and test for success
echo "<strong><u>SQL:<br /></u></strong>";
echo $table_SQLinsert."<br /><br />";
if (mysql_query($table_SQLinsert)) {
echo "was SUCCESSFUL.<br /><br />";
} else {
echo "FAILED.<br /><br />";
}
}
}
?>