<?php
$tech = ["PHP","Java","CPP"];
function drukuj_form(){
echo "<!DOCTYPE html>
<html>
<head>
<title>Aplikacja bazodanowa</title>
<meta charset='UTF-8'>
</head>
<body>
<form method='get' action=''>
Nazwisko:
<input type='text' name='nazw'><br>
Wiek:
<input type='text' name='wiek'><br>
<select name='panstwo'>
<option>Polska</option>
<option>Czechy</option>
<option>Niemcy</option>
<option>Wielka Brytania</option>
</select><br>
Email:
<input type ='text' name='email'><br>
Zamawiam tutoriale z języka:<br>
";
global $tech;
foreach ($tech as $value)
echo "<input type='checkbox' name='tech[]' value=$value>$value";
echo "<br>Sposób zapłaty:<br>
<input type='radio' name='zaplata' value='eurocard'>eurocard
<input type='radio' name='zaplata' value='visa'>visa
<input type='radio' name='zaplata' value='przelew'>przelew bankowy<br>
<input type='submit' name='submit' value='Dodaj'>
<input type='submit' name='submit' value='Pokaż'>
<input type='submit' name='submit' value='PHP'>
<input type='submit' name='submit' value='Java'>
<input type='submit' name='submit' value='CPP'><br>
ID:<input type='text' size='3' name='usunid'><input type='submit' name='submit' value='Usuń'><br>";
}
function walidacja($bd){
$args=[
'nazw'=>FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'wiek'=>FILTER_VALIDATE_INT,
'panstwo'=>FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'email'=>FILTER_VALIDATE_EMAIL,
'tech'=>[
'filter'=>FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'flags'=>FILTER_REQUIRE_ARRAY
],
'zaplata'=>FILTER_SANITIZE_FULL_SPECIAL_CHARS
];
$dane = filter_input_array(INPUT_GET,$args);
$errors="";
foreach($dane as $key => $val)
if($val===false or $val===NULL)
$errors.=$key." ";
if($errors==="")
dodajdoBD($bd, $dane);
else
echo "<br>Nieprawidlowe dane: ".$errors;
}
function dodajdoBD($bd, $d){
$string="";
foreach($d['tech'] as $val)
$string .= $val.',';
$string= substr($string, 0, strlen($string) - 1);
//$sql="INSERT INTO klienci VALUES (NULL, 'Babacka', '22', 'Niemcy', '
[email protected]', 'Java,CPP', 'Przelew')";
$sql="INSERT INTO klienci VALUES (NULL,'".$d['nazw']."','".$d['wiek']."','".$d['panstwo']."','".$d['email']."','".$d['tech']."','".$d['zaplata']."')";
$bd->insert($sql);
}
function usun($bd){
if(filter_input(INPUT_GET, 'usunid'))
{
$sql="DELETE FROM klienci WHERE ID=".$_GET['usunid'];
if($bd->delete($sql))
echo $sql;
else
}
}
?>
<?php
class Baza {
private $mysqli; //uchwyt do BD
public function __construct($serwer, $user, $pass, $baza) {
$this->mysqli = new mysqli($serwer, $user, $pass, $baza);
/* sprawdz połączenie */
if ($this->mysqli->connect_errno) {
printf("Nie udało sie połączenie z serwerem: %sn",
$this->mysqli->connect_error);
exit();
}
/* zmien kodowanie na utf8 */
if ($this->mysqli->set_charset("utf8")) {
//udało sie zmienić kodowanie
}
} //koniec funkcji konstruktora
function __destruct() {
$this->mysqli->close();
}
public function select($sql, $pola) {
//parametr $sql – łańcuch zapytania select
//parametr $pola - tablica z nazwami pol w bazie
//Wynik funkcji – kod HTML tabeli z rekordami (String)
$tresc = "";
if ($result = $this->mysqli->query($sql)) {
$ilepol = count($pola); //ile pól
$ile = $result->num_rows; //ile wierszy
// pętla po wyniku zapytania $results
$tresc.="<table><tbody>";
while ($row = $result->fetch_object()) {
$tresc.="<tr>";
for ($i = 0; $i < $ilepol; $i++) {
$p = $pola[$i];
$tresc.="<td>" . $row->$p . "</td>";
}
$tresc.="</tr>";
}
$tresc.="</table></tbody>";
$result->close(); /* zwolnij pamięć */
}
return $tresc;
}
}
?>
<?php
include_once("funkcje.php");
drukuj_form();
include_once "baza.php";
//tworzymy uchwyt do bazy danych:
$bd = new Baza("localhost", "root", "", "klienci");
if (filter_input(INPUT_GET, "submit")) {
$akcja = filter_input(INPUT_GET, "submit");
switch ($akcja) {
case "Dodaj" : walidacja($bd); break;
case "Pokaż" : echo $bd->select("select Nazwisko,Zamowienie from klienci",
array("Nazwisko","Zamowienie")); break;
case "Usun" break;
}
}
?>