Created
July 13, 2019 14:47
-
-
Save qutal/d10ecb5b911fdccae597bfc9c49a76a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$user_password=''; | |
$user='root'; | |
$data_base='qutal'; | |
$table='db'; | |
$host='localhost'; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>База данных</title> | |
</head> | |
<body> | |
<form name="DB" action="" method="post"> | |
<label>Имя</label> <input type="text" name="name" placeholder="имя"> <br> | |
<label>Текст</label><br> | |
<textarea name="text" cols="15" rows="15"></textarea><br> | |
<?php | |
/*База данных называетя qutal, таблица - db, параметры для подключения расположены в connection.php*/ | |
if(isset($_POST["save"])){ | |
require_once 'connection.php'; | |
$name=$_POST["name"]; | |
$text=$_POST["text"]; | |
$link=mysqli_connect($host,$user,$user_password,$data_base) or die("Ошибка подключения:".mysqli_close($link)); | |
$query="INSERT INTO `$table`(`name`,`_text`) VALUES ('$name','$text')"; | |
$res=mysqli_query($link,$query) or die("Ошибка записи:".mysqli_close($link)); | |
mysqli_close($link); | |
} | |
if(isset($_POST["print_all"])){ | |
require_once 'connection.php'; | |
$link=mysqli_connect($host,$user,$user_password,$data_base) or die("Ошибка подключения:".mysqli_close($link)); | |
$query="SELECT * FROM `$table` WHERE 1"; | |
$res=mysqli_query($link,$query) or die("Ошибка чтения все записей:".mysqli_close($link)); | |
$res=mysqli_fetch_assoc($res); | |
if(!$res){ | |
echo "Нет записей <a href='http://database'>Назад</a>"; | |
} | |
$i=0; | |
while ($res) { | |
$i++; | |
for(reset($res);$k=key($res);next($res)){ | |
echo "$res[$k] "; | |
} | |
echo "<br>"; | |
//LIMIT пропускает i строк и печатает 1 строку | |
$res=mysqli_query($link,"SELECT * FROM `$table` LIMIT $i,1") or die("Ошибка чтения все записей:".mysqli_close($link)); | |
$res=mysqli_fetch_assoc($res); | |
} | |
if($i>0){ | |
echo "<a href='http://database'>Назад</a>"; | |
} | |
mysqli_close($link); | |
} | |
if(isset($_POST["print_by_name"])){ | |
require_once 'connection.php'; | |
$find_name=$_POST["find_name"]; | |
$link=mysqli_connect($host,$user,$user_password,$data_base) or die("Ошибка подключения:".mysqli_close($link)); | |
$query="SELECT * FROM `$table` WHERE `name`='$find_name'"; | |
$res=mysqli_query($link,$query) or die("Ошибка чтения одной записи:".mysqli_close($link)); | |
$res=mysqli_fetch_assoc($res); | |
if(!$res){ | |
echo "Таких записей нет <a href='http://database'>Назад</a>"; | |
} | |
$i=0; | |
while ($res) { | |
$i++; | |
for(reset($res);$k=key($res);next($res)){ | |
echo "$res[$k] "; | |
} | |
echo "<br>"; | |
$res=mysqli_query($link,"SELECT * FROM `$table` WHERE `name`='$find_name' LIMIT $i,1") or die("Ошибка чтения все записей:".mysqli_close($link)); | |
$res=mysqli_fetch_assoc($res); | |
} | |
if($i>0){ | |
echo "<a href='http://database'>Назад</a>"; | |
} | |
mysqli_close($link); | |
} | |
?> | |
<br> | |
<input type="submit" name="save" value="Сохранить"> <input type="submit" name="print_all" value="Распечатать все" > <br> | |
<label>Имя</label> <input type="text" name="find_name" placeholder="имя"> <br> | |
<input type="submit" name="print_by_name" value="Распечатать по имени"> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment