Davidk67
Davidk67

Reputation: 41

Unexpected end of file in php?

I am writing up this php form for a project and for some reason I am getting an error message at the end of the code? Can somebody explain to me why this is happening? It would really help for future situations like this.

    <?php


    require_once'login.php';
    $conn=new mysqli($hn,$un,$pw,$db);
    if($conn->connect_error) die($conn->connect_error);


    if (isset($_POST['delete']) && isset($_POST['isbn']))
    {

        $isbn = get_post($conn, 'isbn');
        $query =  "DELETE FROM publications WHERE isbn='isbn'";
        $result = $conn->query($query);
        if(!$result) echo "DELETE failed: $query </br>".
            $conn->error."</br></br>";

    }

        if (isset($_POST['author']) &&
        isset($_POST['title']) &&
        isset($_POST['type']) &&
        isset($_POST['year']) &&
        isset($_POST['isbn']))
    {

        $author = get_post($conn, 'author');
        $title = get_post($conn, 'title');
        $type = get_post($conn, 'type');
        $year = get_post($conn, 'year');
        $isbn = get_post($conn, 'isbn');
        $query = "INSERT INTO publications VALUES".
            "( '$author',  '$title', '$type', '$year' ,'$isbn')";
        $result = $conn->query($result);
        if(!$result) echo "INSERT failed :$query</br>".
            $conn->error . "</br></br>";


    }


    echo <<<END
    <form action = "sqltest.php" method="post"><pre>        
    Author <input type="text" name = "author">
    Title <input type="text" name = "title">
    Type <input type="text" name="Type">
    Year <input type="text" name="year">
    ISBN <input type="text" name="isbn">
    <input type="submit" value="ADD RECORD">
    </pre></form>
    _END;

    $query = "SELECT * FROM publications" ;

    $result = $conn-query($query);
        if(!$result) die("Database access failed:".$conn->error);

    $rows = $result->num_rows;

    for($j = 0; $j < $rows; ++$j)
   {

        $result->data_seek($j);
        $row = $result->fetch_array(MYSQLI_NUM);

    echo <<<_END
<pre>
Author $row[0]
Title $row[1]
Type $row[2]
Year $row[3]
ISBN $row[4]
</pre>
<form action ="sqltest.php" method="post">
<input type="hidden" name="delete" value="yes">
<input type="hidden" name ="isbn" value="$row[4]">
<input type="submit" value="DELETE RECORD"></form>
   _END;
  }
    $result->close();
    $conn->close();

    function get_post($conn, $var)
    {
        return $conn->real_escape_string($_POST[$var]);
    }
   ?>

Upvotes: 3

Views: 89

Answers (2)

Prime
Prime

Reputation: 2482

Heredocs are pretty strict about when they end so that it doesn't accidentally end somewhere you didn't want it to, it must be ended with the opening characters preceded by absolutely nothing (no whitespace, only a new line) followed by a semi-colon.

So this:

    echo <<< END
    Some words and stuff
    END; //Whitespace before END;

is invalid syntax, this would be the correct syntax:

    echo <<< END
    Some words and stuff
END; //No whitespace before END;

Upvotes: 0

Logan Wayne
Logan Wayne

Reputation: 5991

Note:

  • You forgot an underscore(_) to one of your heredoc.
  • You forgot the columns for your insert query.
  • You forgot > on your $result = $conn-query($query);, it should be $result = $conn->query($query);

Code:

echo <<<_END
  <form action = "sqltest.php" method="post"><pre>        
  Author <input type="text" name = "author">
  Title <input type="text" name = "title">
  Type <input type="text" name="Type">
  Year <input type="text" name="year">
  ISBN <input type="text" name="isbn">
  <input type="submit" value="ADD RECORD">
  </pre></form>
_END;

Insert query:

$query = "INSERT INTO publications VALUES (column1, column2, column3, column4)".
         "('$author',  '$title', '$type', '$year' ,'$isbn')";

Upvotes: 1

Related Questions