如何从MySQL数据库编辑记录

时间:2015-11-25 23:24:43

标签: php html mysql

这是我的第一篇文章!

首先,我有我的代码,它在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>";
                        }
                                                                                }
                                            }
}

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

目前,当我点击“修改”链接时,它会加载编辑页面并显示空白字段并显示“获取产品详细信息失败”。如果它可以检索所选择的相应行的数据,那将是很好的。有人可以帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:1)

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与准备好的语句结合起来以避免这种情况。