0

I was following a tutorial, i can insert data and display it. But when i try to update the data, it does give me the success message, but the data doesn't change.'s still the same.

Here is the code :

view.php


<?php 
    session_start(); 
    include("./includes/connect.inc.php")
?>


<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
    <title>Document</title>
</head>
<body>

    <?php if(isset($_SESSION['message'])) : ?>
        <h5 class="alert alert-success"><?= $_SESSION['message']; ?></h5>
    <?php 
        unset($_SESSION['message']);
        endif; 
    ?>
            <table class="table table-hover container mt-5">
            <thead>
                <tr>
                <th scope="col">Service/Brand</th>
                <th scope="col">Discount</th>
                <th scope="col">Expiry</th>
                <th scope="col">Code</th>
                <th scope="col">Actions</th>
                </tr>
            </thead>
            <tbody>
                
                <?php 
                $query = "SELECT * FROM codes";
                $stmt = $conn->prepare($query);
                $stmt->execute();

                $stmt->setFetchMode(PDO::FETCH_OBJ); // PDO::FETCH_ASSOC === <td>$row["service"];</td> 
                $result = $stmt->fetchAll(); 
                if($result) {

                    foreach($result as $row) {
                        ?>

                            <tr>
                                <td><?= $row->service;?></td>
                                <td><?= $row->discount;?></td>
                                <td><?= $row->expiry;?></td>
                                <td><?= $row->code;?></td>

                                <td><a href="edit-code.php?id=<?= $row->id; ?>" class="btn btn-success">Edit</a></td>
                            </tr>

                        <?php
                    }

                } else {
                    ?>

                        <tr>
                            <td>No Record Found</td>
                        </tr>

                    <?php
                }
                ?>
            </tbody>
            </table>
            <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
</body>
</html>

update.inc.php

<?php
session_start();
include("connect.inc.php");

if(isset($_POST["update-code-btn"])) {

    $code_id = $_POST["code_id"];
    $service = $_POST["service"];
    $discount = $_POST["discount"];
    $expiry = $_POST["expiry"];
    $code = $_POST["code"];

    try {

        $query = "UPDATE codes SET service=:service, discount=:discount, expiry=:expiry, code=:code WHERE id=:id LIMIT 1";
        $stmt = $conn->prepare($query);

        $data = [
            ":service" => $service,
            ":discount" => $discount,
            ":expiry" => $expiry,
            ":code" => $code,
            ":id" => $id,
        ];

        $query_execute = $stmt->execute($data);

        if($query_execute){
            $_SESSION["message"] = "Updated Successfully";
            header("Location: ../view.php");
            exit(0);
        } else {
            $_SESSION["message"] = "Data Update Failed";
            header("Location: ../view.php");
            exit(0);
        }

    } catch(PDOException $e) {
        echo $e->getMessage();
    }

}

edit-code.php

<?php
    include("./includes/connect.inc.php")
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
    <title>Edit Code</title>
</head>
<body>

    <h1 class="m-5">Edit code</h1>
    <a href="view.php" class="btn btn-primary mx-5 mb-5">Back</a>

    <?php
        if(isset($_GET["id"])) {
            $code_id = $_GET["id"];

            $query = "SELECT * FROM codes WHERE id=:code_id LIMIT 1";
            $stmt = $conn->prepare($query);
            $data = [":code_id" => $code_id];
            $stmt->execute($data);

            $result = $stmt->fetch(PDO::FETCH_OBJ); // PDO::FETCH_ASSOC
        }
    ?>
    
    <form class="container" action="./includes/update.inc.php" method="POST">
        <input type="hidden" name="id" value="<?= $result->id; ?>" class="form-control" id="id">
    <div class="mb-3">
        <label for="service" class="form-label">Service/Brand</label>
        <input type="text" name="service" value="<?= $result->service; ?>" class="form-control" id="service">
        <!-- aria-describedby="service" -->
    </div>
    <div class="mb-3">
        <label for="discount" class="form-label">Discount</label>
        <input type="text" name="discount" value="<?= $result->discount; ?>" class="form-control" id="discount">
        <!-- aria-describedby="discount" -->
    </div>
    <div class="mb-3">
        <label for="expiry" class="form-label">Expiry</label>
        <input type="text" name="expiry" value="<?= $result->expiry; ?>" class="form-control" id="expiry">
        <!-- aria-describedby="expiry" -->
    </div>
    <div class="mb-3">
        <label for="code" class="form-label">Code</label>
        <input type="text" name="code" value="<?= $result->code; ?>" class="form-control" id="code">
        <!-- aria-describedby="code" -->
    </div>

    <button type="submit" name="update-code-btn" class="btn btn-success mb-5">Save</button>
    </form>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
</body>
</html>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • It is not us who need to ask questions. It must be you. You must ask *your code* to pinpoint the problem. This is called debugging. See a short guide: https://phpdelusions.net/basic_principles_of_web_programming#debugging – Your Common Sense Jan 02 '23 at 17:02
  • By the look of it, however, it should work. Consider shortening your example to a single PHP script that updates the database with random values and see if it works – Your Common Sense Jan 02 '23 at 17:05
  • In your `update.php`, I don't see where you get the value for `$id`. You don't do anything with the form field called `id`, but you do try to do something with a form field called `code-id` which, as far as I can see, does not exist. – droopsnoot Jan 02 '23 at 18:16

0 Answers0