09.10
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
$db = mysqli_connect('localhost', 'root', '', '3p_1_pacjenci');
|
||||
|
||||
$f = fopen('dane.txt', 'r');
|
||||
|
||||
$query = "INSERT INTO tabela_1 (identyfikator, imie, nazwisko, email) VALUES (?, ?, ?, ?)";
|
||||
$stmt = mysqli_prepare($db, $query);
|
||||
|
||||
$truncate = "TRUNCATE TABLE tabela_1";
|
||||
|
||||
mysqli_query($db, $truncate);
|
||||
|
||||
$id = $imie = $nazwisko = $email = null;
|
||||
mysqli_stmt_bind_param($stmt, 'isss', $id, $imie, $nazwisko, $email);
|
||||
|
||||
mysqli_begin_transaction($db);
|
||||
|
||||
|
||||
$line = 0;
|
||||
while (($row = fgetcsv($f, 0, ';')) !== false) {
|
||||
$line++;
|
||||
|
||||
[$id, $imie, $nazwisko, $email] = array_map('trim', array_slice($row, 0, 4));
|
||||
|
||||
mysqli_stmt_execute($stmt);
|
||||
}
|
||||
|
||||
$selectSql = "SELECT identyfikator, imie, nazwisko, email FROM tabela_1 ORDER BY identyfikator";
|
||||
$res = mysqli_query($db, $selectSql);
|
||||
|
||||
$html = '<table border="1" cellspacing="0">';
|
||||
$html .= '<thead><tr><th>Identyfikator</th><th>Imię</th><th>Nazwisko</th><th>Email</th></tr></thead><tbody>';
|
||||
|
||||
if ($res && mysqli_num_rows($res) > 0) {
|
||||
while ($row = mysqli_fetch_assoc($res)) {
|
||||
$id = htmlspecialchars($row['identyfikator'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
||||
$im = htmlspecialchars($row['imie'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
||||
$na = htmlspecialchars($row['nazwisko'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
||||
$em = htmlspecialchars($row['email'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
||||
|
||||
$html .= '<tr>';
|
||||
$html .= '<td>' . $id . '</td>';
|
||||
$html .= '<td>' . $im . '</td>';
|
||||
$html .= '<td>' . $na . '</td>';
|
||||
$html .= '<td>' . $em . '</td>';
|
||||
$html .= '</tr>';
|
||||
}
|
||||
} else {
|
||||
$html .= '<tr><td colspan="4">Brak danych</td></tr>';
|
||||
}
|
||||
|
||||
$html .= '</tbody></table>';
|
||||
|
||||
|
||||
mysqli_commit($db);
|
||||
fclose($f);
|
||||
mysqli_close($db);
|
||||
|
||||
echo <<<HTML
|
||||
|
||||
<!doctype html>
|
||||
<html lang="pl">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport"
|
||||
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<title>T61b - pacjenci php z bazą danych i plikiem tekstowym</title>
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>Zadanie T61b</h1>
|
||||
<h2>Autor: Jakub Grzegorczyk</h2>
|
||||
</header>
|
||||
<div class="box">
|
||||
<p>
|
||||
1. Utwórz bazę danych o nazwie 3p_1_pacjenci. <br><br>
|
||||
2. W bazie danych utwórz tabelę tabela_1 zawierającą kolumny: <br><br>
|
||||
<ul>
|
||||
<li>identyfikator,</li>
|
||||
<li>imię,</li>
|
||||
<li>nazwisko,</li>
|
||||
<li>email.</li>
|
||||
</ul>
|
||||
3. Utwórz plik tekstowy o nazwie dane.txt zawierający dane 3 pacjentów. <br><br>
|
||||
4. Napisz skrypt php, który czyta dane z pliku i zapisuje je do tabeli tabela_1 i wyświetla je na stronie zadanie.php w postaci tabeli.
|
||||
</p>
|
||||
</div>
|
||||
<div class="box">
|
||||
{$html}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
HTML;
|
||||
Reference in New Issue
Block a user