How to register the value of an input="text" in the database?

Asked

Viewed 32 times

-2

How can I include the value of the name input identificador to be saved in the database, with the code below:


<?php
require_once "conexao.class.php";
$conexao = new PDO('mysql:host=localhost;dbname=informacoes;charset=utf8', 'root', '');
$msg = false;
if(isset($_FILES['arquivo']))
{
$extensao = strtolower(substr($_FILES['arquivo']['name'],-4));
$novo_nome = md5(time()) . $extensao;


$diretorio = "upload/";

move_uploaded_file($_FILES['arquivo']['tmp_name'], $diretorio.$novo_nome);

$query = $conexao->prepare("INSERT INTO arquivo (codigo, arquivo, identificador) VALUES (null, '$novo_nome' );");

$query->execute();

if ($query->execute()) $msg = "Imagem enviada com sucesso!!!";
else $msg = "Falha ao enviar a imagem.";

}

?>

<body bgcolor="#008080">
    
</br>
</br>
</br>
</br>
</br>
</br>
</br>
<div style='background:white;'><center><h1><font face="CALIBRI">------UPLOAD DE IMAGEM ------</font></h1></center>

</br>   


<center><?php if ($msg != false) echo "<p> $msg </p>"; ?></center>
<center>
<form action="upload.php" method="POST" enctype="multipart/form-data">

Identificador: <input type="text" name="identificador" size="3" >


    Arquivo: <input type="file" required name="arquivo">

    <input type="submit" value="Salvar">

</br>
</br>
<h1><center><font face="CALIBRI">-----------------------------------------</font></center></h1> 
</form>
</center></div>
</a>
</body>

I’m having a hard time with this part.

1 answer

1

That field código is the primary key of the table arquivo? If it is, you can do something like this:

    $identificador = $_POST['identificador'];
    $arquivo = $_FILES['arquivo'];

    $query = $conexao->prepare("INSERT INTO arquivo (arquivo, identificador) 
    VALUES ( ?, ? )");

    $query->bindParam(1, $arquivo);
    $query->bindParam(2, $identificador);

    if ($query->execute()) 
        $msg = "Imagem enviada com sucesso!!!";
    else 
        $msg = "Falha ao enviar a imagem.";
  • Yes, it is pk. And your solution was perfect, it worked properly. Obg

  • Good, take the opportunity to search this "bindParam" is interesting to always go through it when you will make an entry in the database to avoid sql Injection.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.