· 5 years ago · Jul 08, 2020, 08:14 PM
1<!DOCTYPE html>
2<html>
3<head>
4 <meta charset="UTF-8">
5 <title>Home</title>
6</head>
7<body>
8 <center><h1>Cable database</h1></center>
9 <?php
10 /*The username is root and the password is mysql this
11 *should be changed to something more secure later
12 */
13 $connection = mysqli_connect('localhost', 'root', 'mysql');
14 if($connection){
15 echo "Connected";
16 }else{
17 die("database connection failed");
18 }
19 //create database if it doesn't exist already
20 $createdbQuery = "CREATE DATABASE IF NOT EXISTS cabledb";
21
22
23 //select database
24 mysqli_select_db($connection, cabledb);
25
26 /*
27 //create table
28 $createTableQuery = "CREATE TABLE CableTable (
29 id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
30 CableType VARCHAR(8),
31 RoadName VARCHAR(14),
32 CableLength INT(255),
33 CableNumber INT(255),
34 CableFault INT(2)
35
36 )";
37 */
38 //create table
39 $createTableQuery = "CREATE TABLE IF NOT EXISTS `cable` (
40 id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
41 CableTubes INT (255),
42 CableCores INT(12),
43 CableCoreColourCode INT(255),
44 StartLocationGridRef INT(255),
45 EndLocationGridRef INT(255),
46 ClosureId VARCHAR(5),
47 ClosureTray INT(24),
48 TraySplice INT(24)
49 )";
50
51
52 if(mysqli_query($connection, $createdbQuery)){
53 echo " cable db created successfully";
54 } else {
55 echo " error creating database";
56 }
57
58 //mysqli_query($connection, $selectdbQuery);
59
60
61 if(mysqli_query($connection, $createTableQuery)){
62 echo " cable table created successfully";
63 } else {
64 echo " error creating table";
65 }
66 //use htmlspecialchars to prevent scripts being entered into form
67 if($_SERVER['REQUEST_METHOD'] === 'POST'){
68 echo "CableTubes: ". htmlspecialchars($_POST['cableTubes']);
69 echo "CableCores: ". htmlspecialchars($_POST['cableCores']);
70 echo "CableCoreColourCode: ". htmlspecialchars($_POST['cableCoreColourCode']);
71 echo "StartLocationGridRef: ". htmlspecialchars($_POST['startLocationGridRef']);
72 echo "EndLocationGridRef: ". htmlspecialchars($_POST['endLocationGridRef']);
73 echo "ClosureId: ". htmlspecialchars($_POST['closureId']);
74 echo "ClosureTray: ". htmlspecialchars($_POST['closureTray']);
75 echo "TraySplice: ". htmlspecialchars($_POST['traySplice']);
76
77 $cableTubes = $POST['cableTubes'];
78 $cableCores = $POST['cableCores'];
79 $cableCoreColourCode = $POST['cableCoreColourCode'];
80 $startLocationGridRef = $POST['startLocationGridRef'];
81 $endLocationGridRef = $POST['endLocationGridRef'];
82 $closureId = $POST['closureId'];
83 $closureTray = $POST['closureTray'];
84 $traySplice = $POST['traySplice'];
85
86 $query = "INSERT INTO cabledb.CableTable(cablesTubes, cableCores, cableCoreColourCode, startLocationGridRef";
87 $query .= "endLocationGridRef, closureId, closureTray, traySplice) ";
88 $query .= "VALUES ('cablesTubes', 'cableCores', 'cableCoreColourCode', 'startLocationGridRef'";
89 $quey .= "'endLocationGridRef', 'closureId', 'closureTray', 'traySplice') ";
90
91
92
93
94
95 $result = mysqli_query($connection, $query);
96 /*
97 if(!$result){
98 die('Query failure' );
99 }
100 */
101
102 $getSQL = "SELECT * FROM `cable` ";
103 $result = mysqli_query($connection, $getSQL);
104 //echo $result;
105 }
106
107
108
109 ?>
110
111
112 <form method="post">
113 <div>
114 <label for="cableType">Cable type</label>
115 <!--<input id="cableType" name="cableType" autofocus />-->
116 <input type="radio" name="cableType"
117 <?php if(isset($cable) && $cable=="fibre")echo"checked";?>
118 value="fibre">fibre
119 <input type="radio" name="cableType"
120 <?php if(isset($cable) && $cable=="metallic")echo"checked";?>
121 value="metallic">metallic
122 </div>
123 <div>
124 <label for="cableTubes">Cable tube</label>
125 <input id="cableTubes" name="cableTubes" autofocus />
126 </div>
127 <div>
128 <label for="cableCores">Cable cores</label>
129 <input id="cableCores" name="cableCores" autofocus />
130 </div>
131 <div>
132 <label for="cableCoreColourCode"">Cable colour code</label>
133 <input id="cableCoreColourCode" name="cableCoreColourCode" autofocus />
134 </div>
135
136 <div>
137 <label for="startLocationGridRef"">Start location grid reference</label>
138 <input id="startLocationGridRef" name="startLocationGridRef" autofocus />
139 </div>
140
141 <div>
142 <label for="endLocationGridRef"">End location grid reference</label>
143 <input id="endLocationGridRef" name="endLocationGridRef" autofocus />
144 </div>
145
146 <div>
147 <label for="closureId"">Closure ID</label>
148 <input id="closureId" name="closureId" autofocus />
149 </div>
150
151 <div>
152 <label for="closureTray"">Closure tray</label>
153 <input id="closureTray" name="closureTray" autofocus />
154 </div>
155
156 <div>
157 <label for="traySplice"">Tray Splice</label>
158 <input id="traySplice" name="traySplice" autofocus />
159 </div>
160
161 <label for="Has fault">Has fault</label>
162 <input type="radio" name="hasFault"
163 <?php if(isset($hasFault) && $hasFault=="true")echo"checked";?>
164 value="hasFault">faulty
165 <input type="radio" name="hasFault"
166 <?php if(isset($hasFault) && $hasFault=="false")echo"checked";?>
167 value="hasFault">no fault
168 <div>
169 <button type="submit">Submit</button>
170 </div>
171 </form>
172
173 <?php
174 if(isset($_POST['submit'])){
175 $cableTubes = $POST['cableTubes'];
176 $roadName = $POST['roadName'];
177
178 if($cableTubes && $cableCores){
179 echo $cableTubes;
180 echo $cableCores;
181 }
182 }
183
184
185
186
187
188
189 ?>
190
191 <?php
192//$sql "INSERT INTO cabledb(cableType, roadName, cableLength, cableNum)
193 //VALUES(cableType, roadName, cableLength, cableNum)";
194 ?>
195 <ul>
196 <!--<?php/* foreach ($cableType as $cableType): ?>
197 <li><?php echo htmlspecialchars($cableType); ?></li>
198 <li><?php echo htmlspecialchars($roadName); ?></li>
199 <li><?php echo htmlspecialchars($cableLength); ?></li>
200 <li><?php echo htmlspecialchars($cableNum); ?></li>
201 <?php endforeach; */ ?>-->
202 </ul>
203</body>
204</html>