如何编辑MySQL数据库中的记录


how to edit a record from MySQL database

这是我的第一篇文章!

首先,我有我的代码,它在index.php上输出一个表。最后,我有一个编辑链接,它把我带到edit.php页面:

if ($result->num_rows > 0) {
    echo "<p><table><tr><th>ID</th><th>Film Name</th><th>Producer</th><th>Year Published</th><th>Stock</th><th>Price</th><th>Function</th></tr>";
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row["ID"]."</td><td>".$row["FilmName"]."</td><td>".$row["Producer"]."</td><td>".$row['YearPublished']."</td><td>".$row['Stock']."</td><td>".$row['Price']."</td><td>"."<a href='"edit.php'">Edit</a> / Delete"."</td></tr></p>";
    }
    echo "</table>";

edit.php(首先是表单):

$query = "SELECT * FROM ProductManagement WHERE ID=" . $_GET["ID"] . ";"; // Place required query in a variable
                $result = mysqli_query($connection, $query); // Execute query
                if ($result == false) { // If query failed
                    echo "<p>Getting product details failed.</p>";
                } else { // Query was successful   
                    $productDetails = mysqli_fetch_array($result, MYSQLI_ASSOC); // Get results (only 1 row 
                    // is required, and only 1 is returned due to using a primary key (id in this case) to 
                    // get the results)
                    if (empty($productDetails)) { // If getting product details failed
                        echo "<p>No product details found.</p>"; // Display error message
                    }
                }
                ?>
<form id="updateForm" name="updateForm" action="<?php echo "?mode=update&ID=" . $productDetails["ID"]; ?>" method="post">                                
                    <div>
                        <label for="updateFormProductCostPrice">ID</label>
                        <input id="updateFormProductCostPrice" name="ID" type="text" readonly 
                               value="<?php echo $productDetails["ID"]; ?>">
                    </div>
                    <div>
                        <label for="updateFormProductName">Film Name</label>
                        <input id="updateFormProductName" name="FilmName" type="text" value="<?php echo $productDetails["FilmName"]; ?>">
                    </div>
                    <div>
                        <label for="updateFormProductDescription">Producer</label>
                        <textarea rows="4" cols="50" id="Producer" 
                                  name="productDescription"><?php echo $productDetails["Producer"]; ?></textarea>                
                    </div>
                    <div>
                        <label for="updateFormProductPrice">Year Produced</label>
                        <input id="updateFormProductPrice" name="YearProduced" type="text" 
                               value="<?php echo $productDetails["YearProduced"]; ?>">
                    </div>
                    <div>
                        <label for="updateFormProductStock">Stock:</label>
                        <input id="updateFormProductStock" name="Stock" type="text" 
                               value="<?php echo $productDetails["Stock"]; ?>">
                    </div>
                    <div>
                        <label for="updateFormProductEan">Price:(&#163)</label>
                        <input id="updateFormProductEan" name="Price" type="text" 
                               value="<?php echo $productDetails["Price"]; ?>">
                    </div>
                    <div>
                        <input id="updateSubmit" name="updateSubmit" value="Update product" type="submit">
                    </div>
                </form>
</body>

然后是更新记录的php代码(edit.php续):

if (((!empty($_GET["mode"])) && (!empty($_GET["id"]))) && ($_GET["mode"] == "update")) { // If update
        echo "<h1>Update product</h1>";
        if (isset($_POST["updateSubmit"])) { // If update form submitted
            // Check all parts of the form have a value
            if ((!empty($_POST["ID"])) && (!empty($_POST["FilmName"])) 
                    && (!empty($_POST["Producer"])) && (!empty($_POST["YearProduced"])) 
                    && (!empty($_POST["Stock"])) && (!empty($_POST["Price"]))) {
                // Create and run update query to update product details
                $query = "UPDATE products "
                        . "SET FilmName = '" . $_POST["FilmName"] . "', "
                        . "Producer = '" . $_POST["Producer"] . "', "
                        . "YearProduced = '" . $_POST["YearProduced"] . "', "
                        . "Stock = " . $_POST["Stock"] . ", "
                        . "Price = '" . $_POST["Price"] . "' "
                        . "WHERE id=" . $_GET['ID'] . ";";
                $result = mysqli_query($connection, $query);
                if ($result == false) { // If query failed - Updating product details failed (the update statement failed)
                    // Show error message
                    echo "<p>Updating failed.</p>";
                } else{ // Updating product details was sucessful (the update statement worked)
                    // Show success message
                    echo "<p>Updated</p>";
                        }
                                                                                }
                                            }
}

我很抱歉这里有很多代码。基本上,当我点击主页上表格中的编辑时,我希望它能加载所选行的数据,这样我就可以更新它

目前,当我点击"编辑"链接时,它会加载编辑页面,其中有空白字段,并显示"获取产品详细信息失败"。如果它能够检索所选相应行的数据,那就太好了。有人能帮忙吗?谢谢

edit.php文件中,$_GET["ID"]为空,因为链接中没有ID值,所以查询不返回结果。此外,在上一个文件中,您有$_GET["id"],它与您使用的值($_GET["ID"])不同。

试试这个:

echo "
    <tr>
        <td>".$row["ID"]."</td>
        <td>".$row["FilmName"]."</td>
        <td>".$row["Producer"]."</td>
        <td>".$row['YearPublished']."</td>
        <td>".$row['Stock']."</td>
        <td>".$row['Price']."</td>
        <td><a href='"edit.php?ID=".$row["ID"]."'">Edit</a>
        <td><a href='"delete.php?ID=".$row["ID"]."'">Delete</a>
    </tr>";

此外,您还存在SQL注入漏洞。您可以将mysqli与准备好的语句结合起来以避免这种情况。