Jump to content

Getting error around my <<<_END portioin of code


Recommended Posts

Hello All,

 

I have been trying to figure out why the <<<_END portion of the code is failing?  I am not sure what exactly is causing the error, but the other parts of the code looks accurate.  When I debug each line and I comment out the first <<<_END the errors disappear so it is something that part that I am missing.  Can anyone help me please?

 

<?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['author'])&& //checks for any inputs that might've been made

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');

 

if (isset($_POST['delete'])&& $isbn != "")

{

$query = "DELETE FROM classics WHERE isbn='$isbn'";

 

if (!mysql_query($query, $db_server))

echo "DELETE failed: $query<br />" .

mysql_error() . "<br /><br />";

}

else

{

$query = "INSERT INTO classics VALUES" .

"('$author', '$title', '$category', '$year', '$isbn')";//This part inserts values

 

if (!mysql_query($query, $db_server))

echo "INSERT failed: $query<br />" .

mysql_error() . "<br /><br />";

}

}

echo <<<_END //This area is for inputing the data to the user and then submitting the value.

<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  //begins where the second html form starts

<pre>

  Author $row[0] // this area will display the Author and Title and etc from each result

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

}

?>

 

 

Link to comment
Share on other sites

I think the problem is with the comments. You cannot have anything directly after <<<_END. Move your comments up a line

//This area is for inputing the data to the user and then submitting the value.
echo <<<_END
...
_END;

 

Also please wrap any code posted within code (


or


) tags Makes reading code easierh

Link to comment
Share on other sites

Thank  you WildTeen88.  That's what I figured too...I tried that just now and it still didn't work :( 

Is my formatting wong?

 

 

 

<?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['author'])&& //checks for any inputs that might've been made

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');

 

if (isset($_POST['delete'])&& $isbn != "")

{

$query = "DELETE FROM classics WHERE isbn='$isbn'";

 

if (!mysql_query($query, $db_server))

echo "DELETE failed: $query<br />" .

mysql_error() . "<br /><br />";

}

else

{

$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 />";

}

}

//This area is for inputing the data to the user and then submitting the value.

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

}

?>

 

 

Link to comment
Share on other sites

Wow thanks WildTeen88!!  That worked thank you so much!  I didn't even think to check that..if I was getting an error within a config file or .properties file in Unix I would have thought to do that...but I never would have thought that about PHP!  I will keep this in mind going forward :)

 

Cheers MATE!

Link to comment
Share on other sites

  • 3 years later...

Hi,

I am new to PHP and I am trying to learn the language thanks to the O'REILLY book "Learning PHP, MySQL, Javascript & CSS" by Robin Nixon.

The code shown above by michaelsaintclaire is actually an example of this book and I have the exact same problem as he had, but the proposed answers did not fix the problem.

As I am working on my own database, I have a slightly different code:

<?php //sqltest.php
 
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
 
if (!$db_server)
die("Impossible de se connecter à MySQL : " . mysql_error());
 
mysql_select_db($db_database, $db_server)
or die("Impossible de selectionner la base de donnees : " . mysql_error());
 
if (isset($_POST['delete']) && isset($_POST['id']))
{
$id = get_post('id');
$query = "DELETE FROM publications WHERE id= '$id'";
 
if (!mysql_query($query, $db_server))
echo "La suppression a echoue : $query<br />" .
mysql_error() . "<br /><br />";
}
 
if (isset($_POST['auteur']) &&
isset($_POST['titre']) &&
isset($_POST['categorie']) &&
isset($_POST['message']) &&
isset($_POST['id']))
 
{
$auteur = get_post('auteur');
$titre = get_post('titre');
$categorie = get_post('categorie');
$message = get_post('message');
$id = get_post('id');
 
$query = "INSERT INTO publications VALUES" .
"('$auteur', '$titre', '$categorie', '$message', '$id')";
 
if (!mysql_query($query, $db_server))
echo "L\'insertion a echoue : $query<br />" .
mysql_error() . "<br /><br />";
}
 
echo <<<_END
<form action="sqltest.php" method="post">
<pre>
   Auteur <input type="text" name="auteur" />
    Titre <input type="text" name="titre" />
Categorie <input type="text" name="categorie" />
  Message <input type="text" name="message" />
       Id <input type="text" name="id" />
<input type = "submit" value="Ajouter un message" />
</pre></form>
_END;
 
$query = "SELECT * FROM publications";
$result = msql_query($query);
 
if (!$result) die ("Acces impossible a la base de donnees : " . msql_error());
$rows = mysql_num_rows($result);
 
for ($j = 0 ; $j < $rows ; ++$j)
{
$row = mysql_fetch_row($result);
echo <<<_END
<pre>
   Auteur $row[0]
    Titre $row[1]
Categorie $row[2]
  Message $row[3]
       ID $row[4]
</pre>
<form action="sqltest.php" method="post">
<input type="hidden" name="delete" value="yes" />
<input type="hidden" name="id" value="$row[4]" />
<inpute type="submit" value="Supprimer le message" /></form>
_END;
}
 
msql_close($db_server);
 
function get_post($var)
{
return mysql_real_escape_string($_POST[$var]);
}
 
?>

When I run the program, only the form is displayed.

I know that the values entered into the form are actually recorded in the database, since I could see them appear through PHPMyAdmin.

However, the content of the database is not displayed on the web page, accordingly to what the program is supposed to do.

So, I guess the problem comes from this part of the code:

 

 

 
$query = "SELECT * FROM publications";
$result = msql_query($query);
 
if (!$result) die ("Acces impossible a la base de donnees : " . msql_error());
$rows = mysql_num_rows($result);
 
for ($j = 0 ; $j < $rows ; ++$j)
{
$row = mysql_fetch_row($result);
echo <<<_END
<pre>
   Auteur $row[0]
    Titre $row[1]
Categorie $row[2]
  Message $row[3]
       ID $row[4]
</pre>
<form action="sqltest.php" method="post">
<input type="hidden" name="delete" value="yes" />
<input type="hidden" name="id" value="$row[4]" />
<inpute type="submit" value="Supprimer le message" /></form>
_END;
}
 

Do you have any idea of what I did wrong with this piece of code?

Many thanks in advance,

Thibaud

 

Edited by ThibaudClement
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.