<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Language" content="en" />
    <link rel="stylesheet" href="style.css" />
    <title>Add new message</title>
  </head>
  <body>
<center>

<?php

require "../libraries/database-mysql.php";

if (!database_connect())
{
  echo "Sorry, fail connect to database.";
  return;  
}

if ($_POST["add"]==1)
{
  $name=$_POST["name"];
  $text=$_POST["text"];
  $result=database_query("SELECT * FROM text WHERE name LIKE '$name'");
  if (database_num_rows($result)==0)
  {
    database_query("INSERT INTO text (name, text) VALUES ('$name', '$text')");
  }
  else
  {
    echo "Name of your text is already in db.";
  }
}

?>

<br><br><strong>Add message:</strong>
<form action="" method="post">
Name: <input type="text" name="name" size="20" value="<?php echo $name; ?>"><br>
Text: <input type="text" name="text" size="50" value="<?php echo $text; ?>"><br>
<input type="hidden" name="add" value="1">
<input type="submit" value="Add new message">
</form>

<br><br><table border="1">
<?php

$result=database_query("SELECT * FROM text ORDER BY text_id DESC");
if (database_num_rows($result)>0) //zaznam existuje v db
  {
    while ($result_db = database_fetch_array($result))
    {
      ?><tr><td><strong><?php echo $result_db["name"];?></strong></td><td><?php echo $result_db["text"];?></td></tr><?php
    }
  }
  else //porovnani se nazdarilo, dana hlaska neni v db, zobraz univerzalni
  {
    echo "Tab is empty...";
  }

?>
</table>
<?php

database_close();
  
?>
</center>
</body>
</html>
