XAMPP – Uue kasutaja, andmebaasi ja tabeli loomine


Config fail sätted
<?php
//sinu sätted võivad olla teised
$serverName='localhost';
$username='jaan';
$password='123456';
$database='jaan';
$connection=new mysqli($serverName,$username,$password,$database);
$connection->set_charset('UTF8');
/*CREATE TABLE loomad(
id int PRIMARY KEY AUTO_INCREMENT,
nimi varchar(50),
kirjeldus text
);
INSERT INTO loomad(nimi, kirjeldus) VALUES('kass','väga hea sõber');
SELECT * from loomad;*/
Looma tabeli näitamine
<?php
require("conf.php");
?>
<!doctype html>
<html lang="en">
<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>Andmetabeli sisu näitamine</title>
</head>
<body>
<h1>Andmetabeli "Loomad" sisu näitamine</h1>
<?php
global $connection;
//showing data in a table
$order=$connection->prepare("SELECT id,nimi,kirjeldus FROM loomad");
$order->bind_result($id,$nimi,$kirjeldus);
$order->execute();
echo "<table>";
echo "<tr>
<th>ID</th>
<th>Loomanimi</th>
<th>Kirjeldus</th>
</tr>";
while($order->fetch()){
echo "<tr>";
echo "<td>$id</td>";
echo "<td>$nimi</td>";
echo "<td>$kirjeldus</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

Looma tabeli lisamine
<?php
require("conf.php");
global $connection;
$order=$connection->prepare("INSERT INTO loomad(nimi) VALUES (?)");
//"s"-string; $_REQUEST['loomanimi'] query in input textbox with name="loomanimi"
//Kontrollime, kas tekstikast pole tühi ja siis lisame uue andmed
if (!empty($_REQUEST['loomanimi'])) {
$order->bind_param("s", $_REQUEST['loomanimi']);
$order->execute();
//changes adress bar
header("Location: tableAddandShow.php");
}
?>
<br>
//form, kust me võttame uued andmed kasutajast
<form action="" method="post">
<label for="nimi">Loomanimi</label>
<input type="text" name="loomanimi" id="nimi" maxlength="50">
<input type="submit" value="Lisa">
</form>
<?php
$order->close();
?>

Looma tabeli kustutamine
function clearVarsExcept($username){
return strtok(basename($_SERVER['REQUEST_URI']), "?")."?$username=".$_REQUEST[$username];
}
if (isset($_REQUEST['kustuta'])){
$order=$connection->prepare('DELETE FROM loomad where id=?');
$order->bind_param("i",$_REQUEST['kustuta']);
$order->execute();
}
//change the cycle
while($order->fetch()){
echo "<tr>";
echo "<td>$id</td>";
echo "<td>$nimi</td>";
echo "<td>$kirjeldus</td>";
echo "<td><a href='$_SERVER[PHP_SELF]?kustuta=$id'>Kustuta</a></td>";
echo "</tr>";
}
while($order->fetch()){
echo "<tr>";
echo "<td>$id</td>";
echo "<td>$nimi</td>";
echo "<td>$kirjeldus</td>";
echo "<td><a href='".clearVarsExcept("leht")."&kustuta=$id'>Kustuta</a></td>";
echo "</tr>";
}
echo "</table>";
?>
<br>
<form action="<?=clearVarsExcept('leht')?>" method="post">
<label for="nimi">Loomanimi</label>
<input type="text" name="loomanimi" id="nimi" maxlength="50">
<label for="Kirjeldus">Kirjeldus</label>
<input type="text" name="Kirjeldus" id="Kirjeldus">
<input type="submit" value="Lisa">
</form>

Looma tabeli faili lõpp versioon
<?php
require("conf.php");
function clearVarsExcept($username){
return strtok(basename($_SERVER['REQUEST_URI']), "?")."?$username=".$_REQUEST[$username];
}
global $connection;
//"s"-string; $_REQUEST['loomanimi'] query in input textbox with name="loomanimi"
if (!empty($_REQUEST['loomanimi'])) {
$order=$connection->prepare("INSERT INTO loomad(nimi,kirjeldus) VALUES(?, ?)");
$order->bind_param("ss", $_REQUEST['loomanimi'],$_REQUEST['Kirjeldus']);
$order->execute();
//changes adress bar
//$_SERVER[PHP_SELF] before file name
// header("Location: $_SERVER[PHP_SELF]");
}
if (isset($_REQUEST['kustuta'])){
$order=$connection->prepare('DELETE FROM loomad where id=?');
$order->bind_param("i",$_REQUEST['kustuta']);
$order->execute();
}
?>
<h1>Andmetabeli "Loomad" sisu näitamine ja lisamine</h1>
<?php
global $connection;
//showing data in a table
$order=$connection->prepare("SELECT id,nimi,kirjeldus FROM loomad");
$order->bind_result($id,$nimi,$kirjeldus);
$order->execute();
echo "<table>";
echo "<tr>
<th>ID</th>
<th>Loomanimi</th>
<th>Kirjeldus</th>
<th>Tegevus</th>
</tr>";
while($order->fetch()){
echo "<tr>";
echo "<td>$id</td>";
echo "<td>$nimi</td>";
echo "<td>$kirjeldus</td>";
echo "<td><a href='".clearVarsExcept("leht")."&kustuta=$id'>Kustuta</a></td>";
echo "</tr>";
}
echo "</table>";
?>
<br>
<form action="<?=clearVarsExcept('leht')?>" method="post">
<label for="nimi">Loomanimi</label>
<input type="text" name="loomanimi" id="nimi" maxlength="50">
<label for="Kirjeldus">Kirjeldus</label>
<input type="text" name="Kirjeldus" id="Kirjeldus">
<input type="submit" value="Lisa">
</form>
<?php
$order->close();
?>
Puu tabeli andmete muutmine
if(isset($_REQUEST ['muuda'])) {
$order=$connection->prepare("UPDATE puud SET nimi=?,puulink=? WHERE id=?");
$order->bind_param("ssi", $_REQUEST['nimi'],$_REQUEST['pilt'],$_REQUEST['muuda']);//ssi - str str int
$order->execute();
}
if($order-> fetch()){
if(isset($_REQUEST['muutmine'])){
echo "<br><form action='$_SERVER[PHP_SELF]'>
<input type='hidden' name='muuda' value='$id'>
<h2>Puu Andmete Muutmine</h2>
Puunimi:
<input type='text' name='nimi' value='$nimi'><br>
Pilti link:
<textarea name='pilt'>$puulink</textarea>
<input type='submit' value='Muuda'>
</form>";
}
echo "<strong>".$nimi."</strong><br>";
echo "<img src='$puulink' alt='puulink' width=500px height=400px margin-right:1%>";
echo "<a href='$_SERVER[PHP_SELF]?kustuta=$id'>Kustuta</a>";
echo "<br><a href='$_SERVER[PHP_SELF]?id=$id&muutmine=jah'>Muuda</a>";
}else{
echo "ERROR CODE: Existence";
}
}

Puu tabeli faili lõpp versioon
<?php
require('conf.php');
global $connection;
//lisamine INSERT INTO
if(!empty($_REQUEST['puuvorm'])){
$order=$connection->prepare('INSERT INTO puud(nimi, puulink) VALUES(?, ?)');
$order->bind_param('ss', $_REQUEST['puunimi'], $_REQUEST['puulink']); // s - string
$order->execute();
//изменяет адресную строку
//$_SERVER[PHP_SELF] - до имени файла
header("Location: $_SERVER[PHP_SELF]");
}
//DELETE FROM
if(isset($_REQUEST ['kustuta'])) {
$order = $connection->prepare("DELETE FROM puud WHERE id=?");
$order->bind_param("i", $_REQUEST['kustuta']);
$order->execute();
}
//UPDATE
if(isset($_REQUEST ['muuda'])) {
$order=$connection->prepare("UPDATE puud SET nimi=?,puulink=? WHERE id=?");
$order->bind_param("ssi", $_REQUEST['nimi'],$_REQUEST['pilt'],$_REQUEST['muuda']);//ssi - str str int
$order->execute();
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="../css/style.css">
<title>Puu Andmebaas</title>
</head>
<body>
<h1>Puu Andmebaas</h1>
<div class="leftcolumn">
<h2>Puud</h2>
<?php
//nüitamine puunimed
global $connection;
$order=$connection->prepare("SELECT id, nimi FROM puud");
$order->bind_result($id,$nimi);
$order->execute();
echo "<ul>";
while($order->fetch()){
echo "<li><a href='$_SERVER[PHP_SELF]?id=$id'>".$nimi."</a></li>";
}
echo "</ul>";
echo "<a href='$_SERVER[PHP_SELF]?lisa=jah'>Lisa...</a>";
if (isset($_REQUEST['lisa'])){
echo "<br><form action='' method='post'>
<input type='hidden' name='puuvorm' value='jah'>
<label for='puunimi'>Puu nimi</label>
<input type='text' name='puunimi' id='puunimi'>
<br><br>
<label for='puulink'>Puu pilti link</label>
<textarea name='puulink' id='puulink'></textarea>
<input type='submit' value='Lisa'>
</form>";
}
?>
</div>
<div class="rightcolumn">
<h3>Puu pilt</h3>
<?php
//nüitamine puunimed
global $connection;
if(isset($_REQUEST['id'])){
$order=$connection->prepare("SELECT id, nimi,puulink FROM puud WHERE id=?");
$order->bind_param('i', $_REQUEST['id']);
$order->bind_result($id,$nimi, $puulink);
$order->execute();
if($order-> fetch()){
if(isset($_REQUEST['muutmine'])){
echo "<br><form action='$_SERVER[PHP_SELF]'>
<input type='hidden' name='muuda' value='$id'>
<h2>Puu Andmete Muutmine</h2>
Puunimi:
<input type='text' name='nimi' value='$nimi'><br>
Pilti link:
<textarea name='pilt'>$puulink</textarea>
<input type='submit' value='Muuda'>
</form>";
}
echo "<strong>".$nimi."</strong><br>";
echo "<img src='$puulink' alt='puulink' width=500px height=400px margin-right:1%>";
echo "<a href='$_SERVER[PHP_SELF]?kustuta=$id'>Kustuta</a>";
echo "<br><a href='$_SERVER[PHP_SELF]?id=$id&muutmine=jah'>Muuda</a>";
}else{
echo "ERROR CODE: Existence";
}
}
?>
</div>
</body>
</html>
Puu tabeli link
Pages: 1 2