Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda