Unexpected end of file in php?

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

    <?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]);
    }
   ?>

      

+3


source to share


2 answers


Note:

  • You forgot the underscore ( _

    ) character for one of your heredocs.
  • You forgot the columns for the insert query.
  • You forgot >

    on yours $result = $conn-query($query);

    , it must 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')";

      

+1


source


Heredocs are pretty strict, when they end so that they don't accidentally end somewhere you don't want them to, they must end with leading characters preceded by absolutely nothing (no spaces, just a newline) as a colon.

So this:

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

      



is invalid syntax, this will be the correct syntax:

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

      

0


source







All Articles