add_customer.php (3545B)
1 <!DOCTYPE html> 2 <html lang="eng"> 3 <head> 4 <title>Add user</title> 5 <!-- Bootstrap CSS --> 6 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> 7 <link rel="stylesheet" type="text/css" href="/css/homepage.css"> 8 </head> 9 <?php 10 include "views/navbar.php"; 11 include "utils/autoloader.php"; 12 if(!in_array(1, $_SESSION['user_permissions'])){ 13 header('Location: /dashboard'); 14 exit; 15 } 16 $db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "wap2"); 17 $msg = ""; 18 if ($_SERVER["REQUEST_METHOD"] == "POST") { 19 $errors = array(); // initialize an empty array to store errors 20 21 // Check if voornaam is set and not empty 22 if (isset($_POST['voornaam']) && !empty($_POST['voornaam'])) { 23 $fname = $_POST['voornaam']; 24 } else { 25 $errors[] = "Voornaam is required"; 26 } 27 28 // Check if achternaam is set and not empty 29 if (isset($_POST['achternaam']) && !empty($_POST['achternaam'])) { 30 $lname = $_POST['achternaam']; 31 } else { 32 $errors[] = "Achternaam is required"; 33 } 34 // Check if email is set and not empty 35 if (isset($_POST['email']) && !empty($_POST['email'])) { 36 $email = $_POST['email']; 37 } else { 38 $errors[] = "E-mail is required"; 39 } 40 // Check if there are any errors 41 if (count($errors) > 0) { 42 // Print out the errors 43 foreach ($errors as $error) { 44 $msg .= $error . "<br>"; 45 } 46 } else { 47 //create a database object with table customer 48 $c = $db->get(Customer::class); 49 //check if customer already exists 50 if($c->where("email", $email)){ 51 $msg = "this user already exists: " . $c->email . " " . $c->first_name . " " . $c->last_name; 52 }else{ 53 $c = $db->get(Customer::class); 54 //set new user data 55 $c->first_name = $fname; 56 $c->last_name = $lname; 57 $c->email = $email; 58 //add user with the add function 59 if($c->add()){ 60 $msg = "added to the db this info:<br> email: {$c->email}<br> firstname: {$c->first_name}<br> lastname: {$c->last_name}"; 61 }; 62 } 63 } 64 } 65 ?> 66 <body> 67 <div class="container"> 68 <h1>Add customer</h1> 69 70 <form action="add_customer" method="post"> 71 <div class="mb-3"> 72 <label for="voornaam" class="form-label"><b>Voornaam:</b></label> 73 <input type="text" class="form-control" name="voornaam" id="voornaam" placeholder="Voornaam"> 74 </div> 75 <div class="mb-3"> 76 <label for="achternaam" class="form-label"><b>Achternaam:</b></label> 77 <input type="text" class="form-control" name="achternaam" id="achternaam" placeholder="Achternaam"> 78 </div> 79 <div class="mb-3"> 80 <label for="email" class="form-label"><b>Email:</b></label> 81 <input type="text" class="form-control" name="email" id="email" placeholder="Email"> 82 </div> 83 <button type="submit" class="btn btn-primary" name="submit">Add customer</button> 84 </form> 85 <?php echo $msg;?> 86 </div> 87 </body> 88 </html>