如何用PHP实现激光胶片报价页及订单管理系统(增删改查)
Hey there! Let's build out your laser film quote page and order management system using PHP. I’ll walk you through the full implementation, including finishing up your existing JS calculation logic and adding all the required CRUD (Create, Read, Update, Delete) features.
1. Database Setup
First, let's create a database table to store all quote data. I’ll use MySQL for this example—adjust the credentials later to match your setup.
CREATE TABLE film_quotes ( id INT AUTO_INCREMENT PRIMARY KEY, quantity INT NOT NULL, unit_price DECIMAL(10,2) NOT NULL, laser_price DECIMAL(10,2) NOT NULL, total_amount DECIMAL(10,2) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP );
2. Frontend with Complete JS Calculation
I’ll finish your existing JS function and add dynamic row functionality so users can add multiple quote items at once. Here’s the full frontend code (save as index.php):
<!DOCTYPE html> <html> <head> <title>Laser Film Quote Manager</title> <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script> <style> .quote-row { margin: 12px 0; padding: 10px; border: 1px solid #eee; border-radius: 4px; } input { padding: 6px; margin: 0 8px; width: 120px; } button { padding: 7px 14px; cursor: pointer; margin-right: 8px; } table { margin-top: 20px; border-collapse: collapse; width: 100%; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #f5f5f5; } </style> </head> <body> <h1>Laser Film Quote & Order Management</h1> <!-- New Quote Form --> <form id="quoteForm" method="POST" action="process.php"> <div class="quote-row"> <input type="number" name="qty[]" id="qty_1" placeholder="Quantity" min="1" oninput="calc(this)"> <input type="number" step="0.01" name="unitprice[]" id="unitprice_1" placeholder="Unit Price" min="0" oninput="calc(this)"> <input type="number" step="0.01" name="laspprice[]" id="laspprice_1" placeholder="Laser Price" min="0" oninput="calc(this)"> <input type="number" step="0.01" name="total[]" id="total_1" placeholder="Total" readonly> </div> <button type="button" id="addRow">Add Another Item</button> <button type="submit" name="saveQuote">Save Quotes</button> </form> <!-- Existing Quotes List --> <h2>Current Quotes</h2> <table> <tr> <th>ID</th> <th>Quantity</th> <th>Unit Price</th> <th>Laser Price</th> <th>Total</th> <th>Actions</th> </tr> <?php // Database connection (replace with your credentials) $conn = mysqli_connect('localhost', 'your_username', 'your_password', 'your_database'); if (!$conn) die("Connection failed: " . mysqli_connect_error()); // Fetch and display quotes $result = mysqli_query($conn, "SELECT * FROM film_quotes ORDER BY created_at DESC"); while($row = mysqli_fetch_assoc($result)) { echo "<tr> <td>{$row['id']}</td> <td>{$row['quantity']}</td> <td>${$row['unit_price']}</td> <td>${$row['laser_price']}</td> <td>${$row['total_amount']}</td> <td> <a href='edit.php?id={$row['id']}'>Edit</a> | <a href='process.php?delete={$row['id']}' onclick='return confirm(\"Delete this quote?\")'>Delete</a> </td> </tr>"; } mysqli_close($conn); ?> </table> <script type="text/javascript"> // Complete your calculation function function calc(control){ var row = $(control).closest('.quote-row'); var qty = parseInt(row.find("input[id^='qty']").val()) || 0; var price = parseFloat(row.find("input[id^='unitprice']").val()) || 0; var laspprice = parseFloat(row.find("input[id^='laspprice']").val()) || 0; // Calculate total: (Unit Price + Laser Price) × Quantity var total = (price + laspprice) * qty; row.find("input[id^='total']").val(total.toFixed(2)); } // Add dynamic quote rows let rowCount = 1; $("#addRow").click(function(){ rowCount++; const newRow = ` <div class="quote-row"> <input type="number" name="qty[]" id="qty_${rowCount}" placeholder="Quantity" min="1" oninput="calc(this)"> <input type="number" step="0.01" name="unitprice[]" id="unitprice_${rowCount}" placeholder="Unit Price" min="0" oninput="calc(this)"> <input type="number" step="0.01" name="laspprice[]" id="laspprice_${rowCount}" placeholder="Laser Price" min="0" oninput="calc(this)"> <input type="number" step="0.01" name="total[]" id="total_${rowCount}" placeholder="Total" readonly> <button type="button" onclick="$(this).parent().remove()">Remove</button> </div>`; $("#quoteForm").append(newRow); }); </script> </body> </html>
3. Backend Processing (Save & Delete)
Create a process.php file to handle form submissions and delete requests:
<?php // Database connection $conn = mysqli_connect('localhost', 'your_username', 'your_password', 'your_database'); if (!$conn) die("Connection failed: " . mysqli_connect_error()); // Save new quotes if(isset($_POST['saveQuote'])){ $qtyArray = $_POST['qty']; $unitPriceArray = $_POST['unitprice']; $laserPriceArray = $_POST['laspprice']; $totalArray = $_POST['total']; foreach($qtyArray as $index => $qty){ // Sanitize inputs to prevent SQL injection $qty = mysqli_real_escape_string($conn, $qty); $unitPrice = mysqli_real_escape_string($conn, $unitPriceArray[$index]); $laserPrice = mysqli_real_escape_string($conn, $laserPriceArray[$index]); $total = mysqli_real_escape_string($conn, $totalArray[$index]); $sql = "INSERT INTO film_quotes (quantity, unit_price, laser_price, total_amount) VALUES ('$qty', '$unitPrice', '$laserPrice', '$total')"; mysqli_query($conn, $sql); } header("Location: index.php"); exit(); } // Delete a quote if(isset($_GET['delete'])){ $id = mysqli_real_escape_string($conn, $_GET['delete']); $sql = "DELETE FROM film_quotes WHERE id='$id'"; mysqli_query($conn, $sql); header("Location: index.php"); exit(); } mysqli_close($conn); ?>
4. Edit Quote Page
Create an edit.php file to let users update existing quotes:
<?php $conn = mysqli_connect('localhost', 'your_username', 'your_password', 'your_database'); if (!$conn) die("Connection failed: " . mysqli_connect_error()); $quoteId = mysqli_real_escape_string($conn, $_GET['id']); $quote = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM film_quotes WHERE id='$quoteId'")); // Update quote if(isset($_POST['updateQuote'])){ $qty = mysqli_real_escape_string($conn, $_POST['qty']); $unitPrice = mysqli_real_escape_string($conn, $_POST['unitprice']); $laserPrice = mysqli_real_escape_string($conn, $_POST['laspprice']); $total = mysqli_real_escape_string($conn, $_POST['total']); $sql = "UPDATE film_quotes SET quantity='$qty', unit_price='$unitPrice', laser_price='$laserPrice', total_amount='$total' WHERE id='$quoteId'"; mysqli_query($conn, $sql); header("Location: index.php"); exit(); } ?> <!DOCTYPE html> <html> <head> <title>Edit Laser Film Quote</title> <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script> </head> <body> <h1>Edit Quote #<?php echo $quote['id']; ?></h1> <form method="POST" action="edit.php?id=<?php echo $quoteId; ?>"> <div style="margin: 10px 0;"> <label>Quantity:</label> <input type="number" name="qty" value="<?php echo $quote['quantity']; ?>" min="1" oninput="calc(this)"> </div> <div style="margin: 10px 0;"> <label>Unit Price:</label> <input type="number" step="0.01" name="unitprice" value="<?php echo $quote['unit_price']; ?>" min="0" oninput="calc(this)"> </div> <div style="margin: 10px 0;"> <label>Laser Price:</label> <input type="number" step="0.01" name="laspprice" value="<?php echo $quote['laser_price']; ?>" min="0" oninput="calc(this)"> </div> <div style="margin: 10px 0;"> <label>Total:</label> <input type="number" step="0.01" name="total" value="<?php echo $quote['total_amount']; ?>" readonly> </div> <button type="submit" name="updateQuote">Update Quote</button> <a href="index.php">Cancel</a> </form> <script type="text/javascript"> function calc(){ const qty = parseInt($("input[name='qty']").val()) || 0; const price = parseFloat($("input[name='unitprice']").val()) || 0; const laspprice = parseFloat($("input[name='laspprice']").val()) || 0; const total = (price + laspprice) * qty; $("input[name='total']").val(total.toFixed(2)); } </script> </body> </html>
Quick Notes to Improve the System
- Replace
your_username,your_password, andyour_databasewith your actual database credentials. - For better security, use prepared statements instead of
mysqli_real_escape_stringto prevent SQL injection. - Add frontend validation (e.g., ensure inputs aren’t empty, quantities are positive integers).
- You can style the pages further to match your existing app’s design.
内容的提问来源于stack exchange,提问作者ashvin solanki

