Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP MySql & Javascript

    Hello to all!

    I have a form that, by PHP, uploads files to a certain path and also inserts some sql data on a database. This upload form is on the page that also retrieves the information sent before to the database.
    However, the user has to refresh the page to see the information about the file he has already uploaded. How can i had that function to the action (as long as the php upload, and sql insert).

    here is the code
    Code:
    <?php
    	//Incluir o ficheiro onde está definida a ligação para o MySQL.
    	include("mysql.inc");
    
    	echo "<h1>Detalhes de Projecto</h1>";
    	$id = $_GET["id"];
    	
    	$sql = "SELECT projecto.titulo, projecto.localizacao, projecto.data, documento.caminho, autor.nome1, autor.nome2 
    	FROM projecto, documento, autor, projectoautor  
    	WHERE autor.id = projectoautor.idautor AND projecto.documento = documento.id
    	AND projecto.id = projectoautor.idprojecto	
    	AND projecto.id = $id	";
    	$resultado = mysql_query($sql);
    
    	$linha = mysql_fetch_array($resultado, MYSQL_ASSOC);
    	
    	$titulo = $linha["titulo"];
    	$localizacao = $linha["localizacao"];
    	$data = $linha["data"];
    	$autor = $linha["nome1"]. " " .$linha["nome2"];
    	
    	echo "<table width=100% border=0>
      <tr>
        <td width=12%>T&iacute;tulo:</td>
        <td width=88 align=right>$titulo</td>
      </tr>
      <tr>
        <td>Institui&ccedil;&otilde;es:</td>
        <td></td>
      </tr>
      <tr>
        <td>Autores:</td>
        <td align=right>$autor</td>
      </tr>
      <tr>
        <td>Localiza&ccedil;&atilde;o:</td>
        <td align=right>$localizacao</td>
      </tr>
      <tr>
        <td>Data:</td>
        <td align=right>$data</td>
      </tr>
      <tr>
        <td></td>
        <td></td>
      </tr>
    </table>";
    	
    
      $sql = "SELECT  documento.id, documento.caminho, documento.thumb, documento.tipo, documento.tamanho, documento.projecto
    		 FROM documento, projecto 
    		 WHERE projecto.id= documento.projecto AND documento.projecto=$id";
      $result = mysql_query ($sql, $ligacao_mysql);
      if (mysql_num_rows($result)>0) {
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
          $str .= "<a href=\"detalhes1.php?id=".$row["id"]."\">".$row["caminho"]."</a> ";
          $str .= "[".$row["thumb"]."] ";
          $str .= "[".$row["tipo"]."] ";
          $str .= "[<a href=\"detalhes1.php?act=rem&id=".$row["id"]."\">Remove</a>]<br>";
        }
        print $str;
      }
    
    // you can change this to any directory you want
    // as long as php can write to it
    $uploadDir = "C:\wamp\www\bo\upload2\files\/";
    
    
    if(isset($_POST['upload']))
    {
        $caminho = $_FILES['userfile']['name'];
        $thumb  = $_FILES['userfile']['tmp_name'];
        $size = $_FILES['userfile']['size'];
        $type = $_FILES['userfile']['type'];
     
        // the files will be saved in filePath 
        $filePath = $uploadDir . $caminho;
    	
        // move the files to the specified directory
        // if the upload directory is not writable or
        // something else went wrong $result will be false
        $result    = move_uploaded_file($thumb, $filePath);
        if (!$result) {
            echo "Error uploading file";
            exit;
        }
    
        if(!get_magic_quotes_gpc())
        {
            $fileName  = addslashes($caminho);
            $filePath  = addslashes($filePath);
        }  
    
        $query = "INSERT INTO documento (caminho, thumb, tipo, tamanho, projecto ) ".
                 "VALUES ('$caminho', '$thumb', '$type', '$size', '$id')";
    
        mysql_query($query) or die('Error, query failed : ' . mysql_error());                    
    	
        echo "<b>Ficheiro enviado</b>";
    
    	}    
    	// fazer isto se o utilizador clickar apagar ou ver
    	
    if ($_GET) {
      $id = $_GET["id"];
      $act = $_GET["act"];
      switch ($act) {
        case rem:
    		unlink("C:\wamp\www\bo\upload2\files\notas.txt");
          $sql = "DELETE FROM documento WHERE id=$id";
          @mysql_query ($sql, $ligacao_mysql);
         # Header("Location:./detalhes1.php");
          exit();
          break;
        default:
          print "<img src=\"image.php?iid=$iid\">";
          break;
      }
    }  
    	mysql_close($ligacao_mysql);  
    ?>
    <form class="enviar"action="" method="post" enctype="multipart/form-data" name="uploadform">
      <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
        <tr> 
          <td width="246"><input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="userfile" type="file" class="box" id="userfile">
             </td>
          <td width="80"><input name="upload" type="submit" class="box" id="upload" value="  Upload  "></td>
        </tr>
      </table>
    </form>
    </div>
    Thank you in advance

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    I am assuming that this line:

    PHP Code:
      $sql "SELECT  documento.id, documento.caminho, documento.thumb, documento.tipo, documento.tamanho, documento.projecto
             FROM documento, projecto 
             WHERE projecto.id= documento.projecto AND documento.projecto=$id"

    Fetches the documents from the Database. The reason they need to refresh is that this SELECT is before the INSERT. So first it selects and prints the documents, then you insert it. Excluding the new document from the SELECT.

    The solution would be to move the SELECT after the INSERT.

    Just a helpful tip, and this is a personal one. I see that you are quite good with English. Unless your project disallows it, it might be beneficial to write your code in English too. This makes it easier for others to read your code.

  • #3
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    in fact that line does also fetches the general information, but not the actual file. I can have the general information but not uploaded files so the order has to be the same.

    thanks for the advice


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •