-
Notifications
You must be signed in to change notification settings - Fork 10
/
addNewCustomer.php
70 lines (55 loc) · 1.91 KB
/
addNewCustomer.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
64
65
66
67
68
69
70
<?php
$success = '';
$error = '';
if (isset($_POST['submit'])) {
// Define $username and $password
$surname = $_POST['surname'];
$forename = $_POST['forename'];
$town = $_POST['town'];
$county = $_POST['county'];
$tel = $_POST['telephone'];
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$conn= mysqli_connect("localhost", "root", "", "compsys");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// To protect MySQL injection for Security purpose
$surname = stripslashes($surname );
$forename = stripslashes($forename);
$town = stripslashes($town);
$county = stripslashes($county);
$tel = stripslashes($tel);
$surname = mysqli_real_escape_string($conn, $surname );
$forename = mysqli_real_escape_string($conn, $forename);
$town = mysqli_real_escape_string($conn, $town);
$county = mysqli_real_escape_string($conn, $county);
$tel = mysqli_real_escape_string($conn, $tel);
if ( is_numeric($tel) ) {
$query = "SELECT * FROM customers WHERE tel = '$tel'";
$valid = mysqli_query($conn, $query);
if (!$valid) {
$error = "Could not connect to the database!";
}
if (mysqli_num_rows($valid) == 0 ) {
$sql = "INSERT INTO customers (surname, forename, town, county, tel)
VALUES ('$surname', '$forename', '$town', '$county', '$tel');";
$res = mysqli_query($conn, $sql);
if (!$res) {
$error = "Error registering....";
}
if (mysqli_affected_rows($conn) == 1) {
$success = "Customer created successfully. Redirecting.....";
header("refresh:5; url=customer.php");
} else {
$error = ("Could not register due to system error!");
}
} else {
$error = "The customer already exist in the system.";
}
} else {
$error = "Telephone number should numeric!";
}
mysqli_close($conn);
}
?>