S
S
Sandro_s2018-05-02 14:13:45
PHP
Sandro_s, 2018-05-02 14:13:45

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>

guestbook.php (blocks)
<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>

guestbook_comment.php (in blocks folder)
<p><b><?php echo $name;?>:</b><?php echo $comment;?></p>

functions.php
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

1 answer(s)
I
ipokos, 2018-05-02
@ipokos

Based on check:
I believe that the length of the name or comment is less than 3 characters
p.s. what is the PHPMYADMIN tag for?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question