· 5 years ago · Jul 07, 2020, 12:12 PM
1 $connection = mysqli_connect('localhost', 'root', 'mysql');
2 if($connection){
3 echo "Connected";
4 }else{
5 die("database connection failed");
6 }
7 //create database if it doesn't exist already
8 $createdbQuery = "CREATE DATABASE IF NOT EXISTS cabledb";
9 //$selectdbQuery = "SELECT cabledb";
10
11
12
13 //create table
14 $createTableQuery = "CREATE TABLE CableTable (
15 id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
16 CableType VARCHAR(8),
17 RoadName VARCHAR(14),
18 CableLength INT(255),
19 CableNumber INT(255),
20 CableFault INT(2)
21
22 )";
23
24
25 if(mysqli_query($connection, $createdbQuery)){
26 echo " cable db created successfully";
27 } else {
28 echo " error creating database";
29 }
30
31 //mysqli_query($connection, $selectdbQuery);
32
33
34 if(mysqli_query($connection, $createTableQuery)){
35 echo " cable table created successfully";
36 } else {
37 echo " error creating table";
38 }
39 //use htmlspecialchars to prevent scripts being entered into form
40 if($_SERVER['REQUEST_METHOD'] === 'POST'){
41 echo "Cable type: ". htmlspecialchars($_POST['cableType']);
42 echo "Road name: ". htmlspecialchars($_POST['roadName']);
43 echo "cable length: ". htmlspecialchars($_POST['cableLength']);
44 echo "cable Number #: ". htmlspecialchars($_POST['cableNum']);
45 echo "cable has fault: ". htmlspecialchars($_POST['hasFault']);
46
47 $cableType = $POST['cableType'];
48 $roadName = $POST['roadName'];
49 $cableLength = $POST['cableLength'];
50 $cableLength = $POST['cableNum'];
51 $cableLength = $POST['hasFault'];
52
53 echo $cableType, "Hello cable type";
54 echo $roadName;
55
56 $query = "INSERT INTO cabledb.CableTable(cableType, roadName, cableLength, cableNum, hasFault) ";
57 $query .= "VALUES ('cableType', '$roadName', '$cableLength', '$cableNum', '$hasFault') ";
58
59
60
61 $result = mysqli_query($connection, $query);