Również się bawiłem w to co ty teraz i wykombinowałem coś takiego:
connect.php
abstract class Connect{
private $_host = "localhost";
private $_dbname = "obiektowosc";
private $_root = "root";
private $_password = "";
protected $_connect;
public function __construct() {
try{
$this -> _connect = new PDO("mysql:host=".$this -> _host.";dbname=".$this -> _dbname, $this -> _root, $this -> _password);
$this -> _connect -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this -> _connect -> setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
}
catch(PDOException $e){
echo "błąd połączenia z bazą: ". $e -> getMessage();
}
}
}
user.php
require_once "connect.php";
class User extends Connect {
public function __construct(){
parent::__construct();
}
function AddUser($name, $surname, $age) {
$name = trim($name);
$surname = trim($surname);
$age = trim($age);
try {
$addUser = "
INSERT INTO informacje (id, imie, nazwisko, wiek)
VALUES (null, :name, :surname, :age)";
$bind = $this -> _connect -> prepare($addUser);
$bind -> bindValue(':name', $name, PDO::PARAM_STR);
$bind -> bindValue(':surname', $surname, PDO::PARAM_STR);
$bind -> bindValue(':age', $age, PDO::PARAM_INT);
$bind -> execute();
$bind = null;
header("Location: ".$_SERVER["REQUEST_URI"]);
}
catch(PDOException $e) {
echo "błąd dodania do bazy: ".$e -> getMessage();
}
}
function ShowUsers() {
try {
$showUsers = "SELECT * FROM informacje";
$view_users = $this -> _connect -> query($showUsers) -> fetchAll();
var_dump( $view_users );
require_once "plik.php";
$this -> _connect = null;
}
catch(PDOException $e) {
echo "nie można wyświetlić informacji: ". $e -> getMessage();
}
}
function DeleteUser($id) {
try {
$deleteUser = "DELETE FROM informacje WHERE id = :id";
$bind = $this -> _connect -> prepare($deleteUser);
$bind -> bindValue(':id', $id, PDO::PARAM_STR);
$bind -> execute();
$bind = null;
}
catch(PDOException $e) {
echo "błąd usuwania: ".$e -> getMessage();
}
}
/*
function EditUser(){
}
*/
}
if(!empty($_POST["imie"]) && !empty($_POST["nazwisko"]) && !empty($_POST["wiek"])) {
$add_user = new User;
$add_user -> AddUser($_POST["imie"], $_POST["nazwisko"], $_POST["wiek"]);
$add_user = null;
}
if(isset($_GET["id_delete"]) && !empty($_GET["id_delete"])) {
$url = $_SERVER["SCRIPT_NAME"];
$detele_user = new User;
$detele_user -> DeleteUser($_GET["id_delete"]);
$detele_user = null;
header("Location: ".$url);
}
$user = new User;
$user -> ShowUsers();
$user = null;
i plik.php (w rzeczywstości mam inaczej ale w locie pozmieniałem nazwy)
<!doctype html>
<html>
<head>
<title>Obiektowość</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form method="post">
<label>Imię: <input type="text" name="imie" /></label>
<label>Nazwisko: <input type="text" name="nazwisko" /></label>
<label>Wiek: <input type="text" name="wiek" /></label>
<input type="submit" value="dodaj" />
</form>
<table>
<?php foreach ($view_users as $row): ?>
<tr>
<td><?= $row -> id; ?></td>
<td><?= $row -> imie; ?></td>
<td><?= $row -> nazwisko; ?></td>
<td><?= $row -> wiek; ?></td>
<td><a href="?id_delete=<?= $row -> id; ?>">usuń</a></td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>
Zapewne też jest to fatalnie napisane, ale i tak jestem z tego zadowolony oraz mam nadzieję, że pomogę choć trochę :)