Page 1 of 1

updated problem, still error proned.

Posted: Wed Jun 21, 2006 1:17 am
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); 
}
?>

Posted: Wed Jun 21, 2006 1:18 am
by Robert Plank
You're missing an ending curly bracket.

Posted: Wed Jun 21, 2006 1:19 am
by feyd
count your braces.. and you really need to work on better thread titles.

Thanks Feyd

Posted: Wed Jun 21, 2006 1:27 am
by akimm
Despite your curt and almost rude response you have solved my problem and I thank you very much.

Re: Thanks Feyd

Posted: Wed Jun 21, 2006 1:35 am
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.

Posted: Wed Jun 21, 2006 1:45 am
by Luke
plus this is the third thread you've started to solve one problem

Well sorry for the three threads

Posted: Wed Jun 21, 2006 11:29 am
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.

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

Posted: Wed Jun 21, 2006 11:55 am
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

}	
?>

Posted: Wed Jun 21, 2006 12:47 pm
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
}       
?>

Thanks for the assitance

Posted: Wed Jun 21, 2006 1:17 pm
by akimm
I'm going to try it now.

It still gave an error

Posted: Wed Jun 21, 2006 1:21 pm
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
}        
?>

Posted: Wed Jun 21, 2006 2:12 pm
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).

What could this Parse error mean?

Posted: Thu Jun 22, 2006 5:13 pm
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 
}        
?>

Posted: Thu Jun 22, 2006 5:15 pm
by feyd
missing a semicolon... and missing a quote toward the top..

ok

Posted: Thu Jun 22, 2006 5:49 pm
by akimm
Thanks feyd