Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
PDO Insert uploading a textarea into my DB

I have been trying for months to figure out how to fix and create what I am envisioning which I know is possible to be done and probably is not hard to do.

I am trying to take a textarea that I have placed on a page of mine upload its contents into a database where people can view the information they uploaded. Here's an example.

Person A copy/pastes text into a text area at: http://example.com/textarea/ he clicks an upload/submit button and gets a link like this: http://example.com/A93KJUQ21.txt Anyone that has access to that link will be able to click it and it will display the contents that were uploaded to it. Whatever Person A, B, C, D, etc uploads it will generate a new unique link to the information. Example of this would be as follows:

http ://example.com/A93KJUQ21.txt

http ://example.com/JKO2QN498.txt

http ://example.com/PMNR01NEQ.txt

and so on..

Here is the code I currently have

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['upload'])) {
$textarea = $_POST['paste-area'];

//Add validations

$odb = new PDO("mysql:dbname=dbname;host=localhost", "dbusername", 
"mypasswordgoeshere");

$query = $odb->prepare("INSERT INTO submission (`textarea`) VALUES 
(:textarea)"); //I'm just making up the structures

$query->bindParam(':textarea', $textarea, PDO::PARAM_STR);

$status = $query->execute(); //$status contains true or false

//Other codes...
}
}

?>
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Your table needs two columns, the random string and the textarea contents. When the user submits the form, you need to create the random string and insert that into the DB along with the text area.

$string = uniqid();
$query = $db->prepare("INSERT INTO submission (id, textarea) VALUES (:string, :textarea)");
$query->bindParam(':string', $string);
$query->bindParam(':textarea', $textarea);
$query->execute();
echo "Link is <a href='http://example.com/lookup.php?id=$string'>href='http://example.com/lookup.php?id=$string</a>";

I've made the link point to a PHP script. You can't retrieve from the database using a .txt URL, that just tries to download a regular file. You need to point to a PHP script that fetches the textarea from the submission table.

If you want to make it seem like a .txt file, you could use a rewrite rule on the server, that rewrites the .txt URL to the equivalent .php URL.

over 4 years ago · Santiago Trujillo Report

0

why do you think you need a database for this?

<?php
if (!empty($_POST['text'])) {
    $filename = uniqid().".txt";
    file_put_contents($filename, $_POST['text']);
    die("http://".$_SERVER['HTTP_HOST']."/$filename");
}
?>
<form method=post>
<textarea name=text></textarea>
<input type=submit>
</form>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!