billing2/customers/.hta_slug/new.php

79 lines
4.3 KiB
PHP

<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card shadow-lg">
<div class="card-header bg-primary text-white text-center">
<h4>Customer Registration</h4>
</div>
<div class="card-body">
<?php
require('../.hta_config/conf.php');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
$customerId = str_replace('.', '', uniqid('cust_', true));
$userPassword = md5($_POST['password']);
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $db->prepare("INSERT INTO customers (name, mobile, email, customerId) VALUES (:name, :mobile, :email, :customerId)");
$stmt->bindParam(':name', $_POST['name']);
$stmt->bindParam(':mobile', $_POST['mobile']);
$stmt->bindParam(':email', $_POST['email']);
$stmt->bindParam(':customerId', $customerId);
$stmt2 = $db->prepare("INSERT INTO users (name, mobile, email, type, password, customerId) VALUES (:name, :mobile, :email, :type, :password, :customerId)");
$stmt2->bindParam(':name', $_POST['name']);
$stmt2->bindParam(':mobile', $_POST['mobile']);
$stmt2->bindParam(':email', $_POST['email']);
$stmt2->bindParam(':type', $_POST['type']);
$stmt2->bindParam(':password', $userPassword);
$stmt2->bindParam(':customerId', $customerId);
if ($stmt->execute()) {
$stmt2->execute();
echo '<div class="alert alert-success">New Customer <strong>' . htmlspecialchars($_POST['name']) . '</strong> created successfully.</div>';
} else {
echo '<div class="alert alert-danger">Error inserting into customers table: ' . $stmt->errorInfo()[2] . '</div>';
}
} catch (PDOException $e) {
echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
}
}
?>
<form method="POST">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" id="name" name="name" class="form-control" required>
</div>
<div class="form-group">
<label for="mobile">Mobile:</label>
<input type="text" id="mobile" name="mobile" class="form-control" required>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" name="email" class="form-control" required>
</div>
<div class="form-group">
<label for="type">User Type:</label>
<select class="form-control" name="type" id="type" require>
<option value="">-Select-</option>
<option value="user">User</option>
<option value="admin">Admin</option>
</select>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" name="password" class="form-control" required>
</div>
<button type="submit" class="btn btn-success btn-block mt-2">Save Customer</button>
</form>
</div>
</div>
</div>
</div>
</div>