Answer the question
In order to leave comments, you need to log in
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.
Folder content.
database.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;
}
}
?>
<!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>
Answer the question
In order to leave comments, you need to log in
There are many options here are some:
1) index.html-> index.php
2) open a local copy of the site instead of http:// ......
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question