<?php
  require "header.inc";

  //post
  if (!isset($_POST["fromPage"])){
    echo "      <h1>you come from nowhere</h1>";
    echo "      <a href='wikitroll.php' >Better start somewhere</a>";
  }
  else if (!isset($_POST["title"])){
    echo "      <h1>without a title your text is meaningless</h1>\n";
    echo "      <a href='wikitroll.php?".$_POST["fromPage"]."' >return to previous page</a>\n";
  }
  else if (!isset($_POST["text"])){
    echo "      <h1>do you think that writing nothing will mean something</h1>\n";
    echo "      <a href='wikitroll.php?".$_POST["fromPage"]."' >return to previous page</a>\n";
  }
  else{
    $fromPage = $_POST["fromPage"];
    $title = $_POST["title"];
    $text = $_POST["text"];

    //add some validation

    //insert in database
    // Create connection
    $con=mysqli_connect("localhost","troller","","trololo");
    // Check connection
    if (mysqli_connect_errno($con))
      echo "      <h1>Failed to connect to MySQL: " . mysqli_connect_error() . "</h1>\n";
    //query
    mysqli_query($con,"INSERT INTO page (fromPage, title, txt) VALUES (".$fromPage.", '".$title."','".$text."')");
    //close
    mysqli_close($con);

    echo "      <h1>Congratulation you are a writer</h1>\n";
    echo "      <a href='wikitroll.php?page=".$fromPage."' >return to page</a>\n";
  }

   require "footer.inc";
?>

