Updated 2022-05-22 0
Viewed 12 times
0

I am new to PHP and I need some help. I am having an issue where, I can update the data successfully BUT the changes will not reflect within phpMyAdmin.

I am trying to get it to where a user will search for an item, and if they want to update that item (add information/change info, etc) they can click on a link. I have details.php set up like so:

    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Inventory</title>

    <style>
        table, th, td {
            border:1px solid black;
        }

        h1{
            text-align: center;
        }

        h2{
            text-align: center;
        }
    </style>

</head>
<body>

    <h1 class="ssinv">Sulley Sells Update Items</h>

    <table class="invtable" style="width:100%">
    <tr>
        <th>Product ID</th>
        <th>Product</th>
        <th>Description</th>
        <th>Location</th>
    </tr>
    <?php

$servername = "localhost";
$username = "u***_inventory";
$password = "***";
$db = "u***_products";

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

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

$id = $_GET['id'];
$id = mysqli_real_escape_string($conn,$id);
$sql = 'SELECT * FROM inventory WHERE product_id = "' .$id. '" ';
$result = mysqli_query($conn,$sql);

while($row = mysqli_fetch_assoc($result)) {
   $id = $row['product_id'];
   $product = $row['product']; 
   $description = $row['description'];
   $location = $row['location'];
   
   echo "<tr><td>$id</td>";
   echo "<td>$product</td>";
   echo "<td>$description</td>";
   echo "<td>$location</td></tr>";
}

?>
</table>

    <form action="/SulleySells/scripts/updateitem.php" 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="list_date">Listed Date:</label>
        <input type="date" id="updateltext" name="list_date" />
        <br>
        <label for="list_price">Listed Price:</label>
        <input type="text" id="updateltext"  name="list_price"/>
        <br>
        <button>Update Listing Info</button>
        <hr>
    </div>
    
     <div id="updates" style="display: none">
         <h3>Update Sale Details:</h3>
         
        <label for="sold_date">Sold Date:</label>
        <input type="date" id="updatestext" name="sold_date"/>
        <br>
        <label for="sold_price">Sold Price:</label>
        <input type="text" id="updatestext" name="sold_price"/>
        <br>
        <label for="ship_date">Ship Date:</label>
        <input type="date" id="updatestext" name="ship_date" />
        <br>
        <label for="ship_cost">Ship Cost:</label>
        <input type="text" id="updatestext" name="ship_cost"/>
        <br>
        <a>Update Sold Info</button>
        <hr>
    </div>

The issue I am having is with my updateitem.php. I can update successfully but I am having an issue and I do not know how to select the ID from the "Update" link that is produced in details.php. Updateitem.php is shown below:


$id = isset($_GET['id']) ? $_GET['id'] : 'product_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 = "u***_inventory";
$password = "***";
$db = "u***_products";

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

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

$sqlquery = "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($sqlquery) === TRUE){
    echo "Record Saved.";
} else {
    echo "Error!";
}

$conn->close();

I need to be able to assign an ID to the update link so that I can get my updateitem.php file to make the changes in the database.

🔴 No definitive solution yet