简体   繁体   中英

Trying to update mysql database form in php

My database manages to retrieve values when I navigate from the previous page. When I click the 'Update Product' button, the line

Update product

appears. What I want is when I click the 'Update Product' button, and have modified a record beforehand, I would hope to update the database with the values as well and a confirmation message is displayed to confirm this.

Code:

 <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> <input id="Producer" name="productDescription" type="text" value="<?php echo $productDetails["Producer"]; ?>"> </div> <div> <label for="updateFormProductPrice">Year Published</label> <input id="updateFormProductPrice" name="YearPublished" type="text" value="<?php echo $productDetails["YearPublished"]; ?>"> </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> 

PHP:

 if (((!empty($_GET["mode"])) && (!empty($_GET["ID"]))) && ($_GET["mode"] == "update")) { echo "<h1>Update product</h1>"; if (isset($_POST["updateSubmit"])) { if ((!empty($_POST["ID"])) && (!empty($_POST["FilmName"])) && (!empty($_POST["Producer"])) && (!empty($_POST["YearPublished"])) && (!empty($_POST["Stock"])) && (!empty($_POST["Price"]))) { $query = "UPDATE ProductManagement " . "SET FilmName = '" . $_POST["FilmName"] . "', " . "Producer = '" . $_POST["Producer"] . "', " . "YearPublished = '" . $_POST["YearPublished"] . "', " . "Stock = " . $_POST["Stock"] . ", " . "Price = '" . $_POST["Price"] . "' " . "WHERE ID=" . $_GET['ID'] . ";"; $result = mysqli_query($connection, $query); if ($result == false) { echo "<p>Updating failed.</p>"; } else{ echo "<p>Updated</p>"; } } } } 

So I need the database to update what new value I have entered and it once the 'Update product' Button is pressed, the original value appears and the value is not updated on the database. Why is this? I don't get any error messages. Thanks

The error is that you dont POST the ID but you GET the ID value. input boxes with the readonly attribute don't post values.

change:

if ((!empty($_POST["ID"])) && (!empty($_POST["FilmName"])) 

to:

if ((!empty($_GET["ID"])) && (!empty($_POST["FilmName"])) 

Edit: Total changes to make to make this work:

HTML:

<form id="updateForm" name="updateForm" action="<?php echo "?mode=update&ID=" . $productDetails["ID"]; ?>" method="post">                                
                    <div>
                        <label for="updateFormProductID">ID</label>
                        <input id="updateFormProductID" 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="updateFormProductProducer">Producer</label>
                        <input id="updateFormProductProducer" name="Producer" type="text" 
                                value="<?php echo $productDetails["Producer"]; ?>">                
                    </div>
                    <div>
                        <label for="updateFormProductYearPublished">Year Published</label>
                        <input id="updateFormProductYearPublished" name="YearPublished" type="text" 
                               value="<?php echo $productDetails["YearPublished"]; ?>">
                    </div>
                    <div>
                        <label for="updateFormProductStock">Stock:</label>
                        <input id="updateFormProductStock" name="Stock" type="text" 
                               value="<?php echo $productDetails["Stock"]; ?>">
                    </div>
                    <div>
                        <label for="updateFormProductPrice">Price:(&#163)</label>
                        <input id="updateFormProductPrice" name="Price" type="text" 
                               value="<?php echo $productDetails["Price"]; ?>">
                    </div>
                    <div>
                        <input id="updateSubmit" name="updateSubmit" value="Update product" type="submit">
                    </div>
                </form>

PHP:

if (((!empty($_GET["mode"])) && (!empty($_GET["ID"]))) && ($_GET["mode"] == "update")) {
               echo "<h1>Update product</h1>";
                if (isset($_POST["updateSubmit"])) {
                    if ((!empty($_GET["ID"])) && (!empty($_POST["FilmName"])) 
                            && (!empty($_POST["Producer"])) && (!empty($_POST["YearPublished"])) 
                            && (!empty($_POST["Stock"])) && (!empty($_POST["Price"]))) {
                        $query = "UPDATE ProductManagement "
                                . "SET FilmName = '" . $_POST["FilmName"] . "', "
                                . "Producer = '" . $_POST["Producer"] . "', "
                                . "YearPublished = '" . $_POST["YearPublished"] . "', "
                                . "Stock = " . $_POST["Stock"] . ", "
                                . "Price = '" . $_POST["Price"] . "' "
                                . "WHERE ID=" . $_GET['ID'] . ";";
                        $result = mysqli_query($connection, $query);

                        if ($result == false) {
                            echo "<p>Updating failed.</p>";
                        } else{
                            echo "<p>Updated</p>";
                                }
                                                                                        }
                                                    }
        }

Try setting the name and id of your input fields to the same respective values. I see you call id from one and name from another input field in your php and it might be causing the function to fail.

Like so for example:

<label for="ID">ID</label>
<input id="ID" name="ID" type="text" readonly value="<?php echo $productDetails["ID"]; ?>">

you should be fine using $_POST[] , since the method of your form is POST . (If you change it to GET it will put all the values in the url)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM