پویان بازحوضی
6 ماه پیش توسط پویان بازحوضی مطرح شد
0 پاسخ

مشکل در ذخیره کردن اطلاعات در دیتابیس زبان php

<?php
require_once 'DB Config.php';

if (isset($_POST['insert'])) {
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $description = $_POST['description'];
    $delivery = $_POST['delivery'];
    $discount = $_POST['discount'];
    $payment = $_POST['payment'];
    $total = $_POST['total'];
    $number = $_POST['number'];
    $date = $_POST['date'];
    $products = $_POST['product'];

    try {
        $sql = "INSERT INTO customermanagement.order (firstname, lastname, description, delivery, discount, payment, total, number, date, product) VALUES (:firstname, :lastname, :description, :delivery, :discount, :payment, :total, :number, :date, :product)";
        $query = $conn->prepare($sql);
        $query->bindParam(':firstname', $firstname, PDO::PARAM_STR);
        $query->bindParam(':lastname', $lastname, PDO::PARAM_STR);
        $query->bindParam(':description', $description, PDO::PARAM_STR);
        $query->bindParam(':delivery', $delivery, PDO::PARAM_STR);
        $query->bindParam(':discount', $discount, PDO::PARAM_STR);
        $query->bindParam(':payment', $payment, PDO::PARAM_STR);
        $query->bindParam(':total', $total, PDO::PARAM_STR);
        $query->bindParam(':number', $number, PDO::PARAM_STR);
        $query->bindParam(':date', $date, PDO::PARAM_STR);
        $query->bindParam(':product', $products, PDO::PARAM_STR);

        if ($query->execute()) {
            echo 'سفارش با موفقیت ذخیره شد.';
        } else {
            echo 'خطا در ذخیره‌سازی سفارش.';
        }
    } catch (PDOException $e) {
        echo 'Error: ' . $e->getMessage();
    }
}
?>

<html lang="en">
<head>
    <link rel="stylesheet" href="Style.css">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="Image/Logo1.png" rel="icon" type="Image/png" sizes="80x80">
    <title>Order Registration</title>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function () {
            // برای بار اول صفحه
            loadProductData();

            // برای دکمه "Add"
            $("input[name='addButton']").click(function () {
                addProductSelect();
            });

            // برای دکمه "Remove"
            $("input[name='removeButton']").click(function () {
                removeProductSelect();
            });

            function loadProductData() {
                // اجرای درخواست AJAX برای دریافت اطلاعات محصولات از دیتابیس
                $.ajax({
                    url: 'get_product_data.php',
                    method: 'GET',
                    dataType: 'json',
                    success: function (data) {
                        // پر کردن اپشن‌های select با اطلاعات از دیتابیس
                        var productSelect = $("select#محصول");
                        productSelect.empty();
                        $.each(data, function (index, product) {
                            var optionText = product.imagesize + ' ' + product.type + ' ' + product.color + ' ' + product.priceproduct;
                            productSelect.append($('<option>', {
                                value: product.priceproduct,
                                text: optionText
                            }));
                        });
                    },
                    error: function (error) {
                        console.log('Error: ' + JSON.stringify(error));
                    }
                });
            }

            function addProductSelect() {
                var productSelect = $("select#محصول").first().clone();
                productSelect.show();
                loadProductData();
                $("select#محصول").last().after(productSelect);
            }

            function removeProductSelect() {
                var productSelects = $("select#محصول");
                if (productSelects.length > 1) {
                    // حذف سیلکت آخرین مورد
                    productSelects.last().remove();
                }
            }
        });
    </script>

    <script>
        $(document).ready(function () {
            // برای دکمه "ثبت"
            $("form").submit(function (e) {
                e.preventDefault();
                saveOrder();
            });

            function saveOrder() {
                var firstname = $("input[name='firstname']").val();
                var lastname = $("input[name='lastname']").val();
                var description = $("input[name='description']").val();
                var delivery = $("input[name='delivery']").val();
                var discount = $("input[name='discount']").val();
                var payment = $("input[name='payment']").val();
                var total = $("input[name='total']").val();
                var number = $("input[name='number']").val();
                var date = $("input[name='date']").val();

                // گرفتن مقادیر انتخاب شده از تمام سیلکت‌ها
                var selectedProducts = [];
                $("select#محصول").each(function () {
                    selectedProducts.push($(this).val());
                });

                // ارسال اطلاعات به دیتابیس با استفاده از درخواست AJAX
                $.ajax({
                    url: 'save_order.php', // آدرس فایل PHP برای ذخیره داده در دیتابیس
                    method: 'POST',
                    data: {
                        firstname: firstname,
                        lastname: lastname,
                        description: description,
                        delivery: delivery,
                        discount: discount,
                        payment: payment,
                        total: total,
                        number: number,
                        date: date,
                        products: selectedProducts
                    },
                    success: function (response) {
                        alert('سفارش با موفقیت ذخیره شد.');
                        // انجام هرگونه دیگر عملیات مرتبط با ذخیره‌سازی
                    },
                    error: function (error) {
                        alert('خطا در ذخیره‌سازی سفارش.');
                        console.log('Error: ' + JSON.stringify(error));
                    }
                });
            }
        });
    </script>

</head>
<body bgcolor="#94A684">
<center>
    <a href="Nima%20Studio.html" style="text-decoration:none;"><h1 align="center" class="font"><font size="+6"><img src="Image/Logo2.png" width="70" height="45">Nima Studio</font></h1></a>
    <h2 align="center" class="font"><font>Nima customer and order management software is your constant companion</font></h2>
    <hr color="#272829">
    <a href="Customer%20Registration.php"><button class="pun2"><font class="font">Customer Registration</font></button></a>
    <a href="Order.php"><button class="pun2"><font class="font">Order Registration</font></button></a>
    <a href="Customer%20List.php"><button class="pun2"><font class="font">Customer List</font></button></a>
    <a href="Order%20List.php"><button class="pun2"><font class="font">Order List</font></button></a>
    <a href="Products%20List.php"><button class="pun2"><font class="font">Products List</font></button></a>
    <a href="Index.php"><button class="pun2"><font class="font">Exit</font></button></a>
    <hr color="#272829">
</center>
<center><form>
        <h1 class="font">Order Registration</h1>

        <label for="First Name" class="font" style="font-size: x-large">First Name</label> <br>
        <input id="First Name" type="text" name="firstname" placeholder="محمد" class="pun3">

        <br><br>

        <label for="Last Name" class="font" style="font-size: x-large">Last Name</label> <br>
        <input id="Last Name" type="text" name="lastname" placeholder="احمدی" class="pun3">

        <br><br>

        <select id="محصول" class="pun2" name="product">
            <?php
            $get = "SELECT imagesize,type,color,priceproduct FROM customermanagement.pricelist";
            $q = $conn->prepare($get);
            $q->execute();
            $r = $q->fetchAll(PDO::FETCH_OBJ);
            if ($q->rowCount() > 0) {
                foreach ($r as $result) {
                    ?>
                    <option><?php echo $result->imagesize ?>
                        <?php echo $result->type ?>
                        <?php echo $result->color ?>
                        <?php echo $result->priceproduct ?></option>
                <?php }} ?>
        </select>

        <input type="button" name="addButton" value="Add" class="pun2">
        <input type="button" name="removeButton" value="Delete" class="pun2">

        <br><br>

        <label for="Number" class="font" style="font-size: x-large">Number</label> <br>
        <input id="Number" type="number" name="number" class="pun3">

        <br><br>

        <label for="Delivery Date" class="font" style="font-size: x-large">Delivery Date</label> <br>
        <input id="Delivery Date" type="text" name="delivery date" placeholder="15" class="pun3">

        <br><br>

        <label for "Date" class="font" style="font-size: x-large">Date</label> <br>
        <input id="Date" type="date" name="date" placeholder="1402/01/21" class="pun3">

        <br><br>

        <label for="Discount" class="font" style="font-size: x-large">Discount</label> <br>
        <input id="Discount" type="number" name="discount" placeholder="10000" class="pun3">

        <br><br>

        <label for="Payment" class="font" style="font-size: x-large">Payment</label> <br>
        <input id="Payment" type="number" name="payment" placeholder="150000" required class="pun3">

        <br><br>

        <label for="Total" class="font" style="font-size: x-large">Total</label> <br>
        <input id="Total" type="number" name="total" placeholder="300000" required class="pun3">

        <br><br>

        <input type="submit" name="insert" value="Add" class="pun2">

    </form></center>
</body>
</html>

تو این کد وقتی ثبت سفارش رو میزنم خطا ذخیره سازی اطلاعات در دیتابیس میده میتونید کمکم کنید

</body></html>


ثبت پرسش جدید

به همدیگه کمک کنیم

به پویان بازحوضی کمک کنید تا مشکل خودش را حل کند؛ این‌طور می‌توانیم با هم پیشرفت کنیم.

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام