ok

Mini Shell

Direktori : /proc/self/root/home2/selectio/www/wedding-info-new/admin/
Upload File :
Current File : //proc/self/root/home2/selectio/www/wedding-info-new/admin/adv_new.php

php
Copy code
<?php
include 'header.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    try {
        error_log('submit starts......');
        // Validate and sanitize inputs
        $positionId = isset($_POST['inputPosition']) ? $_POST['inputPosition'] : null;
        $userId = isset($_POST['seller']) ? $_POST['seller'] : null;
        $link = isset($_POST['link']) ? $_POST['link'] : null;
        $status = isset($_POST['status']) ? $_POST['status'] : null;
        $products = isset($_POST['inputProducts']) ? $_POST['inputProducts'] : [];
        $serializedProducts = implode(',',$products);
        // File upload
        if(isset($_FILES['image'])) {
            $image = $_FILES['image']['name'];
            $image_tmp = $_FILES['image']['tmp_name'];
            $upload_dir = "uploads/";
            $target_file = $upload_dir . basename($image);
            $image_path = "uploads/".$image;
            
            $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
            if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
                && $imageFileType != "gif" ) {
                throw new Exception("Sorry, only JPG, JPEG, PNG & GIF files are allowed.");
            }

            // Move file to uploads directory
            if (!move_uploaded_file($image_tmp, $target_file)) {
                throw new Exception("File upload failed");
            }
        } else {
            throw new Exception("No image uploaded");
        }
  
        // Serialize the array to store in the database
        /*$serializedProducts = json_encode($products);*/
        
        
        

        // Start transaction
        $pdo->beginTransaction();


        // Prepare SQL statement for the advertisement table
        $sqlAdvertisement = "INSERT INTO tbl_advertisement (position, seller, link, status, images, product) 
                             VALUES (?, ?, ?, ?, ?, ?)";
        $stmtAdvertisement = $pdo->prepare($sqlAdvertisement);
        $stmtAdvertisement->execute([$positionId, $userId, $link, $status, $image, $serializedProducts]);

        // Commit transaction
        $pdo->commit();

        // Redirect to a success page or do something else upon successful submission
        header("Location: {$_SERVER['PHP_SELF']}");
        exit();
    } catch (Exception $e) {
        // Rollback transaction on error
        $pdo->rollback();
        // Handle error
        echo "Error: " . $e->getMessage();
    }
}

// Fetch positions
$sql = "SELECT * FROM tbl_adv_position WHERE status = 1";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Fetch verified sellers
$sql = "SELECT * FROM tbl_seller WHERE verification_status = 1 AND status = 1";
$stmt2 = $pdo->prepare($sql);
$stmt2->execute();
$sellers = $stmt2->fetchAll(PDO::FETCH_ASSOC);


if($_GET['id']){
$sql = "SELECT * FROM tbl_advertisement WHERE id = '". $_GET['id']."'";
$stmt3 = $pdo->prepare($sql);
$stmt3->execute();
$edit = $stmt3->fetch(PDO::FETCH_ASSOC);

    
}


?>

<!-- Your HTML content -->

<div class="page-wrapper">
    <div class="page-content">
        <div class="card">
            <div class="card-body p-4">
                <h5 class="card-title">Add New Advertisement</h5>
                <hr/>
                <div class="form-body mt-4">
                    <form method="post" action="" enctype="multipart/form-data">
                        <div class="row">
                            <div class="col-lg-12">
                                <div class="border border-3 p-4 rounded">
                                    <div class="row g-3">
                                        <div class="col-md-6">
                                            <label for="inputPosition" class="form-label">Position</label>
                                            <select class="form-select" id="inputPosition" name="inputPosition">
                                                <option value="">Select the option</option>
                                                <?php foreach ($rows as $position) { ?>
                                                    <option value="<?= $position['id'] ?>" <?= ($position['id'] == $edit['position'] ? 'selected' : '') ?>><?= $position['text'] ?></option>
                                                <?php } ?>
                                            </select>
                                        </div>
                                        <div class="col-md-6">
                                            <label for="seller" class="form-label">User</label>
                                            <select class="form-select" id="seller" name="seller">
                                                <option value="">Select the option</option>
                                                <?php foreach ($sellers as $seller) { ?>
                                                    <option value="<?= $seller['id'] ?>"<?= ($seller['id'] == $edit['seller'] ? 'selected' : '') ?>    ><?= $seller['profile_name'] ?></option>
                                                <?php } ?>
                                            </select>
                                        </div>
                                        <div class='col-md-6'>
                                            <label for="inputProducts" class="form-label">Products</label>
                                            <select class="form-select" id="inputProducts" name="inputProducts[]" <?=$edit['product']?> multiple="multiple">
                                                <!-- Options will be populated dynamically -->
                                            </select>
                                        </div>
                                        <div class="col-md-6">
                                            <label for="link" class="form-label">Link</label>
                                            <input class="form-control" type="text" name="link" id="link" <?=$edit['link']?> />
                                        </div>
                                        <div class="col-md-6">
                                            <label for="image" class="form-label">Image</label>
                                            <input type="file" class="form-control" name="image" id="image"  />
                                            <input type='hidden' value='<?=$edit['images']?>'  name='old_image' />
                                        </div>
                                        <div class="col-md-6">
                                            <label for="status" class="form-label">Status</label>
                                            <select class="form-select" name="status" id="status">
                                                <option value="">Select the status</option>
                                                <option <?= (1 == $edit['status'] ? 'selected' : '') ?> value='1'>Active</option>
                                                <option <?= (2 == $edit['status'] ? 'selected' : '') ?> value='2'>In - Active</option>
                                            </select>
                                        </div>
                                        <div class="col-12">
                                            <div class="d-grid">
                                                <button type="submit" class="btn btn-primary">Save Advertisement</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div><!--end row-->
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php' ?>

<script src="assets/js/jquery.min.js"></script>
<script src="assets/plugins/simplebar/js/simplebar.min.js"></script>
<script src="assets/plugins/input-tags/js/tagsinput.js"></script>
<script src="assets/plugins/metismenu/js/metisMenu.min.js"></script>
<script src="assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>


<script>
    $(document).ready(function () {
        var ids = '<?php echo isset($edit['seller']) ? $edit['seller'] : ''; ?>'; // Added single quotes and isset check
        var product = '<?php echo isset($edit['product']) ? $edit['product'] : ''; ?>'; // Added single quotes and isset check
        if(ids){
            var id = ids;
            var products = product; // Added semicolon here
            alert(products + id);
            productf(id, products); // Pass id to product function
        }

        $('#inputProducts').empty();
        $('#inputProducts').select2();

        $('#seller').change(function () {
            $('#inputProducts').empty();
            var id = $(this).val();
            productf(id); // Pass id to product function
        });

        function productf(id, products = null) {
             $.ajax({
                type: 'POST',
                url: 'ui-api/seller-dropdown.php',
                data: {
                    id: id,
                    products: products
                },
                success: function (response) {
                    $('#inputProducts').html(response);
                    $('#inputProducts').select2(); // Initialize select2 after updating options
                }
            });
        }
    });
</script>

Zerion Mini Shell 1.0