Answer the question
In order to leave comments, you need to log in
Why is the entry not being added to the guest book?
When adding a name and a comment to the form, the message 'Error adding an entry to the guestbook' pops up, created in guestbook.php and connected via a separate file alert.php The guestbook table is created in the database (id, name,comment)
alert.php
<script type="text/javascript">
alert ("<?php echo $alert;?>");
</script>
<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>
<p><b><?php echo $name;?>:</b><?php echo $comment;?></p>
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 `questbook` (`name, `comment`) VALUES ('$name', '$comment')");
closeDB();
return $success;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question