Skip to content

Instantly share code, notes, and snippets.

@d630
Last active August 18, 2018 08:15
Show Gist options
  • Save d630/d867e6046049d746212af1c5c9d37861 to your computer and use it in GitHub Desktop.
Save d630/d867e6046049d746212af1c5c9d37861 to your computer and use it in GitHub Desktop.
PHP & MySql 1
<?php
class Data
{
protected $conn;
protected $query;
protected $fields;
public function __construct()
{
}
private function setQuery($q)
{
$this->query = $q;
}
private function setFields()
{
$this->fields = $this->query->fetch_fields();
}
public function getQuery()
{
return $this->query;
}
public function getFields()
{
return $this->fields;
}
public function connect()
{
$this->conn = new mysqli('localhost', 'root', 'root', 'buecher');
if ($this->conn->connect_error) {
echo 'error: ' . mysqli_connect_error();
return false;
}
if (!$this->conn->set_charset("utf8")) {
echo 'error: ' . $conn->error;
}
return true;
}
public function disconnect()
{
if (!$this->conn == null) {
$this->conn->kill($this->conn->thread_id);
$this->conn->close();
}
}
public function selectAllRecords()
{
$this->setQuery($this->conn->query('SELECT * FROM verlag;'));
$this->setFields();
}
public function insertRecord($rec)
{
// TODO
$name = $this->conn->real_escape_string($rec["_name"]);
$insert = "INSERT INTO verlag(name) VALUES('$name')";
$this->setQuery($this->conn->query($insert));
}
}
?>
<?php
include 'insert.php';
include 'select.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
<title>insert</title>
<link rel="stylesheet" href="/tests/src/1/css/insert.css" />
</head>
<body>
<h2>New record</h2>
<form action="insert.php" method="POST">
<table>
<tr>
<td align="right">Name:</td>
<td align="left">
<input type="text" name="_name">
</td>
</tr>
<tr>
<td></td>
<td align="right">
<button type="submit">insert</button>
</td>
</tr>
</table>
</form>
</body>
</html>
form input {
background-color: #90EE90;
color: red;
}
<?php
require_once 'Data.php';
if (isset($_POST) && sizeof($_POST) > 0) {
$db = new Data();
$db->connect();
$db->insertRecord($_POST);
$db->disconnect();
header("Location: /tests/src/1/");
} else {
include 'input.php';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
<title>select</title>
<link rel="stylesheet" href="/tests/src/1/css/select.css" />
</head>
<body>
<h2>All records</h2>
<table id='t01'>
<tr>
<?php foreach($fields as $f): ?>
<th><?= htmlspecialchars($f->name) ?></th>
<?php endforeach ?>
</tr>
<?php while($row = $query->fetch_row()): ?>
<tr>
<?php foreach($row as $val): ?>
<td><?= htmlspecialchars($val) ?></td>
<?php endforeach ?>
</tr>
<?php endwhile ?>
</table>
</body>
</html>
table#t01 {
border: 1px solid black;
border-collapse: collapse;
}
table#t01 tr {
border: 1px solid black;
padding: 12px;
}
table#t01 td {
border: 1px solid black;
padding: 12px;
}
<?php
require_once 'Data.php';
$db = new Data();
$db->connect();
$db->selectAllRecords();
$query = $db->getQuery();
$fields = $db->getFields();
include 'list.php';
$db->disconnect();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment