Storing webforms data within PHP pages - php

I was reading a book about PHP and I wanted to know how can you store data(of web forms) in PHP pages? I know the process of creating a php file and recieving the data using $_POST(or any other way like this). But those values would be overwritten when you submit the webform again.
Without using the database or emailing the data, is there a way to store multiple webform's data in PHP?

If you want to store submissions from multiple sessions (or clients), you'd have to use some sort of database.
It could be an RDBMS like MySQL, and it could be a simple file: http://www.dummies.com/how-to/content/storing-data-with-php-flat-file-or-database.html

The easiest way is using a database, since MySQL is working very well together with php, often a php server includes MySQL and phpmyadmin which makes it easier to create and edit the database, then you just need a php script you include at the top op your page to set up the MySQL connection.
If this didn't convince you to use a database you could use this code as well:
$filename = "<your filename here>";
$content = "username=".$_POST['username'].":password=".$_POST['password']."{etc etc etc}."\n";
file_put_contents($filename,$content,FILE_APPEND);
The code above adds a row with the content to your file with the delimiter : (colon).
Make sure the filename already exitst somthing like: data.txt

Related

php: How to create a file containing variable definitions

So first I want to say that I'm pretty new to php & javascript. But anyways, I want to make something like a User Interface (1) where you can write sth into certain fields which will create a string for every field and save it into a file on the server so you can get the values the fields should have from other devices.
This is a little graphic to show you what I want to make:
So I'm wondering if anybody knows if it's possible and how to create the file/load the values from it.
I think what you might want is a database, that way one php function can write those values to a db and then another can retrieve those values whenever.
If u r using mysql then ...
Create a php script that passes the parameter to database using mysqli_query function (that is saving data in database) and after that the website script can retrieve data from the database. .

Exporting/Downloading CSV on the fly using PHP/MYSQL

I am wondering what is the best way to tackle the following problem. I need to provide a client with the option to download various ranges of records from a database in CSV format. I was thinking of doing the following.
Provide user with a webform to determine what rows to pull from the DB.
Pull the appropriate records and create a large string from the return data.
Post the string to the script which creates the CSV for downloading.
Before I attempt the above I was wondering if there is a better way to do this?
Thanks ^_^
You can use MySQL's SELECT ... INTO OUTFILE to immediately obtain your results in a CSV. You then need only read the contents of that CSV file back to the user.
This is much safer than attempting to construct a CSV yourself (where you must consider how to properly escape field delimiters, etc); however, if you wanted to do it that way, I'd recommend using fputcsv(STDOUT, $results) to avoid common pitfalls.

Should I be using some sort of database?

In an attempt to redesign a website, I put this page together by hand. However, there are currently over 100 more shows already that need to be added, and three new shows get added every year.
In the old system, a text document was used as a makeshift 'database'; it stored data about each show, with shows separated by '#' and data fields separated with ']'. Here's an excerpt:
#The Whorl of the Leaves]WhorlOfTheLeaves]3]F]167
#Aladdin]Aladdin]8]N]0
#A Christmas Carol]XmasCarol84-]7]N]0
#The Feral Child]FeralChild]7]N]118
#Camelot]Camelot]11]N]169
A PHP script was then used to extract the information about each show and make the page seen here.
I'm sure a script could be used to put together a page such as the one shown above, but it seems like there should be a better system than a text document to store the information.
My question is: if the text document 'database' is working, should I bother changing it? Is there a better way to organize the information about each show?
SQLite would be prefect for you. It's a tiny database that requires no configuration or setup - yet comes built into most PHP installs.
Just use PDO and your good.
$db = new PDO('sqlite:/path/to/here/mydb.sq3');
$db->query('SELECT * FROM shows');
Yep, there are a ton of "better" ways of doing this. But if you're happy with it and it works, why change it?
You could save yourself a lot of trouble by using a content management system such as drupal.

The simplest way to allow a web user to update a text file using PHP and Javascript?

Problem:
I don't know the simplest way to allow a single web viewer to update data in a text file on a server. (ie. only 1 person will be changing the data.)
Objective:
To make a prototype web application just one person needs to input in the start and end dates of new assignments and locations of staff and the whole company can visualize the information on a GANTT chart, probably using this Jquery libary.
Constraints:
My data is about the equivalent size of 1000 of these javascript list of lists like
*data = [["John Smith" , "assigment" , "1/1/10", "1/1/11", "Peru"],[...],...]*
Employee assignment data must be on an internal server.
I can't use a database (such as SQlite or MySQL).
I can only use PHP, Javascript, and jQuery.
Fact: Javascript cant directly change a data file sitting on the server.
My tentative fuzzy solution:
On client-side: use jQuery getJSON() to pass the data back and forth between dataReadWriter.php.
On server-side: dataReadWriter.php modifies a PHP array as well as writes modified data and reads JSONdata.txt stored in a text file on our internal server.
Given the constraints, it can't be done a lot smarter than what you are suggesting. One thing though, you shouldn't overwrite the only file containing the data, at least switch back and forth between two files, and make sure that your program does not overwrite the other file if one of the files show any signs of being damaged. You can use a PHP session to keep track of which file is the most recent, but better have some in-file timestamps as a fallback.
Is there anything in particular that you worry about?

HTML Table row edit/Delete

I have an HTML table with contents, I would like to have an feature of Edit/Delete to that table. How do I do it with PHP?
I actually think that this sounds more like a job for JavaScript, which can edit/remove rows on-the-fly and with much less code. (Implement some AJAX too, and you can edit/remove rows in database too).
But if you insist on using PHP, you might just want to add some GET parameters to the Edit/Delete links that would delete or edit those rows.
Well, there is a pure PHP way to do it, and then there is a combination of Javascript and PHP. You must use PHP one way or another if you want your changes to the database to be permanent as that is your gateway to communicating with the database (as far as I know you cannot do that with Javascript as that is client-based and runs entirely in your web browser).
If using just PHP, you must generate HTML documents for each change. E.g., you click on one cell in the table and that gets you to a new HTML page where the field is editable through an input element; or you can list all fields at once for that row and edit them all at the same time. The fields are then posted in a form to a PHP page which will take the new values and update the database (or insert new values or however you wish it to behave). Here's a tutorial for how to do this:
http://www.freewebmasterhelp.com/tutorials/phpmysql/1
You can also mix in some Javascript which allows a more interactive interface to modifying the values in a cell. However, this obviously requires more code and may be overkill for what you're trying to do. Nonetheless, here is a link which demonstrates just that and also shows the code:
http://www.java2s.com/Code/JavaScript/GUI-Components/Editabletablecell.htm
Hope this is what you're looking for.
EDIT:
Forgot that you also wished to delete content in the table. That is also explained in the first link.
If you intend to work with databases, and it seems like you have little understanding of how they work, pick up a good book like: SQL - The Complete Reference. When you have enough knowledge of SQL, look at PHP's PDO extension: http://php.net/manual/en/book.pdo.php

Categories