PDA

View Full Version : Message Box



VictorProfundus
12-15-2007, 05:07 AM
Okay...what I am looking for exactly is for some sort of code that would allow a form to load a value already stored on a database of some type, for instance, a text file. Then, through the form a user could edit what is entered in the box (which would be the value previously loaded). When the user updates it, it would then edit the database.

I am not sure if this is possible through Text Files or not. I have access to SQL but, I am not as experienced with them.

I am just not sure how to call in the value of the text box from a file.

Does anyone know if this is possible?

Edit:
[I would prefer to do this through php...but, if that is not possible, could someone suggest what coding I should use?]

smithster
12-16-2007, 12:01 AM
mysql_connect(localhost,username,password);
mysql_select_db(dbname);

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

$text = mysql_real_escape_string($_POST['textdoc]);

$query = ("UPDATE table set something = '$text' WHERE something = 'something'");
$result = mysql_query($query);

echo 'Database updated';
}
else
{
$query = ("SELECT * FROM table WHERE something = 'something'");
$result = mysql_query($query);
while ($row = mysql_query($result)){
echo '<form method = "POST" action = "">';
echo '<p><textarea rows="10" name="textdoc" cols="37" value=$row['textdoc']></textarea></p>';
echo '<p><input type="submit" value="Submit" name="submit"></p>';
echo '</form>';
}
}


Is this the sort of thing you mean? Basically, this form has a text box, which will already have something written in it - whatever is already stored in the database. When the submit button is pressed, the data in the database is then updated with whatever is in the textbox.

Hope this helps.

Smithster.

VictorProfundus
12-16-2007, 04:58 AM
Yes....This is exactly what I want!!! Thank you soo much...

Okay...My SQL Databases are not working right now...I have way to much junk in them anyway...

Is there anyway to do this based off of Text File databases?

Twey
12-16-2007, 02:29 PM
You don't want to. It'd be much easier with MySQL. You could use SQLite, which is a database stored in a single file.

smithster
12-16-2007, 06:51 PM
Although I agree with Twey, MySQL is a much safer and a better way to go, but here is a script I've put together for you.


<?php

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

$path = 'path_to_file/file.txt';
$content = $_POST['content'];

if(file_exists($path)) {
$file = fopen("$path", "w");
fwrite($file, '' . $_POST['content'] . '');
fclose($file);

echo 'Your file has been updated.<br>';
}
if(!file_exists($path)) {
echo 'Unable to update';
}
}
else
{
$filename = 'file.txt';
?>
<form method=POST action="">
<textarea name=content cols=56 rows=20>
<?php
$fd=fopen($filename,"rb");
$cts=fread($fd,filesize($filename));
fclose($fd);
/*prepare file*/
$cts=str_replace(chr(92).chr(34),chr(34),$cts);
$cts=str_replace(chr(92).chr(39),chr(39),$cts);
$cts=str_replace(chr(92).chr(92),chr(92),$cts);
echo htmlspecialchars($cts);
?>
</textarea>
<input type=submit name=submit class=combtn value='Save'>
</form>
<?php
}
?>


Hope this is what you are after.

Smithster.

VictorProfundus
12-16-2007, 11:45 PM
Yes, this is exactly what I am after.

The text file method...you may not agree with, but, it is an essential part of another script that I am working on for a forum.

Thank you all so very much!

VictorProfundus
12-19-2007, 06:40 AM
Okay...I am sorry to be so nagging...but I require assitance once again.

Is there anyway that you can tweak the above code to have a form in which the user would input the name of the file, and the path. Then the code would input the value into the code...so that it would display and edit the file entered...I realize this might need another page.

I have tried this already, but my php skills are a little rusty, and I have an error somewhere.

Thanks in advance!

smithster
12-20-2007, 08:02 AM
It works, you might want to have a look at tidying it up a little though. I'm just off to work so only had 10 mins to put this together. It does work though as I've tested it.



<?php
session_start();

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

$_SESSION['path'] = $_POST['path'];
$filename = $_POST['filename'];

echo '<form method=POST action="">';
echo '<textarea name=content cols=56 rows=20>';
$fd=fopen($filename,"rb");
$cts=fread($fd,filesize($filename));
fclose($fd);
/*prepare file*/
$cts=str_replace(chr(92).chr(34),chr(34),$cts);
$cts=str_replace(chr(92).chr(39),chr(39),$cts);
$cts=str_replace(chr(92).chr(92),chr(92),$cts);
echo htmlspecialchars($cts);
echo '</textarea>';
echo '<input type=submit name=save class=combtn value="Save">';
echo '</form>';
}
if(isset($_POST['save'])){

$content = $_POST['content'];

if(file_exists($path)) {
$file = fopen("$path", "w");
fwrite($file, '' . $_POST['content'] . '');
fclose($file);

echo 'Your file has been updated.<br>';
}
if(!file_exists($path)) {
echo 'content - '.$content.'<br>';
echo 'path - '.$path.'<br>';
echo 'Unable to update';
}
}
else
{
echo '<form method=POST action="">';
echo 'Filename - <input type="text" name="filename" value=""><br />';
echo 'Path To File - <input type="text" name="path" value=""><br />';
echo '<input type=submit name=last value="next">';
echo '</form>';
}
?>