Theekopje Theekopje - 6 months ago 9
SQL Question

Replace / write code in file by Query

Maybe a stupid question but it is possible to write a form sort of thing that you can fill in which either writes or replaces the code in the file?

For example I have a result table with possible results. Instead of using either a database or replace the code in my file manually I'd like to write a form which changes the code for me when I fill it in. Sort of like a database but then just in the file itself.

Is this possible?

Wit kind regards

Answer

Simple answer: Yes, it is possible, but not recommended.

Elaborating the answer: The reason it is not recommended is because you are opening your doors to hackers that could use XSS (Cross Site Scripting) attacks, unmasking your site, or many other possibilities.

If however you are just curious on how you would modify the code from a form, you can do it as follows.

$new_code = $_POST['newcode'];
$myFile = fopen('table.html', 'w');
fwrite($myFile, $new_code);
fclose($myFile);

A file called table.html would then be created in the same location as the page that is running the previous code. If you want to place the file in another page, you could just add a relative or absolute path to the name, for instance:

$myFile = fopen('../folderA/table.html', 'w');

$_POST is a php superglobal. It is very often used in forms, and if you aren't sure how to use it, there are many great tutorials online.

If you wanted a php file, the code would be the same, except you would change the name of the file from table.html to table.php.

Let me know if that helped!