Possible revision title: "PHP SQL database update query issue: Index is not defined and data is not updated?"
P粉637866931
P粉637866931 2024-04-02 18:46:20
0
1
530

I am having trouble updating the database. I think the error is in the update query itself, but I'm new to SQL and PHP so I'm not 100% sure. Any help would be greatly appreciated.

I got an undefined index error, I fixed it but the problem still exists:

$id = $_GET["id"];
$listdate = isset($_POST['list_date']) ? $_POST['list_date'] : '';
$listprice = isset($_POST['list_price']) ? $_POST['list_price'] : '';
$solddate = isset($_POST['sold_date']) ? $_POST['sold_date'] : '';
$soldprice = isset($_POST['sold_price']) ? $_POST['sold_price'] : '';
$shipdate = isset($_POST['ship_date']) ? $_POST['ship_date'] : '';
$shipcost = isset($_POST['ship_cost']) ? $_POST['ship_cost'] : '';

After changing to the above code (which doesn't work), I still have issues updating the database. Here is the complete current code:

<?

$id = $_GET["id"];
$listdate = $_POST["list_date"];
$listprice = $_POST["list_price"];
$solddate = $_POST["sold_date"];
$soldprice = $_POST["sold_price"];
$shipdate = $_POST["ship_date"];
$shipcost = $_POST["ship_cost"];

$servername = "localhost";
$username = "inventory";
$password = "*****";
$db = "products";

$conn = new mysqli($servername, $username, $password, $db);

if ($conn->connect_error){
    die("Connection failed: ". $conn->connect_error);
}

   
$sql = "UPDATE inventory SET list_date = '$listdate', list_price = '$listprice', sold_date = '$solddate', sold_price = '$soldprice', ship_date = '$shipdate', ship_cost= '$shipcost' WHERE product_id = ' .$id. '";

if($conn->query($sql) === TRUE){
    echo "Record Saved.";
    } else {
        echo "Error!";
    }

$conn->close();

?>

Even if I manually change the ID to the one assigned by the database, the update still doesn't work and nothing changes in the database. I know the form doesn't look pretty, but I set it up as a test. Here's how I set up the form to capture the changes:

<form action="/SulleySells/scripts/updateitem.php?id=<?php echo $id;?>" method="post">

    <script type="text/javascript">
        function ShowHideDiv(listed) {
            var updatel = document.getElementById("updatel");
            updatel.style.display = listed.checked ? "block" : "none";
        }
        function ShowHideDiv2(sold) {
            var updates = document.getElementById("updates");
            updates.style.display = sold.checked ? "block" : "none";
        }
    </script>
    
    <label for="listed">
    <input type="checkbox" id="listed" onclick="ShowHideDiv(this)" />
    Listed?
    </label>
    
    <label for="sold">
    <input type="checkbox" id="sold" onclick="ShowHideDiv2(this)" />
    Sold?
    </label>
    
    <hr>
    
    <div id="updatel" style="display: none">
        <h3>Update Listing Details:</h3>
        <label for="listdate">Listed Date:</label>
        <input type="date" id="updateltext" name="listdate" value=""/>
        <br>
        <label for="listprice">Listed Price:</label>
        <input type="text" id="updateltext"  name="listprice" value=""/>
        <br>
        <button>Update</button>
        <hr>
    </div>
    
     <div id="updates" style="display: none">
         <h3>Update Sale Details:</h3>
         
        <label for="solddate">Sold Date:</label>
        <input type="date" id="updatestext" name="solddate" value=""/>
        <br>
        <label for="soldprice">Sold Price:</label>
        <input type="text" id="updatestext" name="soldprice" value=""/>
        <br>
        <label for="shipdate">Ship Date:</label>
        <input type="date" id="updatestext" name="shipdate" value=""/>
        <br>
        <label for="shipcost">Ship Cost:</label>
        <input type="text" id="updatestext" name="shipcost"value=""/>
        <br>
        <button>Update Sold Info</button>
        <hr>
    </div>

    </form>

P粉637866931
P粉637866931

reply all(1)
P粉415632319

First you must change:

to:

Match what is in your php code and apply it to the rest of the html input

Then change:

WHERE product_id = ' .$id. '

to:

WHERE product_id = '$id'

notify Don't forget to use id="" in the input and for="" in the formatting tag to avoid giving different results

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!