Page 1 of 1

Problem with echo <<<_END

Posted: Mon Aug 16, 2010 4:17 pm
by aknbad23
Here is my code to basically display, add, and delete from a table but it get errors between END statements. What am I doing wrong?

Code: Select all

<?php // sqltest.php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);

if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());

mysql_select_db($db_database, $db_server) or die("Unable to select database: " . mysql_error());

if (isset($_POST['delete']) && isset($_POST['isbn']))
{
    $isbn = get_post('isbn');
    $query = "DELETE FROM classics WHERE isbn='$isbn'";
    if (!mysql_query($query, $db_server)) echo "DELETE failed: $query<br />" . mysql_error() . "<br /><br />";

}

if (isset($_POST['author']) &&
        isset($_POST['title']) &&
        isset($_POST['category']) &&
        isset($_POST['year']) &&
        isset($_POST['isbn']))
{
    $author = get_post('author');
    $title = get_post('title');
    $category = get_post('category');
    $year = get_post('year');
    $isbn = get_post('isbn');

    $query = "INSERT INTO classics VALUES" . "('$author', '$title', '$category', '$year', '$isbn')";

    if (!mysql_query($query, $db_server)) {echo "INSERT failed: $query<br />" . mysql_error() . "<br /><br />";}

}

echo <<<_END  

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

$query = "SELECT * FROM classics";
$result = mysql_query($query);

if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);

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

echo <<<_END
<pre>
 Author $row[0]
  Title $row[1]
Category $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;
}

mysql_close($db_server);
function get_post($var)
{
    return mysql_real_escape_string($_POST[$var]);
}
?>

Re: Problem with echo <<<_END

Posted: Mon Aug 16, 2010 4:34 pm
by Weirdan
post the error message you're getting.

Re: Problem with echo <<<_END

Posted: Mon Aug 16, 2010 5:10 pm
by aknbad23
This block is the problem:

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

For the echo <<<_END i get:
expected: exit, identifier, variable, function, isset, empty, array, include, include_once.........

For the lines afterwards I get:
Syntax Error:
expected: variable, instanceof, as, implements, =>,{,},::,',',OR,XOR,AND,?..........

Re: Problem with echo <<<_END

Posted: Mon Aug 16, 2010 7:26 pm
by requinix
Those are NetBeans error messages. Not messages from PHP.

PHP says
Parse error: syntax error, unexpected T_SL in * on line 35
Googling that gives a number of results. For me, the summary of the second result says
The PHP error “parse error, unexpected T_SL” usually appears when the trailing spaces are after the end of “EOF” word
Lo and behold,

Code: Select all

35. echo <<<_END  ¶