updated problem, still error proned.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

updated problem, still error proned.

Post by akimm »

This is my code thus far, with an error apparently at the very last line, I have no idea why this is an error.


Parse error: parse error, unexpected $ in /nfs/cust/8/25/05/650528/web/add_art.php on line 45

Code: Select all

<?php
if ($_POST['name'] = "" && 
$_POST['email'] = "" && 
$_POST['article'] ="") 
{ 
echo "Please fill out all form fields"; 
} 
// recognize what i want from user 
 $user = $_POST['name'] . $_POST['email'] . $_POST['article']; 
// start an iteration that will add 1,2,3,4,5,6 and so on to each article added  
for ($i=0;$i<count($i);$i++){ 
if ($user)  { 
$fileName = "/article/article1.txt"; 
// This can be anything (including file names generated with a loop like
 //the one above)  
$dude = fopen($fileName, 'w') or die("can't open file"); 
 // open the file or kill the script and show error 
$numBytes = fwrite($dude, $_POST['name'] . $_POST['email'] . $_POST['article']); 
// Write to the file with fwrite now 
$ext = '.txt';
fwrite(article/article . $i . txt); 
// Close the file handle 
fclose($dude); 
}
?>
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

You're missing an ending curly bracket.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

count your braces.. and you really need to work on better thread titles.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Thanks Feyd

Post by akimm »

Despite your curt and almost rude response you have solved my problem and I thank you very much.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: Thanks Feyd

Post by feyd »

akimm wrote:Despite your curt and almost rude response you have solved my problem and I thank you very much.
The fact that it's a forum rule (see below) and that I've held off saying anything about it thus far doesn't say anything now.

I think I'm being quite nice.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

plus this is the third thread you've started to solve one problem
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Well sorry for the three threads

Post by akimm »

I asked Feyd to delete 3 of them, or at least 2 so I could start a new and try to get my problem solved, with each thread people have helped, but not in the way that I needed, I have remodified the code and I'm hoping someone can catch my error and then it should perform exactly as I require.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Here is my updated code, if anyone is kind enough to help.

Post by akimm »

This is the code that I have rewrote, with the rewrite I am pulling up this error

Warning: fopen(article/article0): failed to open stream: No such file or directory in /nfs/cust/8/25/05/650528/web/add_art.php on line 41

Warning: fwrite(): supplied argument is not a valid stream resource in /nfs/cust/8/25/05/650528/web/add_art.php on line 42

Warning: fclose(): supplied argument is not a valid stream resource in /nfs/cust/8/25/05/650528/web/add_art.php on line 43
You need to fill out all form fields

What my objective is, is to allow users of my site the ability to add articles without emailing them to me, and then my having to add them. So I want fopen to open a new file with article $i .txt, presuming I have 3 articles I expect there to be in my /article/ directory article1.txt, article2.txt, and article3.txt

What am I doing wrong?

Code: Select all

<?php
if ($_POST['name'] = "" && 
$_POST['email'] = "" && 
$_POST['article'] ="") 
{ 
echo "Please fill out all form fields"; 
} 
// recognize what i want from user 
 //variable to concocatanate to append user info
$user = $_POST['name'] . $_POST['email'] . $_POST['article']; 
// start an iteration that will add 1,2,3,4,5,6 and so on to each article 
//added 

for ($i=0;$i<count($i);$i++){ 

$fileName = "article/article". $i . $ext; 
}
$ext = '.txt';
$fp = fopen($fileName, 'a');
$fw = fwrite($fileName, 'w');
$fc = fclose($fileName);
if (!$user)  {
  echo "You need to fill out all form fields";
}	else	{
  $fp . $fw . $fc

}	
?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

What am I doing wrong?
Almost everything. Not trying to confuse you, though.
  • = stands for assignment, not an equality test. $_POST['name'] = '' will effectively erase the value sent by user's browser. In php to test two expressions for equality you would use == operator, like this:

    Code: Select all

    if($_POST['user'] == '') {
      // do something
    }
  • Working with files. Here's simple example:

    Code: Select all

    $fp = fopen('/directory/filename.ext', 'w'); // open file. $fp variable now holds a handle to the file
    fwrite($fp, 'text content'); // write to the file. First argument must be the handle opened with fopen
    fclose($fp); // close file handle
    As you can see, every file function in this example accepts a file handle as it's first argument. What you have tried is to pass string as a first agument to fwrite. No wonder you had failed.
  • count() works with arrays, $i variable in your script is of type integer.
  • And last, but not the least. Remember, PHP execute your script line-by-line. You need to account for that. For example, you tried to use $ext variable before its definition.
And one more tip: indent your code to make the program flow obvious. Here I done that for you (without touching the structure of you program):

Code: Select all

<?php
if ($_POST['name'] = "" && $_POST['email'] = "" && $_POST['article'] ="") {
   echo "Please fill out all form fields";
}

// recognize what i want from user
// variable to concocatanate to append user info
$user = $_POST['name'] . $_POST['email'] . $_POST['article'];

// start an iteration that will add 1,2,3,4,5,6 and so on to each article
// added

for ($i=0; $i<count($i); $i++) {
   $fileName = "article/article". $i . $ext;
}

$ext = '.txt';
$fp = fopen($fileName, 'a');
$fw = fwrite($fileName, 'w');
$fc = fclose($fileName);

if (!$user) {
   echo "You need to fill out all form fields";
} else {
   $fp . $fw . $fc
}       
?>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Thanks for the assitance

Post by akimm »

I'm going to try it now.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

It still gave an error

Post by akimm »

Parse error: parse error, unexpected '}' in /nfs/cust/8/25/05/650528/web/add_art.php on line 47

which it has been giving me consistently when I think my program has been fully debugged. I've rewritten this same thing 3-4 times, hence the reason this last rewrite was so error fraught, I made careless mistakes. Thanks very much for the help, I am honestly stumped as to why this still gives me an error.

Code: Select all

<?php 
if ($_POST['name'] == "" && $_POST['email'] == "" && $_POST['article'] == "") { 
   echo "Please fill out all form fields"; 
} 

// recognize what i want from user 
// variable to concocatanate to append user info 
$user = $_POST['name'] . $_POST['email'] . $_POST['article']; 

// start an iteration that will add 1,2,3,4,5,6 and so on to each article 
// added 
$ext = '.txt'; 
for ($i=0; $i<count($i); $i++) { 
   $fileName = "article/article". $i . $ext; 
} 
$fp = fopen($fileName, $user', 'a'); 
$fw = fwrite($fileName, $user, 'w'); 
$fc = fclose($fileName); 

if (!$user) { 
   echo "You need to fill out all form fields"; 
} else { 
   $fp . $fw . $fc 
// THE ERROR IS HERE
}        
?>
Last edited by akimm on Wed Jun 21, 2006 2:33 pm, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Please re-read the check-list I posted above. Every item in it points to specific error you made. And you've fixed only one of them (specifically, using undefined variable).
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

What could this Parse error mean?

Post by akimm »

Parse error: parse error, unexpected '}' in /nfs/cust/8/25/05/650528/web/add_art.php on line 47

here is the php the error is on the last bracer.

Code: Select all

<?php 
echo "<html>\n"; 
echo"<head>\n"; 
?>
<STYLE TYPE='text/css'>"
@import url(style.css);\n"; 
</STYLE>
<body>
<?php 
$date = date('l n/d/y g:i a'); 
echo $date; 
?> 
<form action='<?php $_SERVER['PHP_SELF'] ?> method='POST'>
<p><b>Your name</b> 
<input type='text' name='name' size='30'></p>
<p><b>Your email</b>
<input type='text' name='email' size='30'></p>
<p><b>article</b>
<TEXTAREA NAME='article' COLS='120' ROWS='20' WRAP='virtual'></TEXTAREA></P> 
<p><input type='submit' name='submit' value='submit your aritlce'></p> 

<?php 
if ($_POST['name'] == "" && $_POST['email'] == "" && $_POST['article'] == "") { 
   echo "Please fill out all form fields"; 
} 

// recognize what i want from user 
// variable to concocatanate to append user info 
$user = $_POST['name'] . $_POST['email'] . $_POST['article']; 

// start an iteration that will add 1,2,3,4,5,6 and so on to each article 
// added 
$ext = '.txt'; 
for ($i=0; $i<count($i); $i++) { 
   $fileName = "article/article". $i . $ext; 
} 
$fp = fopen($fileName . $user . 'a'); 
$fw = fwrite($fileName, $user, 'w'); 
$fc = fclose($fileName); 

if (!$user) { 
   echo "You need to fill out all form fields"; 
} else { 
   $fp . $fw . $fc 
// THE ERROR IS HERE 
}        
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

missing a semicolon... and missing a quote toward the top..
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

ok

Post by akimm »

Thanks feyd
Post Reply