atmellexus
Новичок
Дата регистрации:
11.02.2014 19:39:28
Сообщений: 3
Здравствуйте.
Это guestbook.php
<h2>Добавить запись</h2>
<form name="guestbook" action="" method="post">
<table>
<tr>
<td>Имя</td>
<td>
<input type="text" name="name" />
</td>
</tr>
<tr>
<td>Комментарий</td>
<td>
<input type="text" name="comment" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="button_guestbook" value="Добавить" />
</td>
</tr>
</table>
</form>
<h2>Записи в гостевой книге</h2>
<div>
<?php
if (!empty($_POST["button_guestbook"])) {
$name = htmlspecialchars($_POST["name"]);
$comment = htmlspecialchars($_POST["comment"]);
if ((strlen($name) < 3) || (strlen($comment) < 3)) $success = false;
else $success = addGuestBookComment($name, $comment);
if (!$success) {
$alert="Ошибка при добавлении новой записи";
include "alert.php";
}
}
$comments = getAllGuestBookComments();
for ($i = 0; $i < count($comments); $i++) {
$name = $comments[$i]["name"];
$comment = $comments[$i]["comment"];
}
include "blocks/guestbook_comment.php";
?>
</div>
А это functions.php
<?php
$mysqli = false;
function connectDB() {
global $mysqli;
$mysqli = new mysqli("localhost", "root", "", "mysite-local"
;
$mysqli ->query("SET NAMES 'utf8'"
;
}
function getAllArticles () {
global $mysqli;
connectDB();
$result_set = $mysqli->query("SELECT * FROM `articles`"
;
closeDB();
return resultSetToArray($result_set);
}
function getAllGuestBookComments () {
global $mysqli;
connectDB();
$result_set = $mysqli->query("SELECT * FROM `guestbook`"
;
closeDB();
return resultSetToArray($result_set);
}
function addGuestBookComment($name, $comment) {
global $mysqli;
connectDB();
$success = $mysqli->query("INSERT INTO `guestbook` (`name`, `comment`) VALUES ('$name', '$comment'"
;
closeDB();
return $success;
}
function getArticle ($id) {
global $mysqli;
connectDB();
$result_set = $mysqli->query("SELECT * FROM `articles` WHERE `id`='$id'"
;
closeDB();
return $result_set->fetch_assoc();
}
function resultSetToArray($result_set) {
$array = array();
while (($row = $result_set->fetch_assoc()) != false)
$array[] = $row;
return $array;
}
function closeDB() {
global $mysqli;
$mysqli->close();
}
?>
Заранее спасибо.