0

I'm trying to code a database manager in PHP using the CRUD method. I'm almost done, but I cannot figure out why my table data is not being shown. I keep seeing a lot of undefined index errors. I am not sure how to fix this, nor do I know where these placeholders are being defined.

// Show PHP errors
ini_set('display_errors',1);
ini_set('display_startup_erros',1);
error_reporting(E_ALL);

require_once 'classes/member.php';

$objMember = new Member();

// GET
if(isset($_GET['delete_id'])){
  $id = $_GET['delete_id'];
  try{
    if($id != null){
      if($objMember->delete($id)){
        $objMember->redirect('index.php?deleted');
      }
    }else{
      var_dump($id);
    }
  }catch(PDOException $e){
    echo $e->getMessage();
  }
}


?>
<!doctype html>
<html lang="en">
    <head>
        <!-- Head metas, css, and title -->
        <?php require_once 'includes/head.php'; ?>
    </head>
    <body>
        <!-- Header banner -->
        <?php require_once 'includes/header.php'; ?>
        <div class="container-fluid">
            <div class="row">
                <!-- Sidebar menu -->
                <?php require_once 'includes/sidebar.php'; ?>
                <main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
                    <h1 style="margin-top: 10px">Members List</h1>
                    <?php
                      if(isset($_GET['updated'])){
                        echo '<div class="alert alert-info alert-dismissable fade show" role="alert">
                        <strong>User!<trong> Updated with success.
                          <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true"> &times; </span>
                          </button>
                        </div>';
                      }else if(isset($_GET['deleted'])){
                        echo '<div class="alert alert-info alert-dismissable fade show" role="alert">
                        <strong>User!<trong> Deleted with success.
                          <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true"> &times; </span>
                          </button>
                        </div>';
                      }else if(isset($_GET['inserted'])){
                        echo '<div class="alert alert-info alert-dismissable fade show" role="alert">
                        <strong>User!<trong> Inserted with success.
                          <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true"> &times; </span>
                          </button>
                        </div>';
                      }else if(isset($_GET['error'])){
                        echo '<div class="alert alert-info alert-dismissable fade show" role="alert">
                        <strong>DB Error!<trong> Something went wrong with your action. Try again!
                          <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true"> &times; </span>
                          </button>
                        </div>';
                      }
                    ?>
                        <div class="table-responsive">
                            <table class="table table-striped table-sm">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Name</th>
                                        <th>Residential Address</th>
                                        <th>Mailing Address</th>
                                        <th>Precinct</th>
                                        <th>Age</th>
                                        <th>Ethnicity</th>
                                        <th>Gender</th>
                                        <th>Party</th>
                                        <th>Race</th>
                                        <th>Phone Number</th>
                                        <th></th>
                                        <th></th>
                                    </tr>
                                </thead>
                                <?php
                                $query = "SELECT * FROM members LIMIT 25";
                                $stmt = $objMember->runQuery($query);
                                $stmt->execute();
                                ?>
                                <tbody>
                                    <?php if($stmt->rowCount() > 0){
                                        while($rowMember = $stmt->fetch(PDO::FETCH_ASSOC)){
                                    ?>
                                    <tr>
                                        <td><?php print($rowMember['id']); ?></td>
                                        <td><?php print($rowMember['name']); ?></td>
                                        <td><?php print($rowMember['residential_address']); ?></td>
                                        <td><?php print($rowMember['mailing_address']); ?></td>
                                        <td><?php print($rowMember['precinct']); ?></td>
                                        <td><?php print($rowMember['age']); ?></td>
                                        <td><?php print($rowMember['ethnicity']); ?></td>
                                        <td><?php print($rowMember['gender']); ?></td>
                                        <td><?php print($rowMember['party']); ?></td>
                                        <td><?php print($rowMember['race']); ?></td>
                                        <td><?php print($rowMember['phone']); ?></td>
                                        <td><a href="form.php?edit_id=<?php print($rowMember['id']); ?>"><span>Edit</span></a></td>
                                        <td><a href="form.php?edit_id=<?php print($rowMember['id']); ?>"><span data-feather="trash"></span></a></td>
                                    </tr>
                                </tbody>
                                <?php } } ?>
                            </table>
                        </div>
                </main>
            </div>
        </div>
        <!-- Footer scripts, and functions -->
        <?php require_once 'includes/footer.php'; ?>

        <!-- Custom scripts -->
        <script>
            // JQuery confirmation
            $('.confirmation').on('click', function () {
                return confirm('Are you sure you want to delete this member?');
            });
        </script>
    </body>
</html>

I expect my data to be shown on the screen with pagination if necessary.

Jeremy Myrtle
  • 97
  • 2
  • 13
  • I'm following a tutorial I found on YouTube to complete this. I typed out his exact code (only with my data) and get these errors. He is not defining his data inside this file. https://www.youtube.com/watch?v=MFcavjs7MLA – Jeremy Myrtle Nov 05 '19 at 17:25
  • you use values without checking if they exist - that's why you get those errors - you need to use isset() or empty() to ensure data is there before use – treyBake Nov 05 '19 at 17:27
  • The tutorial doesn't show that, nor are those in his index file. I do not see where his data is being defined is his file either. – Jeremy Myrtle Nov 05 '19 at 17:30
  • forget the tutorial as it doesn't teach data validation - and that's the problem, they're not defined – treyBake Nov 06 '19 at 09:17

0 Answers0