PDA

View Full Version : [PHP] Store data sent through form



EpidemiK
April 22nd, 2003, 12:48
I am a complete newbie to PHP. Currently, I'm reading the PHP documentation from php.net. I need a simple script that will save the data from a textarea and still be able to retrieve it so I can view it later.

For example:
form.php

Shows the textarea, processes the form, and saves it into a text file.

When the user submits the data successfully, I want the script to generate an increasing number so that if I go to form.php?file=1 it will display the data.

If I do this again, the data will be stored as form.php?file=2

This may be a little complicated, but I know it could be done. Thanks for helping me. :)

CareBear
April 22nd, 2003, 17:51
using MySQL or just a file? :confused:

EpidemiK
April 22nd, 2003, 18:28
Originally posted by CareBear
using MySQL or just a file? :confused:
Either MySQL, 1 file, or many files. It doesn't matter. :p

CareBear
April 23rd, 2003, 04:57
I was hoping someone else would have answered this by the time I woke up :D

I'm going to assume you use PHPMyAdmin to administer your mysql databases. Create a new table (I called it textentries) with the following layout:

EntryID (Type: MEDIUMINT ; Length: 5 ; Attributes: UNSIGNED ; Null: not null ; Extra: auto_increment ; Primary)
Data (Type: text ; Null: not null) - The TEXT type will allow up to 64Kb of text data entry, if you need larger then use mediumtext which allows up to 16Mb

The code for the form.php page is:

<html>
<head>
<title>Textarea post</title>
</head>
<body>
<?php
// Address to the MySQL database server
$dbServer = "localhost";
// MySQL username
$dbUser = "your-username";
// MySQL username's password
$dbPassword = "your-password";
// MySQL database name
$dbName = "test";

// Check to see if data was posted and that it's at least one character long
if ( ($_POST["submit"] == "Post") && (strlen($_POST["textentry"]) > 0) ) {
$Data = $_POST["textentry"];
if (!get_magic_quotes_gpc())
$Data = addslashes($Data);

// Connect to the MySQL server
$LinkID = mysql_connect($dbServer, $dbUser, $dbPassword);
// Select to the database
mysql_select_db($dbName, $LinkID);
// Do the query
mysql_query("INSERT INTO textentries (Data) VALUES('".$Data."')", $LinkID);
echo "The data was added to the database.";
} else {
$iEntryID = $_GET["entry"];
// Make sure the entry ID is numeric
if (is_numeric($iEntryID)) {
// Connect to the MySQL server
$LinkID = mysql_connect($dbServer, $dbUser, $dbPassword);
// Select to the database
mysql_select_db($dbName, $LinkID);
// Do the query
$Result = mysql_query("SELECT Data FROM textentries WHERE EntryID = ".$iEntryID);
// Output the result
if (mysql_num_rows($Result) == 0)
echo "No data found.";
else {
$Row = mysql_fetch_assoc($Result);
echo "<pre>".$Row["Data"]."</pre>";
}
} else
ShowForm();
}

// This function will output the form
function ShowForm() {
?>
<form action='<?=$_SERVER['PHP_SELF'];?>' method='post'>
Type some text here:<br> <textarea cols='40' rows='10' name='textentry'></textarea>
<br><input type="submit" name="submit" value="Post">
</form>
<?php
}
?>
</body>
</html>I added comment where I thought it was necessary. It's not really a cut and paste script since you said you just needed it for educational purposes :).
It has absolutely no error handling. If something is wrong the MySQL server, database or table they'll see an "ugly" PHP error.

When you copy the code use the quote button below my post since vBulletin seems to be in the habit of "eating" certain characters. Hope it helps you some :)

EpidemiK
April 23rd, 2003, 19:01
Thanks a lot CareBear. :D

I will try it later because I'm grounded and I'm not even supposed to be on the computer today. :mad:

EpidemiK
April 25th, 2003, 09:26
Ok I have just tested it .. how do I retrieve what I had pasted into the textarea? :confused:

Thanks for all your help. :)

CareBear
April 26th, 2003, 13:38
Originally posted by EpidemiK
Ok I have just tested it .. how do I retrieve what I had pasted into the textarea? :confused:http://yourdomain/whatever-you-called-the-script.php?entry=5

it's this part of the code:



$iEntryID = $_GET["entry"];
// Make sure the entry ID is numeric
if (is_numeric($iEntryID)) {
// Connect to the MySQL server
$LinkID = mysql_connect($dbServer, $dbUser, $dbPassword);
// Select to the database
mysql_select_db($dbName, $LinkID);
// Do the query
$Result = mysql_query("SELECT Data FROM textentries WHERE EntryID = ".$iEntryID);
// Output the result
if (mysql_num_rows($Result) == 0)
echo "No data found.";
else {
$Row = mysql_fetch_assoc($Result);
echo "<pre>".$Row["Data"]."</pre>";
}
}

EpidemiK
April 26th, 2003, 17:28
Hmm .. I keep getting the No data found error. I'll try it on another host.

CareBear
April 26th, 2003, 18:28
Originally posted by EpidemiK
Hmm .. I keep getting the No data found error. I'll try it on another host. have you checked in PHPMyAdmin if there is actually data in the table after you post something from the form?
I didn't include error checking so something may have gone wrong.