Jump to content

Recommended Posts

Hi all

I have a txt file with this values:

 

parameter1

parameter two

third value

others values...

 

From a form, I would edit the parameters above:


<form method="POST" action="edit.php">
<?php

$rows = file("file.txt");


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

echo "<input type=text name=$rows[$i] value=$rows[$i]><br />";

}

?>

<input type="submit" value="Edit" name="submit"></p>

</form>

<?php

if (isset($_POST['submit'])) {


$upd= $_POST[$rows[$i]];


if(!($fp = fopen("file.txt","r+"))) {

   echo "Error";

  } else {

   fwrite($fp, $upd);

   fclose($fp);

}

}

?>

 

I use this but doesn't works.

Where it is wrong?

Link to comment
https://forums.phpfreaks.com/topic/63588-update-a-row-in-txt-file/
Share on other sites

Im guessing by "doesn't work" you mean that when you try and edit the file, it doesn't edit successfully. Try:

 

<form method="POST" action="edit.php">
<?php

$rows = file("file.txt");


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

echo "<input type='text' name='lines[$i]' value='$rows[$i]'><br />";

}

?>

<input type="submit" value="Edit" name="submit"></p>

</form>

<?php

if (isset($_POST['submit'])) {

$towrite = implode("\n",$_POST['lines']);

if(!($fp = fopen("file.txt","r+"))) {

   echo "Error";

  } else {

   fwrite($fp, $towrite);

   fclose($fp);

}

}

?>

It's untested, but give it a go.

 

Notice the way that i've named the text fields as an array. Without doing this, you would have to open the file again, and extract each field name - since the name of the fields was what was on each line of the text file.


<form method="POST" action="edit.php">
<?php

$rows = file("file.txt");


foreach ($rows as $rows_num => $rows) {
   echo "<input type=text name=$rows_num value=$rows><br />";
}

?>

<input type="submit" value="Edit" name="submit"></p>

</form>

<?php

if (isset($_POST['submit'])) {


$upd= $_POST[$rows];


if(!($fp = fopen("file.txt","r+"))) {

   echo "Error";

  } else {

   fwrite($fp, $upd);

   fclose($fp);

}

}

?>

 

Im guessing by "doesn't work" you mean that when you try and edit the file, it doesn't edit successfully. Try:

 

<form method="POST" action="edit.php">
<?php
$rows = file("file.txt");

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

echo "<input type='text' name='lines[$i]' value='$rows[$i]'><br />";
}

?>
<input type="submit" value="Edit" name="submit"></p>
</form>

<?php
if (isset($_POST['submit'])) {
$towrite = implode("\n",$_POST['lines']);

if(!($fp = fopen("file.txt","r+"))) {

   echo "Error";

  } else {

   fwrite($fp, $towrite);

   fclose($fp);

}

}

?>

It's untested, but give it a go.

Notice the way that i've named the text fields as an array. Without doing this, you would have to open the file again, and extract each field name - since the name of the fields was what was on each line of the text file.

 

This works but a strange thing happens:

try to edit, from input text, the second value "parameter two" to a thing as "parameter two onetwothreefourfive"

Then click submit and after edit the value above to original "parameter two".

Will be created another row in txt file!!

 

Why?

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.