Can anyone help with this script

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
spudmclard
Forum Newbie
Posts: 16
Joined: Thu Oct 27, 2005 3:06 am

Can anyone help with this script

Post by spudmclard »

Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Im trying to pass data entered into a form to a file 'file2.txt' and append that data in 'form1'.

[b]CODE FOLLOWS[/b]

Code: Select all

<?php
$name=$_POST['data'];
$file_name = "file2.txt"; 
$fp = fopen($file_name, "a");
 //write to the file
fwrite($fp, "$name");
fclose($fp);
$file_name2 = "file1.txt";
$fp2 = fopen($file_name2, "a");
$name = str_replace("$name","***");
?>
So far it succesfully writes to 'file2.txt' but it doesnt append the entry in 'file1.txt'
Any help would be greatly appreciated cos ive hit a massive brick wall with another one waiting close behind it :(


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Can anyone help with this script
short answer: 'no'

long answer:

you arn't writing to the 'file1.txt' so of course its not adding in anything. Is that what you wanted?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Yup, as shiznatix has stated, you have simply failed to use fwrite() in the second instance
spudmclard
Forum Newbie
Posts: 16
Joined: Thu Oct 27, 2005 3:06 am

Post by spudmclard »

Ah

Just proves how stupid i can be......

ive added the following to the file but still no joy.

Code: Select all

$namec = str_replace("$name","***");
fwrite($fp2, "$namec");
As you can guess im struggling here, any help would be appreciated.
sheila
Forum Commoner
Posts: 98
Joined: Mon Sep 05, 2005 9:52 pm
Location: Texas

Post by sheila »

str_replace takes 3 arguments.
Also you don't need quotes around the variable names in your argument list, and watch the order of the arguments

Code: Select all

$namec = str_replace($search, $replace, $name);
$name is the string to be changed
$search is what we want to find
$replace is the new value to be substituted when $search is found

Have a look at the manual http://us2.php.net/str_replace for more details.
Post Reply