create database feherbt
character set utf8
collate utf8_hungarian_ci;
grant all privileges
on feherbt.*
to feherbt@localhost
identified by 'titok';
use feherbt;
create table employees (
id int not null primary key auto_increment,
name varchar(30),
city varchar(30),
salary double
);
===== Példa =====
==== Struktúra ====
projekt01/
|-src/
| |-includes/
| | |-employee.php
| | |-getdata.php
| | |-insertdata.php
| | `-mariadb.php
| |-templates/
| | |-footer.tpl
| | |-header.tpl
| | |-index.tpl
| | `-li.tpl
| `-index.php
`-test/
`-insertEmployeeTest.php
==== employee.php ====
==== mariadb.php ====
connect_error) {
error_log('Hiba! A kapcsolódás sikertelen!');
die;
}
$conn->set_charset("utf-8");
return $conn;
}
}
==== getdata.php ====
query($sql)) {
while(($record = $res->fetch_assoc()) !== null) {
$employee = new Employee();
$employee->name = $record['name'];
$employee->city = $record['city'];
$employee->salary = $record['salary'];
array_push($employees, $employee);
}
}else {
error_log('Hiba! A lekérdezés sikertelen!');
}
$conn->close();
return $employees;
}
}
==== index.php ====
name, $li);
}
$page = str_replace('{{ employeeList }}', $employeeList, $page);
echo $page;
echo file_get_contents('templates/footer.tpl');
==== header.tpl ====
Minta
Minta
==== footer.tpl ====
==== index.tpl ====
{{ employeeList }}
==== li.tpl ====
{{ employee }}
===== Beszúrás =====
include_once 'includes/employee.php';
include_once 'includes/mariadb.php';
class Insertdata {
public static function insertEmployee($employee) {
$conn = Mariadb::connectDb();
$sql = "insert into employees " .
"(name, city, salary) values " .
"(?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssd",
$employee->name,
$employee->city,
$employee->salary
);
$insertOk = false;
if($stmt->execute()) {
$insertOk = true;
error_log('Beszúrt, azonosító: ' . $conn->insert_id . "\n");
}else {
error_log("Hiba! A beszúrás sikertelen!\n");
}
$stmt->close();
$conn->close();
return $insertOk;
}
}
==== Teszt ====
name = 'Parány Mihály';
$employee->city = 'Szeged';
$employee->salary = 2850000;
if(Insertdata::insertEmployee($employee)) {
echo 'Ok Sikeres beszúrás';
}else{
echo 'Hiba! Sikertelen beszúrás!';
}