Update data File

Asked

Viewed 702 times

-1

I have a question about changing FILE data.

<?php

include("conectar.php");
$id = $_GET['id'];
$sql = mysql_query("Select* From tb_trabalhador WHERE id = $id");
while($exibe = mysql_fetch_array($sql)){

$id = $exibe["id"];

$Nome = $exibe["Nome"];

$Morada = $exibe["Morada"];

$Tipo = $exibe["Tipo"];

$Email = $exibe["Email"];

$AlvaraNumero = $exibe["AlvaraNumero"];

$AlvaraValidade = $exibe["AlvaraValidade"];

$AlvaraAnexo = $exibe["AlvaraAnexo"];
?>
<form action="salvaralteracao.php" method="POST">
    <input type="hidden" name="id" value="<?php echo $id; ?>">
    Nome<input type="Varchar" name="Nome" value="<?php echo $Nome; ?>"><p>
    Morada<input type="Text" name="Morada" value="<?php echo $Morada; ?>"><p>
    Email<input type="text" name="Email" value="<?php echo $Email; ?>"><p>
    AlvaraNumero<input type="integer" name="AlvaraNumero" value="<?php echo   $AlvaraNumero; ?>"><p>
    AlvaraValidade<input type="date" name="AlvaraValidade" value="<?php echo   $AlvaraValidade; ?>"><p>
    <input type="hidden" name="AlvaraAnexo" value="<?php echo $AlvaraAnexo; ?>"><p>

When I show it appears lots of symbols due to Attachments (PDF I put)

How to change this by showing attachment data names only?

Salvarcao alteracao.php

   <?php

  include("conectar.php");

  $id = $_POST['id'];

$Nome = $_POST['Nome'];

$Morada = $_POST['Morada'];

$Email = $_POST['Email'];

$AlvaraNumero = $_POST["AlvaraNumero"];

$AlvaraValidade = $_POST["AlvaraValidade"];

if (isset($_FILES[AlvaraAnexo]) && $_FILES[AlvaraAnexo]["name"]!=''){
$nomeTemporario = $_FILES["AlvaraAnexo"]["tmp_name"]; 

$fp = fopen($nomeTemporario, 'r'); 
$AlvaraAnexo = fread($fp, filesize($nomeTemporario)); 
$AlvaraAnexo = addslashes($AlvaraAnexo);

fclose($fp); }

$AcidenteNumero = $_POST["AcidenteNumero"];

$AcidenteValidade = $_POST["AcidenteValidade"];

if (isset($_FILES[AcidenteAnexo]) && $_FILES[AcidenteAnexo]["name"]!=''){

$nomeTemporario = $_FILES["AcidenteAnexo"]["tmp_name"]; 

$fp = fopen($nomeTemporario, 'r'); 
$AcidenteAnexo = fread($fp, filesize($nomeTemporario)); 
$AcidenteAnexo = addslashes($AcidenteAnexo);

fclose($fp)
    (...)
$sqlupdate = "Update tb_trabalhador SET Nome='$Nome',Morada='$Morada',Email='$Email',   AlvaraNumero='$AlvaraNumero',AlvaraValidade='$AlvaraValidade',AlvaraAnexo='$AlvaraAnexo',Aci    denteNumero='$AcidenteNumero',AcidenteValidade='$AcidenteValidade',
AcidenteAnexo='$AcidenteAnexo',SeguroNumero='$SeguroNumero',
 SeguroValidade='$SeguroValidade',SeguroAnexo='$SeguroAnexo',FinancasValidade='$FinancasValidade',
FinancasAnexo='$FinancasAnexo',SocialValidade='$SocialValidade',
SocialAnexo='$SocialAnexo',RemuneracaoValidade='$RemuneracaoValidade',
RemuneracaoAnexo='$RemuneracaoAnexo',InstaladorNumero='$InstaladorNumero',
InstaladorValidade='$InstaladorValidade',InstaladorAnexo='$InstaladorAnexo',
MontadorNumero='$MontadorNumero',MontadorValidade='$MontadorValidade',
MontadorAnexo='$MontadorAnexo' where id=$id ";

 mysql_query($sqlupdate) or die(mysql_error());header('Location: administrador.php');

?>

  • It looks like you are creating a form to allow the user to make changes to a record in your table. If so, the same will be able to exchange the document? (if not, my suggestion would be that you do not have that field hidden with the PDF binary) To update a record in the database you do not need to touch all fields, you can only touch the ones you actually want to change.

  • If the user has the possibility of exchanging the document, it could still be adjusted your form to not have that field hidden with the contents of the PDF. It all depends on the purpose that is driving you to have that field present.

  • Just as it is possible to have the other fields to change the pdf files could also change. My idea is to give chance to change all fields

2 answers

1

So that you can submit a form for changing all fields of a particular record in a table containing columns of the type BLOB with PDF documents present in it, you should avoid the fields input of the kind hidden with the content of the document.

You can get around by linking to the existing document so that the user can view it. You also apply a field input of the kind file to be realized the upload of a new document.

My suggestion then goes through creation of a PHP file for the sole purpose of displaying the PDF document in the browser which may be referred to as follows:

<?php

/** Construir link para apresentação de anexo
 *
 * @param integer $id      O id do registo na base de dados
 * @param string $campo    O nome do campo que contém o BLOB a ser apresentado
 *
 * @return string $html    O HTML necessário para apresentar um link
 */
function apresentarLinkParaDocumento ($id, $campo) {

  $href = 'verdocumentos.php?id='.$id.'&amp;documento='.$campo;
  $title = 'Clique para abrir documento';

  $html = '
  <p>
    Visualizar documento existente: <a href="'.$href.'" target="_blank" title="'.$title.'">Clique aqui</a>
  </p>';

  return $html;
}
?>

To be used:

if ($AlvaraAnexo!='')
  $preview = apresentarLinkParaDocumento ($id, $AlvaraAnexo);
else 
  $preview = '';

And so you have in the variable $preview the HTML required to display a link to the document if there is data in your variable $AlvaraAnexo. If no data exists, it is empty, so it can always be used.

In your form, in the place where you want the link, you apply the variable $preview:

<form action="salvaralteracao.php" method="POST">
  <p>
    <input type="hidden" name="id" value="<?php echo $id; ?>">
    Nome<input type="Varchar" name="Nome" value="<?php echo $Nome; ?>">
  </p>
  <p>
    Morada<input type="Text" name="Morada" value="<?php echo $Morada; ?>">
  </p>
  <p>
    Email<input type="text" name="Email" value="<?php echo $Email; ?>">
  </p>
  <p>
    AlvaraNumero<input type="integer" name="AlvaraNumero" value="<?php echo   $AlvaraNumero; ?>">
  </p>
  <p>
    AlvaraValidade<input type="date" name="AlvaraValidade" value="<?php echo   $AlvaraValidade; ?>">
  </p>
  <p>
    <?php echo $preview; ?>
  </p>
  <!-- Continuação do formulário... -->

Then you have the input of the kind file for a new document to be loaded and all you have to do is check if there is actually a new document to replace the contents of the field in the database:

<input type="file" name="AlvaraAnexo">

When the form is submitted by the user:

// se foi enviado um novo ficheiro
if (isset($_FILES[AlvaraAnexo]) && $_FILES[AlvaraAnexo]["name"]!='') {

  // operações de ler ficheiro para variável

  // consulta à base de dados para actualizar a coluna para este ficheiro
  $consulta = "UPDATE tb_trabalhador SET AlvaraAnexo='$AlvaraAnexoBLOB' WHERE id=$id";

  mysql_query($consulta) or die(mysql_error());
}

// nesta altura o ficheiro novo já foi aplicado na base de dados em vez do antigo, e continuas agora com o resto da operação de actualização.

There are two things mentioned in this answer that I did not elaborate on because they have already been discussed in two of your previous questions:


Updating

The query to update the data must be manipulated in the sense of not touching columns of type BLOB which are used to store files in the database.

For this purpose, and as shown above, you should consult for each column of the type BLOB when it received a new file to be stored in the database.

Below is your final query for the remaining columns without reference to the column AlvaraAnexo of the kind BLOB that has already been manipulated as we can see in the illustration above presented:

/* Removi a linha: "AlvaraAnexo = '$AlvaraAnexo',"
 * Pois essa coluna é actualizada quando verificamos se existe um novo ficheiro
 *
 * Deverás proceder da mesma forma para as restantes
 * colunas do tipo BLOB que guardam um ficheiro
 */
$sqlupdate = "
UPDATE tb_trabalhador 
SET
    Nome = '$Nome',
    Morada = '$Morada',
    Email = '$Email',
    AlvaraNumero = '$AlvaraNumero',
    AlvaraValidade = '$AlvaraValidade',
    AcidenteNumero = '$AcidenteNumero',
    AcidenteValidade = '$AcidenteValidade',
    AcidenteAnexo = '$AcidenteAnexo',
    SeguroNumero = '$SeguroNumero',
    SeguroValidade = '$SeguroValidade',
    SeguroAnexo = '$SeguroAnexo',
    FinancasValidade = '$FinancasValidade',
    FinancasAnexo = '$FinancasAnexo',
    SocialValidade = '$SocialValidade',
    SocialAnexo = '$SocialAnexo',
    RemuneracaoValidade = '$RemuneracaoValidade',
    RemuneracaoAnexo = '$RemuneracaoAnexo',
    InstaladorNumero = '$InstaladorNumero',
    InstaladorValidade = '$InstaladorValidade',
    InstaladorAnexo = '$InstaladorAnexo',
    MontadorNumero = '$MontadorNumero',
    MontadorValidade = '$MontadorValidade',
    MontadorAnexo = '$MontadorAnexo'
WHERE id=$id";

mysql_query($sqlupdate) or die(mysql_error());
header('Location: administrador.php');
  • Placing to view attachments and placing the Button to load data disappears . Switching from Blob to Null

  • @user3253195 For data to disappear, it is because in the database query to update the data you are sending null or an empty variable. You must perform a query to update all fields except those in the files. Files should be updated only if the user has uploaded a new file as illustrated in the reply.

  • Boas Zuul. I didn’t really understand the first Function. I already have a file that shows PDF’s. On the same page put: <?php echo $preview; ? ><input type="file" name="Alvaraanexo"> On the same correct line?

  • @user3253195 The function apresentarLinkParaDocumento() serves to prepare the HTML to present a link to the document. You must change the construction of the link within it to adapt to your scenario. How you present it in the form, it will depend on your layout, but how you suggest it works yes.

  • I’m just having trouble because they all go to NULL and I don’t know why. if (isset($_FILES[Alvaraanexo]) && $_FILES[Alvaraanexo]["name"]!='') { have this code in the save and they still remain null.

  • @user3253195 Just looking at your file where you are looking at the database I can see the problem. To pass to NULL to the database can only be because you are working on that particular column whether there has been a new upload or not.

  • I just made up a little bit and I don’t know if this is how Salvaralteração.php

  • @user3253195 As I said earlier, I think your problem is in the update query... The query has to ignore the fields with BLOB whose files have not been changed, that is, if no new file has been sent to save, the query cannot touch that field. How do you say NULL, It sounds to me like you’re messing with the field by updating whether there’s data or not. Put the part of the update query in your question, seeing it I can help more.

  • I put the update part in the question.

  • @user3253195 I updated the answer to your final query. I also updated the part where you check if the file exists to leave the query that updates the column of the file according to your reality.

Show 5 more comments

0

Try to use the PDF Parser to convert PDF file to text.

Browser other questions tagged

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