/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] View/Write/Delete To File

Ok this what I am trying to do. I have looked but can’t find a good tutorial on how to do this. Here it is.

I am building a web page that the user can access to update portions of their site. One particular spot is a scrolling text box that displays lines from a javascript file which the user does not edit the actual javascript, just the line that they want displayed. I have it set up so that the user can write to this file, but I also want the user to be able to delete or edit a line in the .js file. Basically this is how I want it set up:

The user enters the data and submits it. When they go to a dedicated page they can see the line they just wrote along with the other lines that have already been written. Each one should display ‘delete | edit’.

Is there a way to do this?

Thanks

to post a comment
PHP

21 Comments(s)

Copy linkTweet thisAlerts:
@temp_user123Jun 29.2007 — Sure, just not a real easy one. I've done this with CSV files. The .js file is a flat file -- thus, you cannot just insert or delete lines in the middle of it. Two ways -- all based on sequential line number (or a line tag which is part of the data): Read it all into memory and write it all out -- including or minus the inserted, updated, or deleted lines. Or, read a line at a time from one file and write a line at a time to a new file -- including or minus the inserted, updated, or deleted lines -- then delete the old file and rename the new file back to the old file name.
Copy linkTweet thisAlerts:
@monarch_684authorJun 30.2007 — English please
Copy linkTweet thisAlerts:
@SheldonJun 30.2007 — You could do this easily with php and a mysql database.
Copy linkTweet thisAlerts:
@bluestarsJun 30.2007 — That was pretty simple, actually. You could read the file (to an array) a line at a time until you found the line before or after which you wanted to add another line. Then, you'd add it to the array and continue reading. Once you reached the end of the file, you could pump it all back out to a different file, then delete the original and rename the new one to the same name.

Why do you want to do things with Javascript, anyway?
Copy linkTweet thisAlerts:
@MrCoderJun 30.2007 — Just read the whole file in to a multiline <textarea> then save the edited version to the file, newlines and all.

That way you can insert lines anywhere you want by just pressing enter?
Copy linkTweet thisAlerts:
@monarch_684authorJul 02.2007 — Ok so I been reading and since everybody is saying I am going to use mysql. So now this question goes away from the original:

When ever I write to my table it shows up as blank data. Here's my script can some help me find the problem.

[code=php]
<?php
$username = "username";
$password = "password";
$database = "test";

$line = "10";
$information = $_POST['description'];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO info_scroller VALUES ('$_POST[$line]', '$_POST[$information]')";
if (mysql_query($query)) {
print "successfully inserted record";
}
else {
print "Failed to insert record";
}
mysql_close();
?>
[/code]
Copy linkTweet thisAlerts:
@temp_user123Jul 02.2007 — Seems you want to change this:
[code=php]
$query = "INSERT INTO info_scroller VALUES ('$_POST[$line]', '$_POST[$information]')"; [/code]

to this:
[code=php]
$query = "INSERT INTO info_scroller VALUES ('" . $_POST['line'] . "', '" . $_POST['information'] . "')";[/code]

However, for protection, you really want to scrub your form data before sticking it straight into an SQL query like that.
Copy linkTweet thisAlerts:
@monarch_684authorJul 02.2007 — What do you mean??

and

[code=php]
$query = "INSERT INTO info_scroller VALUES ('" . $_POST['line'] . "', '" . $_POST['information'] . "')";
[/code]


didn't work.
Copy linkTweet thisAlerts:
@temp_user123Jul 02.2007 — What do you mean??

and

[code=php]
$query = "INSERT INTO info_scroller VALUES ('" . $_POST['line'] . "', '" . $_POST['information'] . "')";
[/code]


didn't work.[/QUOTE]

OK, based on this you posted:
[code=php]
$line = "10";
$information = $_POST['description'];[/code]

You would use this:
[code=php]
$query = "INSERT INTO info_scroller VALUES ('" . $line . "', '" . $information . "');";[/code]

or this:
[code=php]
$query = "INSERT INTO info_scroller VALUES ('{$line}', '{$information});";[/code]
Copy linkTweet thisAlerts:
@monarch_684authorJul 02.2007 — Ok still not working. I even went and got my book out of the car and did it's tutorial except it has me adding the values directly in :
[code=php]
<?php

$connection = mysql_connect("localhost", "username", "password");
@mysql_select_db("produce", $connection) or die( "Unable to select database" . mysql_error());

$query = "INSERT INTO fruit (name, number) VALUES('apricots', '203')";
$result = mysql_query($query)
or die ("Couldn't query data" . mysql_error());

$query = "SELECT * FROM fruit";
$result = mysql_query($query)
or die ("Couldn't query data" . mysql_error());

echo "<table border='1'>";
echo "<tr>";
echo "<th>Name</th><th>Number</th>";
echo "</tr>";

while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>", $row['name'], "</td><td>", $row['number'], "</td>";
echo "</tr>";
}

echo "</table>";

mysql_close();
?>
[/code]

I am wanting the values to be user selected using a form. I add this to the script:
[code=php]
$name1 = $_POST["fruit"];
$number1 = $_POST["fnumber"];
.
.
.
$query = "INSERT INTO fruit (name, number) VALUES('$name1', '$fnumber1')";
[/code]


This you would think is the same thing but the first example adds it ok, the second adds a field but nothing there. Even adding the double quotes does not work.
Copy linkTweet thisAlerts:
@temp_user123Jul 02.2007 — What does the HTML look like for these fields?
Copy linkTweet thisAlerts:
@monarch_684authorJul 02.2007 — [code=html]
<form method="post" action="rewrite.php">
</p><input type="text" name="name1"> <input type="text" name="fnumber1"></p>
<p><input type="submit" value="Submit"><input type="reset" value="Reset" /></p>
</form>
[/code]
Copy linkTweet thisAlerts:
@temp_user123Jul 02.2007 — Then this should work -- other than the lack of scrubbing:
[code=php]
$name1 = $_POST["fruit"];
$number1 = $_POST["fnumber"];
.
.
.
$query = "INSERT INTO fruit (name, number) VALUES('{$name1}', '{$fnumber1}');";[/code]
Copy linkTweet thisAlerts:
@monarch_684authorJul 02.2007 — Still no go. It just keeps on putting in blank data. It makes a new row and I can see the new row but no data.
Copy linkTweet thisAlerts:
@temp_user123Jul 02.2007 — How did you define the table? Also, do this and post the result:
[code=php]
$query = "INSERT INTO fruit (name, number) VALUES('{$name1}', '{$fnumber1}');";
echo 'query="' . $query . '"<br>' . "n";[/code]
Copy linkTweet thisAlerts:
@monarch_684authorJul 02.2007 — Here is what is echo:

query="INSERT INTO fruit (name, number) VALUES('', '');"[/QUOTE]

And from what I see my information is not being passed from the html to the php. Odd.
Copy linkTweet thisAlerts:
@monarch_684authorJul 02.2007 — Ok ignore that last post I don't know what was done but it worked
Copy linkTweet thisAlerts:
@temp_user123Jul 02.2007 — Well, I guess all's well that ends well.
Copy linkTweet thisAlerts:
@MrCoderJul 02.2007 — What is the book called?

You should burn it lol!
Copy linkTweet thisAlerts:
@monarch_684authorJul 03.2007 — Spring into PHP 5

It has helped me this far.
×

Success!

Help @monarch_684 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...