PHP如何从SQL Server获取同ID的多行数据?
Hey there! Let's break down your problem step by step, fix the multiple entries issue first, then address your questions about PHP frameworks and switching to Django.
1. Fixing the Multiple Entries Display Problem
Your core issue comes from how you're generating and parsing JSON data. Let's adjust both the PHP and JavaScript code:
First: Fix the PHP (orderDetails.php)
Right now, you're concatenating individual JSON objects together, which creates invalid JSON (e.g., {"a":1}{"b":2} instead of [{"a":1},{"b":2}]). Instead, collect all rows into an array and encode the entire array at once. Also, always use parameterized queries to prevent SQL injection—your current code is vulnerable!
<?php require 'dbConnection.php'; if ($conn) { $orderNumber = $_GET["orderNumberGET"]; // Use parameterized query to avoid SQL injection $sql222 = "SELECT * FROM _Servisni_nalog_ToDo WHERE RN_key = ?"; $params = array($orderNumber); $stmt = sqlsrv_query($conn, $sql222, $params); $resultArray = []; // Collect all rows into an array while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { $resultArray[] = $row; } // Encode the entire array as valid JSON echo json_encode($resultArray); } else { // Return error as JSON for consistent handling echo json_encode(["error" => "No connection to database. Please check your connection."]); } ?>
Second: Update the JavaScript (script.js)
Now that your PHP returns an array of items, you need to loop through this array and dynamically render all entries—hardcoding #item1 to #item6 won't work for variable numbers of entries.
First, add a container in your HTML to hold all the items:
<div id="detailsContainer"></div>
Then adjust the JS to populate this container dynamically:
function showDetails(button) { var orderNumber = button.id; $.ajax({ url: "orderDetails.php", method: "GET", data: { "orderNumberGET": orderNumber }, success: function(response) { var items = JSON.parse(response); // Clear existing content first var detailsContainer = $("#detailsContainer"); detailsContainer.empty(); // Set the PC ID (since all items share the same ID) $("#acKey").text(items[0].RN_key); // Loop through each item and create HTML elements items.forEach(function(item) { var itemRow = $("<div class='item-entry'></div>"); itemRow.append(`<p>Component: ${item.anNo}</p>`); itemRow.append(`<p>Identifier: ${item.acIdent}</p>`); itemRow.append(`<p>Quantity: ${item.anQty}</p>`); itemRow.append("<hr>"); detailsContainer.append(itemRow); }); }, error: function(xhr) { var error = JSON.parse(xhr.responseText); alert(error.error || "Failed to load details."); } }); }
2. Would a PHP Framework Help?
Absolutely, but it depends on your project size:
- Small projects: You can make this work with native PHP, but frameworks like Laravel or Symfony will reduce repetitive code (e.g., built-in database query builders that handle parameterization automatically, ORMs that map database rows to objects).
- Large/long-term projects: Framewares enforce structure, improve maintainability, and come with security features out of the box (like CSRF protection, input validation) that you'd have to build manually in native PHP.
If your current code is just this CRUD flow, native PHP is fine—but if you plan to expand the app, a framework will save you time in the long run.
3. Is Switching to Python Django Reasonable?
It's reasonable if:
- You're comfortable with Python, or want to learn it. Django has a steep learning curve but offers a lot out of the box (auto-generated admin panels, powerful ORM, built-in security).
- You're starting fresh or don't mind rewriting existing code. Switching an existing PHP project to Django would mean reworking all backend logic, which is a big time investment.
- You prefer Python's ecosystem (e.g., data processing libraries, which might be useful if you ever need to analyze your inventory data).
If your current project is already functional and you just need to fix this display issue, sticking with PHP (either native or adding a lightweight framework like Slim) is more efficient. But if you're looking to rebuild or start a new project, Django is a solid choice.
内容的提问来源于stack exchange,提问作者Dome

