HEX
Server: LiteSpeed
System: Linux s1321.sgp1.mysecurecloudhost.com 4.18.0-513.11.1.lve.el8.x86_64 #1 SMP Thu Jan 18 16:21:02 UTC 2024 x86_64
User: usas (2047)
PHP: 8.2.32
Disabled: NONE
Upload Files
File: /home/usas/swastikenterprises.usas.in/Admin/update-customer.php
<?php
include 'Database/config.php';

if (isset($_POST['submit'])) {
    $cid = $_GET['id'];

    // Sanitize customer fields
    $cust_name      = mysqli_real_escape_string($conn, $_POST['cust_name']);
    $cust_address   = mysqli_real_escape_string($conn, $_POST['cust_address']);
    $cust_no        = mysqli_real_escape_string($conn, $_POST['cust_no']);
    $cust_mail      = mysqli_real_escape_string($conn, $_POST['cust_mail']);
    $purchase_date  = mysqli_real_escape_string($conn, $_POST['purchase_date']);
    $product_name   = mysqli_real_escape_string($conn, $_POST['product_name']);
    $product_category = mysqli_real_escape_string($conn, $_POST['product_category']);
    $install_date   = mysqli_real_escape_string($conn, $_POST['install_date']);
    $service        = mysqli_real_escape_string($conn, $_POST['service']);
    $service_month  = isset($_POST['service_month']) ? mysqli_real_escape_string($conn, $_POST['service_month']) : null;

    // ----------------
    // 1. Update customer table
    // ----------------
    $sql2 = "UPDATE `customers` SET 
                `cust_name`='{$cust_name}',
                `cust_address`='{$cust_address}',
                `cust_ph`='{$cust_no}',
                `cust_mail`='{$cust_mail}',
                `cust_pdate`='{$purchase_date}',
                `cust_model`='{$product_name}',
                `cust_insdate`='{$install_date}',
                `cust_product`='{$product_category}'
             WHERE cust_id = '{$cid}'";

    if (!mysqli_query($conn, $sql2)) {
        die("Customer update failed: " . mysqli_error($conn));
    }

    // ----------------
    // 2. Clear old schedules
    // ----------------
    $deleteOld = "DELETE FROM service_schedule WHERE cust_id = '{$cid}'";
    mysqli_query($conn, $deleteOld);

    // ----------------
    // 3. Insert new schedule(s)
    // ----------------
    if ($service == "paid") {
        // Paid service: one row with month
        $insert = "INSERT INTO service_schedule (cust_id, service_type, service_month) 
                   VALUES ('{$cid}', 'paid', '{$service_month}')";
        mysqli_query($conn, $insert);

    } elseif ($service == "free") {
        // Free service: up to 3 dates
        if (isset($_POST['free_dates'])) {
            foreach ($_POST['free_dates'] as $date) {
                if (!empty($date)) {
                    $insert = "INSERT INTO service_schedule (cust_id, service_type, service_date) 
                               VALUES ('{$cid}', 'free', '{$date}')";
                    mysqli_query($conn, $insert);
                }
            }
        }

    } elseif ($service == "amc") {
        // AMC service: up to 15 dates
        if (isset($_POST['amc_dates'])) {
            foreach ($_POST['amc_dates'] as $date) {
                if (!empty($date)) {
                    $insert = "INSERT INTO service_schedule (cust_id, service_type, service_date) 
                               VALUES ('{$cid}', 'amc', '{$date}')";
                    mysqli_query($conn, $insert);
                }
            }
        }
    }

    // ----------------
    // Redirect back to customers page
    // ----------------
    header('Location: customers.php');
    exit;
}
?>










<?php
include 'include/header.php';

?>
<style>
	.gradient-custom {
/* fallback for old browsers */
.card-registration .select-input.form-control[readonly]:not([disabled]) {
font-size: 1rem;
line-height: 2.15;
padding-left: .75em;
padding-right: .75em;
}
.card-registration .select-arrow {
top: 13px;
}

input[type=text]{
	background: #fff;
}
label{
	font-size: 18px;
}
</style>
<section class="vh-100 gradient-custom">
  <div class="container py-5 h-100">
    <div class="row justify-content-center align-items-center h-100">
      <div class="col-12 col-lg-9 col-xl-7">
        <div class="card shadow-2-strong card-registration" style="border-radius: 15px;">
          <div class="card-body p-4 p-md-5">
            <h3 class="mb-4 pb-2 pb-md-0 mb-md-5">Edit Customer</h3>
            <?php
            include 'Database/config.php';
            $cid = $_GET['id'];
            $sql1 = "SELECT 
            customers.cust_id,
            customers.cust_name,
            customers.cust_address,
            customers.cust_ph,
            customers.cust_mail,
            customers.cust_pdate,
            customers.cust_model,
            customers.cust_insdate,
            customers.cust_product,
            service_schedule.id AS schedule_id,
            service_schedule.service_type,
            service_schedule.service_date,
            service_schedule.service_month,
            service_schedule.status
         FROM customers
         LEFT JOIN service_schedule 
         ON customers.cust_id = service_schedule.cust_id
         WHERE customers.cust_id = $cid LIMIT 1";

            $result1 = mysqli_query($conn, $sql1);
            if (mysqli_num_rows($result1) > 0) {
              while ($row1=mysqli_fetch_assoc($result1)) {

            ?>
            <form action="<?php $_SERVER['PHP_SELF'];?>" method = "POST">

              <div class="row">
                <div class="col-md-6 mb-4">

                  <div data-mdb-input-init class="form-outline">
                  	<label class="form-label" for="firstName">Customer Name</label>
                    <input type="text" id="firstName" class="form-control form-control-lg" / name="cust_name" value="<?php echo $row1['cust_name'];?>">
                    
                  </div>

                </div>
                <div class="col-md-6 mb-4">

                  <div data-mdb-input-init class="form-outline">
                  	<label class="form-label" for="lastName">Address</label>
                    <input type="text" id="lastName" class="form-control form-control-lg" / name="cust_address" value="<?php echo $row1['cust_address'];?>">
                    
                  </div>

                </div>
              </div>
              <div class="row">
                <div class="col-md-6 mb-4">

                  <div data-mdb-input-init class="form-outline">
                    <label class="form-label" for="firstName">Customer No</label>
                    <input type="text" id="lastName" class="form-control form-control-lg" / name="cust_no" value="<?php echo $row1['cust_ph'];?>">
                    <!--<select class="form-control form-control-lg" name="blog_category">
                      
                    </select>-->
                    
                  </div>

                </div>
                <div class="col-md-6 mb-4">

                  <div data-mdb-input-init class="form-outline">
                    <label for="birthdayDate" class="form-label">E-mail Id</label>
                    <input type="text" id="lastName" class="form-control form-control-lg" / name="cust_mail" value="<?php echo $row1['cust_mail'];?>">
                    
                  </div>
                </div>
              </div>

                 <div class="row">
    <!-- Product Category -->
    <div class="col-md-6 mb-4">
        <div data-mdb-input-init class="form-outline">
            <label class="form-label">Product Category</label>
            <select class="form-control form-control-lg" name="product_category" id="product_category" required>
                <option value="" selected disabled>Select Category</option>
                <?php
                include 'Database/config.php';
                $sql = "SELECT * FROM products";
                $result = mysqli_query($conn, $sql);
                if (mysqli_num_rows($result) > 0) {
                    while ($row = mysqli_fetch_assoc($result)) {
                        echo "<option value='{$row['id']}'" .
                             ($row['id'] == $row1['cust_product'] ? ' selected' : '') .
                             ">{$row['product_name']}</option>";
                    }
                }
                ?>
            </select>
        </div>
    </div>

    <!-- Product Name -->
    <div class="col-md-6 mb-4">
        <div data-mdb-input-init class="form-outline">
            <label class="form-label">Product Name</label>
            <select class="form-control form-control-lg" name="product_name" id="product_name" required>
                <option value="" selected disabled>Select Product Name</option>
                <?php
                $sql = "SELECT * FROM product_name";
                $result = mysqli_query($conn, $sql);
                if (mysqli_num_rows($result) > 0) {
                     while ($row = mysqli_fetch_assoc($result)) {
                        echo "<option data-category='{$row['product_id']}' value='{$row['id']}'" .
                             ($row['id'] == $row1['cust_model'] ? ' selected' : '') .
                             ">{$row['products_name']}</option>";
                    }
                }
                ?>
            </select>
        </div>
    </div>
</div>

               <div class="row">
                <div class="col-md-6 mb-4">

                  <div data-mdb-input-init class="form-outline">
                    <label class="form-label" for="firstName">Purchase Date</label>
                    <input type="date" id="lastName" class="form-control form-control-lg" / name="purchase_date" value="<?php echo $row1['cust_pdate'];?>">
                    <!--<select class="form-control form-control-lg" name="blog_category">
                      
                    </select>-->
                    
                  </div>

                </div>

                <div class="col-md-6 mb-4">

                  <div data-mdb-input-init class="form-outline">
                    <label class="form-label" for="firstName">Installation Date</label>
                    <input type="date" id="lastName" class="form-control form-control-lg" / name="install_date" value="<?php echo $row1['cust_insdate'];?>">
                    <!--<select class="form-control form-control-lg" name="blog_category">
                      
                    </select>-->
                    
                  </div>

                </div>
               
              </div>

         <?php
include 'Database/config.php';

$cid = $_GET['id'];

// Get customer + service schedule data
$sql = "SELECT customers.*, service_schedule.service_type, service_schedule.service_date, service_schedule.service_month 
        FROM customers
        LEFT JOIN service_schedule ON customers.cust_id = service_schedule.cust_id
        WHERE customers.cust_id = '$cid'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);

// Fetch all service schedules for this customer (for free/amc multiple dates)
$schedules = [];
$sq2 = "SELECT * FROM service_schedule WHERE cust_id = '$cid'";
$res2 = mysqli_query($conn, $sq2);
while ($r2 = mysqli_fetch_assoc($res2)) {
    $schedules[] = $r2;
}

// Separate free & amc dates
$free_dates = [];
$amc_dates = [];
foreach ($schedules as $sc) {
    if ($sc['service_type'] == 'free') {
        $free_dates[] = $sc['service_date'];
    } elseif ($sc['service_type'] == 'amc') {
        $amc_dates[] = $sc['service_date'];
    }
}
?>

<div class="row">
  <!-- Service Type -->
  <div class="col-md-6 mb-4">
    <div class="form-outline">
      <label for="service" class="form-label">Service Type</label>
      <select class="form-control form-control-lg" name="service" id="update_service">
        <option value="">-- Select Service Type --</option>
        <option value="free" <?php if($row['service_type']=='free') echo "selected"; ?>>Free Service</option>
        <option value="paid" <?php if($row['service_type']=='paid') echo "selected"; ?>>Paid Service</option>
        <option value="amc" <?php if($row['service_type']=='amc') echo "selected"; ?>>AMC</option>
      </select>
    </div>
  </div>

  <!-- Paid Service (Month Dropdown) -->
  <div class="col-md-6 mb-4" id="monthDropdown" style="<?php echo ($row['service_type']=='paid')?'display:block;':'display:none;'; ?>">
    <div class="form-outline">
      <label class="form-label">Next Service (Month)</label>
      <select class="form-control form-control-lg" name="service_month">
        <?php
        $months = ["Not Included","January","February","March","April","May","June","July",
                   "August","September","October","November","December"];
        foreach($months as $m){
            $sel = ($row['service_month']==$m) ? "selected" : "";
            echo "<option value='$m' $sel>$m</option>";
        }
        ?>
      </select>
    </div>
  </div>

  <!-- Free Service (3 Dates) -->
  <div class="col-md-12 mb-4" id="freeServiceDates" style="<?php echo ($row['service_type']=='free')?'display:block;':'display:none;'; ?>">
    <div class="form-outline">
      <label class="form-label">Next Service Dates (Select up to 3)</label>
      <div class="row">
        <?php for($i=0; $i<3; $i++): ?>
          <div class="col-md-4 mb-2">
            <input type="date" class="form-control" name="free_dates[]" 
                   value="<?php echo isset($free_dates[$i]) ? $free_dates[$i] : ''; ?>">
          </div>
        <?php endfor; ?>
      </div>
    </div>
  </div>

  <!-- AMC (15 Dates) -->
  <div class="col-md-12 mb-4" id="amcServiceDates" style="<?php echo ($row['service_type']=='amc')?'display:block;':'display:none;'; ?>">
    <div class="form-outline">
      <label class="form-label">AMC Service Dates (15)</label>
      <div class="row">
        <?php for($i=0; $i<15; $i++): ?>
          <div class="col-md-4 mb-2">
            <input type="date" class="form-control" name="amc_dates[]" 
                   value="<?php echo isset($amc_dates[$i]) ? $amc_dates[$i] : ''; ?>">
          </div>
        <?php endfor; ?>
      </div>
    </div>
  </div>
</div>



               <div class="mt-4 pt-2 w-100">
                <?php
                if (isset($_POST['submit'])) {
                  echo '<p style= "color:red;">' . $error . '</p>';
                }
                ?>
                <input data-mdb-button-init data-mdb-ripple-init class="btn btn-primary btn-lg" type="submit" value="Update Customer" / name = "submit">
              </div>

            </form>
            <?php

              }
            }
?>

          </div>
        </div>
      </div>
    </div>
  </div>
</section>






<?php
include 'include/footer.php';

?>