R
R
Rishat Sultanov2016-11-21 17:49:06
PHP
Rishat Sultanov, 2016-11-21 17:49:06

How to implement CRUD correctly?

Good afternoon, ladies and gentlemen.
I set up a server in Denver and started learning SQL and PHP.
Started with CRUD. I found a lesson, but at the end I have such problems.
Can you help me find the error.
PHPmyAdmin.
5B5ad322.jpg
Folder content.
10265fF9.jpgdatabase.php

<?php
class Database
{
    private static $dbName = 'customers' ;
    private static $dbHost = 'localhost' ;
    private static $dbUsername = 'localhost';
    private static $dbUserPassword = 'localhost';
     
    private static $cont  = null;
     
    public function __construct() {
        die('Init function is not allowed');
    }
     
    public static function connect()
    {
       // One connection through whole application
       if ( null == self::$cont )
       {     
        try
        {
          self::$cont =  new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword); 
        }
        catch(PDOException $e)
        {
          die($e->getMessage()); 
        }
       }
       return self::$cont;
    }
     
    public static function disconnect()
    {
        self::$cont = null;
    }
}
?>

index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link   href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/bootstrap.min.js"></script>
</head>
 
<body>
    <div class="container">
            <div class="row">
                <h3>TEST CRUD</h3>
            </div>
            <div class="row">
                <table class="table table-striped table-bordered">
                  <thead>
                    <tr>
                      <th>Name</th>
                      <th>Email Address</th>
                      <th>Mobile Number</th>
                    </tr>
                  </thead>
                  <tbody>
                  <?php
                   include 'database.php';
                   $pdo = Database::connect();
                   $sql = 'SELECT * FROM customers ORDER BY id DESC';
                   foreach ($pdo->query($sql) as $row) {
                            echo '<tr>';
                            echo '<td>'. $row['name'] . '</td>';
                            echo '<td>'. $row['email'] . '</td>';
                            echo '<td>'. $row['mobile'] . '</td>';
                            echo '</tr>';
                   }
                   Database::disconnect();
                  ?>
                  </tbody>
            </table>
        </div>
    </div> <!-- /container -->
  </body>
</html>

+ bootstrap
At the output I have:
e20547a1.jpg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daemon23RUS, 2016-11-21
@rishatss

There are many options here are some:
1) index.html-> index.php
2) open a local copy of the site instead of http:// ......

A
Alexander, 2016-11-21
@xpert13

You cannot use PHP code in the HTML file, change the file extension to PHP.
PS Look for other tutorials, those that teach you to mix HTML and PHP code are bad.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question