2015-11-25 50 views
0

這是我的第一篇文章!如何從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代碼更新記錄(編輯.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>"; 
         } 
                       } 
              } 
} 

我很抱歉,這裏有很多代碼。基本上,當我單擊主頁上的表中的編輯時,我希望它會加載所選行的數據,以便我可以更新它。

當前,當我點擊'編輯'鏈接時,它會加載編輯頁面,並且它有空白字段並且顯示「獲取產品詳細信息失敗」。如果它可以檢索所選相應行的數據,那將是非常好的。有人可以幫忙嗎?謝謝!

+0

'ID'!='id' - 因此檢查PHP和SQL端的錯誤。你沒有那樣做。 –

+1

請查閱以下鏈接http://php.net/manual/en/mysqli.error.php和http://php.net/manual/en/function.error-reporting.php 並將其應用於您的代碼。 –

+1

還使用準備語句來避免SQL注入。 –

回答

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與預先準備的語句組合起來以避免這種情況。

相關問題