Page 1 of 1

error! why? please help!

Posted: Thu Aug 15, 2002 5:31 am
by dreamtime
hi guys I am getting this error what does it mean?

Parse error: parse error, unexpected T_STRING in d:\InetPub\wwwroot\wask\news.php on line 1

the full code for this page is below

<?
if($action == "edit" && isset($HTTP_POST_VARS['password'])) {
//obviously you should change this password on the next line
if($HTTP_POST_VARS['password'] == "editpass") {
//First let's recompile that line with the pipe symbols so we can reinsert it
$line = $HTTP_POST_VARS['date'] . "|" . $HTTP_POST_VARS['name'];
$line .= "|" . $HTTP_POST_VARS['news'];
$line = str_replace("\r\n","<BR>",$line);
$line .= "\r\n";
$data = file('news.txt');
$data[$id] = $line;
//the next line makes sure the $data array starts at the beginning
reset($data);
//now we open the file with mode 'w' which truncates the file
$fp = fopen('news.txt','w');
foreach($data as $element) {
fwrite($fp, $element);
}
fclose($fp);
echo "Item Edited!<BR><BR>\n";
echo "<a href=\"$PHP_SELF\">Go Back</a>\n";
exit;
} else {
echo "Bad password!\n";
exit;
}
}
if($action == "edit") {
$data = file('news.txt');
$element = trim($data[$id]);
$pieces = explode("|", $element);
//the next line is to reverse the process of turning the end of lines into breaking returns
$news = str_replace("<BR>","\r\n",$pieces[2]);
echo "Make the changes you would like and press save.<BR>\n";
echo "<FORM ACTION=\"$PHP_SELF?action=edit\" METHOD=\"POST\" NAME=\"editform\">\n";
echo "Name:<BR>\n";
echo "<INPUT TYPE=\"text\" SIZE=\"30\" NAME=\"name\" value=\"".$pieces[1]."\"><BR>\n";
echo "The News:<BR>\n";
echo "<TEXTAREA NAME=\"news\" COLS=\"40\" ROWS=\"5\">".$news."</TEXTAREA><BR><BR>\n";
echo "Password:<BR>\n";
echo "<INPUT TYPE=\"password\" SIZE=\"30\" NAME=\"password\"><BR>\n";
echo "<INPUT TYPE=\"hidden\" NAME=\"date\" VALUE=\"".$pieces[0]."\">\n";
echo "<INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"$id\">\n";
echo "<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Save\"><BR>\n";
echo "</FORM>\n";
exit;
}
if($action == "delete" && isset($HTTP_POST_VARS['password'])) {
//obviously you should change this password on the next line
if($HTTP_POST_VARS['password'] == "deletepass") {
$data = file('news.txt');
//this next line will remove the single news item from the array
array_splice($data,$id,1);
//now we open the file with mode 'w' which truncates the file
$fp = fopen('news.txt','w');
foreach($data as $element) {
fwrite($fp, $element);
}
fclose($fp);
echo "Item deleted!<BR><BR>\n";
echo "<a href=\"$PHP_SELF\">Go Back</a>\n";
exit;
} else {
echo "Bad password!\n";
exit;
}
}
if($action == "delete") {
echo "<H2>You are about to delete the following news item.</H2>\n";
$data = file('news.txt');
$element = trim($data[$id]);
$pieces = explode("|", $element);
echo $pieces[2] . "<BR>" . "<b>Posted by " . $pieces[1] . " on " . $pieces[0] . "</b>\n";
echo "<BR><BR>\n";
echo "Are you sure you want to delete this news item? If so, enter the password and click on Delete.<BR>\n";
echo "<FORM ACTION=\"$PHP_SELF?action=delete\" METHOD=\"POST\" NAME=\"deleteform\">\n";
echo "Password:<BR>\n";
echo "<INPUT TYPE=\"password\" SIZE=\"30\" NAME=\"password\"><BR>\n";
echo "<INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"$id\">\n";
echo "<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Delete\"><BR>\n";
echo "</FORM>\n";
exit;
}


echo "<H1><u>Current News</u></H1>\n";
$data = file('news.txt');
//next line removed to make everything else easier in the admin script
//$data = array_reverse($data);
foreach($data as $key=>$element) {
$element = trim($element);
$pieces = explode("|", $element);
echo $pieces[2] . "<BR>" . "<b>Posted by " . $pieces[1] . " on " . $pieces[0] . "</b>\n";
echo "&nbsp;<a href=\"$PHP_SELF?action=delete&id=$key\">Delete</a>\n";
echo "&nbsp;<a href=\"$PHP_SELF?action=edit&id=$key\">Edit</a>\n";
echo "<BR><BR>\n";
}
echo "<HR>\n";
echo "<H1><u>Add News</u></H1>\n";
if($HTTP_POST_VARS['submit']) {
if($HTTP_POST_VARS['password'] == 'pass') {
if(!$HTTP_POST_VARS['name']) {
echo "You must enter a name";
exit;
}
if(!$HTTP_POST_VARS['news']) {
echo "You must enter some news";
exit;
}
if(strstr($HTTP_POST_VARS['name'],"|")) {
echo "Name cannot contain the pipe symbol - |";
exit;
}
if(strstr($HTTP_POST_VARS['news'],"|")) {
echo "News cannot contain the pipe symbol - |";
exit;
}
$fp = fopen('news.txt','a');
if(!$fp) {
echo "Error opening file!";
exit;
}
$line = date("m.d.y") . "|" . $HTTP_POST_VARS['name'];
$line .= "|" . $HTTP_POST_VARS['news'];
$line = str_replace("\r\n","<BR>",$line);
$line .= "\r\n";
fwrite($fp, $line);
if(!fclose($fp)) {
echo "Error closing file!";
exit;
}
echo "<b>News added!</b>\n";
} else {
echo "Bad Password";
}
}

?>
<FORM ACTION="<?=$PHP_SELF?>" METHOD="POST" NAME="newsentry">
Your name:<BR>
<INPUT TYPE="text" SIZE="30" NAME="name"><BR>
The News:<BR>
<TEXTAREA NAME="news" COLS="40" ROWS="5"></TEXTAREA><BR><BR>
News Password:<BR>
<INPUT TYPE="password" SIZE="30" NAME="password"><BR>
<INPUT TYPE="submit" NAME="submit" VALUE="Post it!"><BR>
</FORM>

if anyone can help I would appreciate it.

Posted: Thu Aug 15, 2002 5:42 am
by volka
hmmm...copied the code and ran it.
It's complaining about some undefined vars - but that was to be expected.
Sure you posted the complete script?
Parse error: parse error, unexpected T_STRING in d:\InetPub\wwwroot\wask\news.php on line 1
^ the very first line is the interesting one ;)

Posted: Thu Aug 15, 2002 5:46 am
by twigletmac
Just had the same result as volka... Generally we won't need to see the whole script for an error on line 1 though.

Mac

Posted: Thu Aug 15, 2002 6:13 am
by ReDucTor
Line 1 is <? this doesn't have any string tokens(', ", etc) in it, there for there has to be something your just not showing us :P

Posted: Thu Aug 15, 2002 6:46 am
by dreamtime
sorry guys it is the full code. It is actually from a tutorial at http://codewalkers.com/tutorials.php?show=7&page=5 you will see all I have done is cut and paste.

Posted: Thu Aug 15, 2002 6:58 am
by Beefy
isset() and empty() in a control structure like if sometimes (I don't know whne and why) cause this error.

The solution could be: make two if-clauses out of the one...perhaps it'll work.

Posted: Thu Aug 15, 2002 7:03 am
by dreamtime
what does that mean in english? Im only a newbie to php or I wouldnt use the tutorial, I just need this news posting for my site

Posted: Thu Aug 15, 2002 7:12 am
by twigletmac
It's really weird that you get this error because cutting and pasting the same code I don't and parse errors aren't system specific (are they?). Are you sure you didn't accidentally type an extra character on the first line of your own version of this code?

I doubt it's the isset() or empty() functions causing issues, I've never seen them cause parse errors.

Mac

Posted: Thu Aug 15, 2002 7:26 am
by dreamtime
what does that mean in english? Im only a newbie to php or I wouldnt use the tutorial, I just need this news posting for my site

Posted: Thu Aug 15, 2002 7:49 am
by twigletmac
OK, you've got a parse error when you run the code that nobody else can replicate when they run the same code. This (curtesy of dale) may help explain a bit more what causes a parse error: http://www.imahosting.com/dale/php/

What does the first line (after the <?php bit) of your version of the code that you are running to get this error look like?

Mac

Posted: Thu Aug 15, 2002 7:51 am
by dreamtime
if($action == "edit" && isset($HTTP_POST_VARS['password'])) {

Posted: Thu Aug 15, 2002 8:35 am
by twigletmac
What is your setup, ie. which version of PHP, which OS and which webserver. I know you're probably really frustrated with all of this but it's very difficult to help without being able to reproduce the error. One thing you might want to try (if you haven't already) is to copy and paste the code again into a new file and try and run that.

Sorry that we haven't been able to be of better help.

Mac

Posted: Thu Aug 15, 2002 8:43 am
by dreamtime
no its cool man thanks for the help. I am running on a windows nt server (not my choice its the clients!) I thought that maybe it was the problem but I have not scrapped that script and found another but im now getting an error

Notice: Undefined variable: title in d:\InetPub\wwwroot\wask\post_news_process.php on line 19

Notice: Undefined variable: news in d:\InetPub\wwwroot\wask\post_news_process.php on line 19

Notice: Undefined variable: author in d:\InetPub\wwwroot\wask\post_news_process.php on line 19

the script is below but I cant find anything wrong with it? Or am I just crap?
<?

include "common.php";

$dbcon = mysql_connect("$server", "$user", "$password");
if (!$dbcon)
die("failed to open database");

if (!mysql_select_db ($db, $dbcon) )

{
die ("couldn't open $db: ".mysql_error() );
}

/* We have now connected, unless you got an error message */

/* Lets post some news ! */
$query = "INSERT INTO news(title, news, author, date) VALUES('$title','$news','$author', now())";

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

{

die (mysql_error());

}

echo ("News item entered.");

?>

Posted: Thu Aug 15, 2002 9:05 am
by twigletmac
If you are getting $title, $news and $author from a form try changing them to $_POST['title'], $_POST['news'] and $_POST['author']. Have a read of this as well:
http://www.devnetwork.net/forums/viewtopic.php?t=511

Try:

Code: Select all

<?php 

include "common.php"; 

$dbcon = mysql_connect($server, $user, $password) or die(mysql_error()); 
mysql_select_db ($db) or die ('couldn''t open '.$db.': '.mysql_error()); 

/* We have now connected, unless you got an error message */ 

/* Lets post some news ! */ 
$query = "INSERT INTO news(title, news, author, date) VALUES('".$_POST&#1111;'title']."', '".$_POST&#1111;'news']."', '".$_POST&#1111;'author']."', now())"; 

mysql_query ($query) or die (mysql_error());

echo 'News item entered.'; 

?>
Hopefully this helps,

Mac

Posted: Thu Aug 15, 2002 10:30 am
by dreamtime
well solved the problem thanks to that other post you sent me. The stupid gets that setup the php had the 4.2 version so the register_globals was set to off.

once again thanks man

aaron