Page 1 of 1

Help with passing value in fileUpload class/function/Mysql

Posted: Fri Mar 20, 2009 3:31 pm
by dv_evan
Hello All,

I have a problem passing value.
I have downloaded Max's Upload php codes and attempting to customise it to suite my need. Evrything is working fine, upload and entering the file name into a MySQL database are working.

I need to also enter and ID value coming from a form along with the file name into the database. I tried many ways but failed to pass the value into the SQL Statement to update the data base.

This value if coming from a submittion form pass into upload.php ($PID=$_POST['id'];) , the problem is when I tried Passing the value of $PID into the SQL in maxUpload.class.php function Upload($PID) is not working. I tried echoing the variable $PID it works at the begining of the funcion but not in the IF statements which consist of the SQL statement.

I Tried to expalin this problem as best as I can and hope you understand, but please ask me for clarification.

Here are the two php files below and the table fields.

Kindly help.
Dave



Table fields:
ID,FileName






upload.php

Code: Select all

<?php require_once("maxUpload.class.php"); ?>
 
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Max Upload</title>
   <link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
 
<body>
<?php
       $PID=$_POST['id'];
 
    $myUpload = new maxUpload(); 
    //$myUpload->setUploadLocation(getcwd().DIRECTORY_SEPARATOR);
    $myUpload->uploadFile($PID);
?>
</body>  


maxUpload.class.php

Code: Select all

<?php
/*************************************************
 * Max's File Uploader
 *
 * Version: 1.0
 * Date: 2007-11-26
 *
 ****************************************************/
 
 
class maxUpload{
    var $uploadLocation;
    
    /**
     * Constructor to initialize class varaibles
     * The uploadLocation will be set to the actual 
     * working directory
     *
     * @return maxUpload
     */
    function maxUpload(){
        $this->uploadLocation = getcwd().DIRECTORY_SEPARATOR;
    }
 
    /**
     * This function sets the directory where to upload the file
     * In case of Windows server use the form: c:\\temp\\
     * In case of Unix server use the form: /tmp/
     *
     * @param String Directory where to store the files
     */
    function setUploadLocation($dir){
        $this->uploadLocation = $dir;
    }
    
    function showUploadForm($msg='',$error=''){
?>
       <div id="container">
            <div id="header"><div id="header_left"></div>
            <div id="header_main">Max Upload</div><div id="header_right"></div></div>
            <div id="content">
<?php
if ($msg != ''){
    echo '<p class="msg">'.$msg.'</p>';
} else if ($error != ''){
    echo '<p class="emsg">'.$error.'</p>';
 
}
?>
                <form action="" method="post" enctype="multipart/form-data" >
                     <center>
                         <label>File:
                             <input name="myfile" type="file" size="30" /><br>
               <input type="text" name="ID" size="30" />
 
                         </label>
                         <label>
                             <input type="submit" name="submitBtn" class="sbtn" value="Upload" />
                         </label>
                     </center>
                 </form>
             </div>
             <div id="footer"><a href="http://www.phpf1.com" target="_blank">Powered by PHP F1</a></div>
         </div>
<?php
    }
 
    function uploadFile($PID){
        if (!isset($_POST['submitBtn'])){
            $this->showUploadForm();
        } else {
            $msg = '';
            $error = '';
 
            //Check destination directory
            if (!file_exists($this->uploadLocation)){
                $error = "The target directory doesn't exists!";
            } else if (!is_writeable($this->uploadLocation)) {
                $error = "The target directory is not writeable!";
            } else {
                include ('testConn.php');
                $pic=( $_FILES['myfile']['name']);
                //Writes the information to the database
               mysql_query("INSERT INTO `employees` VALUES ('$PID', '$pic')") ;
 
                $target_path = $this->uploadLocation . basename( $_FILES['myfile']['name']);
 
                if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
                    $msg = basename( $_FILES['myfile']['name']).
                    " was uploaded successfully!";
                } else{
                    $error = "The upload process failed!";
                }
            }
 
            $this->showUploadForm($msg,$error);
        }
 
    }
 
}
?>

Re: Help with passing value in fileUpload class/function/Mysql

Posted: Fri Mar 20, 2009 3:52 pm
by php_east
dv_evan wrote:I tried echoing the variable $PID it works at the begining of the funcion but not in the IF statements which consist of the SQL statement.
i don't understand this part. exacly what do you mean by 'it works' ?
and how or in what way does it not work in the IF ?

also, you need to know that insert cannot take in the same id, it will give you mysql error duplicate key. to insert into the same id, you need to do an update, or use insert/on duplicate key update. this may not be the fault, but is somethiing you have to eliminate as being possible fault.

Re: Help with passing value in fileUpload class/function/Mysql

Posted: Fri Mar 20, 2009 5:45 pm
by dv_evan
Thanks for the response php_east.
Here is what I meant by your queries:
I tried echoing the variable $PID it works at the begining of the funcion but not in the IF statements which consist of the SQL statement.
Answer:
Refer to line 68 onward

Code: Select all

function uploadFile($PID){
               [color=#BF0000]echo $PID;[/color]  //[b]this output works[/b]
        if (!isset($_POST['submitBtn'])){
            $this->showUploadForm();
        } else {
                    
            [color=#BF0000]echo $PID;[/color]  //[b]this does not work [/b]  
            $msg = '';
            $error = '';
 

also, you need to know that insert cannot take in the same id, it will give you mysql error duplicate key. to insert into the same id, you need to do an update, or use insert/on duplicate key update. this may not be the fault, but is somethiing you have to eliminate as being possible fault.
Answer: I do not have a problem with the SQL Staement, the problem is getting the value of $PID to INSERT into the table. And I do not need to update the table, I am able to INSERT into the table with the File Name but the problem is I cannot get the value from the passed value ($PID) to INSERT into the table.

I do hope I clarify this and you can assist me in getting the $PID to INSERT into the table.


Kindly ask if you need anymore questions.

Appreciates all help.
Thanks
Dave

Re: Help with passing value in fileUpload class/function/Mysql

Posted: Sat Mar 21, 2009 3:25 am
by php_east

Code: Select all

<form action="" method="post" enctype="multipart/form-data" >
your form doesn't go anywhere. i think it shoud go to upload.php yes ?