Heyy...i'm trying to save the attachment file location in mydb...the thing is it's just saving the file name
eg.database.txt

i want it to look like this
C:/bla/bla/test

my codes
addtask.php

<html>
<head>
<title>Task Management System</title>
<link rel="stylesheet" href="Appcss.css" type="text/css"/>
<script src="datetimepicker_css.js"></script>   
<script type="text/javascript">
      // Javascript function which takes care for multiple uploads
      var attachmentlimit = 5; // Limiting maximum uploads to 5
      var attachmentid = 1;
      function attachmore() { // Function is called when user presses Attach Another File
        attachmentid += 1;
        document.getElementById('attachmentdiv').innerHTML += '<div id="attachmentdiv_' + attachmentid + '" style="margin-top:5px"><input type="file" id="attachment_' + attachmentid + '" name="attachment_' + attachmentid + '" size="30" onchange="document.uploadattachments.submit();"/></div>';
        if(attachmentid == attachmentlimit) {
          document.getElementById('addanother').style.display='none';
        }
      }
    function testSelect(form) 
    {
        var multipleVar = form.list.options[form.list.selectedIndex].value 
        if(form.comments.value == "")
        {
            form.comments.value = multipleVar + '\n'; 
         }
        else if (form.comments.value != "")
        {
            var insertedMultipleVar = form.comments.value;
            form.comments.value = insertedMultipleVar +  multipleVar + '\n'
         }
}
    </script>
</head>
<body>
<form name=form5 method="post">
<table width="100%" border=1 bordercolor="blue" bgcolor="blue">  
<tr>
<td align=right><input value="HOME" type="submit" size="25" onclick="form5.action='mainpage.php'"><input value="LOGOUT" type="submit" size="25" onclick="form5.action='logout.php'"></td>           <!--creating User ID textbox-->
</tr>    
</table>
</form>
<center>
<FORM METHOD=GET name="form" >                               <!--Creating a form with actions-->                                          <!--Body CSS-->                              <!--Inserted images-->
<h1>Add New Task</h1>
<hr>
<table border=1 bordercolor=black><tr><td>
<table>
<h3><u>Task Details</u></h3>    
<tr>
<td>Task Name : </td><td><b><input name="taskname" type="text" size=20></td></tr>
<tr><td>Assign By : </td><td><b><select name="aname">             <!--Name of the combo box-->     
<?php
//for each row we get from mysql, echo a form input
while ($row = mysql_fetch_array($result1)) {
echo "<option>$row[Name]</option>\n";
}
?></td></tr>
<tr><td>Responsible Staff : </td><td><select name="list">                <!--Name of the combo box-->     
<?php
//for each row we get from mysql, echo a form input

while ($row1 = mysql_fetch_array($result)) {
echo "<option>$row1[Name]</option>\n";
}
?>
</select><input type="button" value="Add!" onClick="testSelect(this.form);"></td></tr>  
<tr><td></td>
<td><textarea name="comments" cols="40" rows="4" ></textarea></td></tr>
<tr><td>Start Date :</td><td><input type="date" name="sdate" id="sdate" size=10><img src="images/cal.gif" onclick="javascript:NewCssCal('sdate','yyyymmdd')" style="cursor:pointer"/></td></tr>             
<tr><td>End Date :</td><td><input type="date" name="edate" id="edate" size=10 name="edate"/></input><img src="images/cal.gif" onclick="javascript:NewCssCal('edate','yyyymmdd')" style="cursor:pointer"/></td></tr>
<tr><td>Description</td><td><textarea name="comments1" cols="40" rows="5" onfocus="clearContents(this);">Describe Your Task....</textarea></td></tr>
<tr><td>Attachment</td><td>    
    <!-- Form taking care of the uploads, notice that the frame target is the iframe contained inside, to which it fires upload.php -->
    <form id="uploadattachments" enctype="multipart/form-data" name="uploadattachments" target="attachmentiframe" action="upload.php" method="post">
      <div id="attachmentdiv" >
        <iframe name="attachmentiframe" style="display:none"></iframe>
        <div id="attachmentdiv_1" style="margin-top:5px">
            <input type="file" id="attachment_1" name="attachment_1" size="50" onchange="document.uploadattachments.submit();"/>
        </div>
      </div>
      <!-- div showing error message for invalid file type -->
      <div id="typeerrormessage" style="display:none;margin-left:30px">
        <font color=#990000 size=1>Only png, jpg and gif file type are supported</font>
      </div>
      <!-- div showing error message for exceeded file size -->
      <div id="sizeerrormessage" style="display:none;margin-left:30px">
        <font color=#990000 size=1>File exceeded maximum allowed limit of 100 Kb</font>
      </div>
      <div id="addanother" >
        <a href="javascript:void(0)" onclick="attachmore();"><font size=2>Attach another file</font></a>
      </div>
    </form></td></tr>
<tr><td></td><td><input type="submit" value="Add Task!" onclick="form.action='taskregistration.php';"></td></form></tr></table></td></tr></table>                                                                               
</center></body>
</html>

taskregistration.php

<title>Task Management System</title>
<link rel="stylesheet" href="Appcss.css" type="text/css"/>
<?php
session_start();
if($_GET["taskname"] &&$_GET["aname"] && $_GET["comments"] && $_GET["sdate"] && $_GET["edate"] && $_GET["comments1"] && $_GET["attachment_1"])  //getting the value from the register form(if fail or empty) promt user
{
        require_once 'connection_details.php';                  //Calling the Connection Detail php file
        $tbl_name="task";                                       // Table name

        // Connect to server and select database.
        mysql_connect("$host", "$username", "$password")or die("cannot connect");
        mysql_select_db("$db_name")or die("cannot select DB");

        $taskname=$_GET["taskname"];
        $aname=$_GET["aname"];
        $rname=$_GET["comments"];
        $sdate=$_GET["sdate"];
        $edate=$_GET["edate"];
        $desc=$_GET["comments1"];
        $link=$_GET["attachment_1"];



        echo $taskname;
        echo $aname;
        echo $rname;
        echo $sdate;
        echo $edate;
        echo $desc;


            //sql commands to insert the data into the database
            $sql="INSERT INTO $tbl_name (TaskName,AssignBy,ResponsStaff,StartDate,EndDate,Description,AFile)VALUES('".$taskname."','".$aname."','".$rname."','".$sdate."','".$edate."','".$desc."','".$link."')";       //sql query defined
            $result=mysql_query($sql) or mysql_error();                                         //runs the query
            echo "<h1>you have registered sucessfully</h1>";                  
            header("refresh:1;url=MainPage.php" );                          //links to main login page after one second delay
}
else {
print"invalid data";
//header("refresh:1;url=addtask.php" );
}
?>

upload.php

<?php
  // Set the upload target directory
  $target_path = "Data/";

  for($i=1;$i<=5;$i++) {
    $attachments = 'attachment_'.$i;
    $attachmentdiv = 'attachmentdiv_'.$i;
    $FileName = $_FILES[$attachments]['name'];
    // Check if filename is empty
    if($FileName != "") {
      $FileType = $_FILES[$attachments]['type'];
      $FileExtension = strtolower(substr($FileName,strrpos($FileName,'.')+1));
      // Check for supported file formats
      if($FileExtension != "doc" && $FileExtension != "docx" && $FileExtension != "pdf" && $FileExtension != "txt") {
        echo "<script type='text/javascript'>parent.document.getElementById('typeerrormessage').style.display = 'inline';</script>";
      }
      else {
        $FileSize = round($_FILES[$attachments]['size']/1024);
        // Check for file size
        if($FileSize > 300) {
          echo "<script type='text/javascript'>parent.document.getElementById('sizeerrormessage').style.display = 'inline';</script>";
        }
        else {
          $FileTemp = $_FILES[$attachments]['tmp_name'];
          $FileLocation = $target_path.basename($FileName);
          // Finally Upload
          if(move_uploaded_file($FileTemp,$FileLocation)) {
            // On successful upload send a message to corresponding attachmentdiv from which the file came from
            echo "<script type='text/javascript'>parent.document.getElementById('".$attachmentdiv."').innerHTML = '<input CHECKED type=\"checkbox\"><a href=\"http://abhinavsingh.com/webdemos/upload/".$FileName."\" target=\"_blank\"><font size=2><b>".$FileName."</b> <i>(".$FileType.")</i> ".$FileSize." Kb</font>';</script>";
            echo "<script type='text/javascript'>parent.document.getElementById('typeerrormessage').style.display = 'none';</script>";
            echo "<script type='text/javascript'>parent.document.getElementById('sizeerrormessage').style.display = 'none';</script>";
          } 
          else {
            echo "There was an error uploading the file, please try again!";
          }
        }
      }
    }
  }
?>

can someone guide me...thank you in advance

Recommended Answers

All 4 Replies

Looks like you need to give your uploads an id and that task table needs an id

then you pass the task id into the upload or the upload id into the task and pull across the full path instead of just the filename.

and then also make the form more sequential - it seems as though you can upload a file or add a task separately, making it so you add a task and can attach an upload to the task you would have the task info you need to insert or attach the upload to it

Or as i think would be the senior way of doing it - you can make them as separate entities and have uploads and tasks - then a manaement of both and a select box drop down where you can select what upload you want to attach to the task and an option to upload a new file and edit the task to add the new image

actually i already have an attribute called taskID in table task, u mean by passing the taskID into the upload.php??

could you kindly show me an example...i don't quite understand your statement...sorry

Sorry got in the middle of stuff atm

Heres an upload table i quickly grabbed:

CREATE TABLE `uploads` (
  `id` INT(8) NOT NULL AUTO_INCREMENT,#unique upload id
  `c_id` INT(8) DEFAULT NULL,#user_id that uploaded file
  `tid` INT(6) DEFAULT NULL,#ticket_id the upload is attached to(task_id for you)
  `folder` ENUM('general','report','attachment') NOT NULL DEFAULT 'general',
  `title` VARCHAR(120) DEFAULT NULL,
  `filename` VARCHAR(200) DEFAULT NULL,
  `filetype` VARCHAR(60) DEFAULT NULL,
  `virus_checked` TINYINT(1) NOT NULL DEFAULT '0',
  `time_uploaded` DATETIME DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MYISAM DEFAULT CHARSET=latin1

And i had it set up on a ticketing system, heres the ticket table:

CREATE TABLE `tickets` (
  `tid` INT(9) NOT NULL AUTO_INCREMENT,#ticket_id
  `creator_id` INT(6) NOT NULL,#user_id of creator
  `to_id` INT(6) NOT NULL DEFAULT '0',#rest is irrelavant
  `to_staff_id` INT(6) NOT NULL DEFAULT '0',
  `circular` TINYINT(1) NOT NULL DEFAULT '0',
  `datecreated` DATETIME DEFAULT NULL,
  `lastreply` DATETIME DEFAULT NULL,
  `subject` VARCHAR(150) DEFAULT NULL,
  `category` ENUM('Query','Question','Complaint','Internal','Circular') NOT NULL DEFAULT 'Query',
  `status` ENUM('Amber','Green','Red','Yellow') NOT NULL DEFAULT 'Red',
  `cat_id` INT(6) DEFAULT NULL,
  `question_feat` VARCHAR(20) DEFAULT NULL,
  `cust_says_finished` TINYINT(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`tid`)
) ENGINE=MYISAM DEFAULT CHARSET=latin1

I'll work on pulling out the files i got on it, they are dug into a crm system with a huge function file

Looking at the file i've put together it will probably cause more confusion than help, the quickest way to get it working is just put the 2 forms together so they both have all the data:

<form method='post' enctype="multipart/form-data">
<input type='hidden' name='task_id' value='5'/>
<table>
<h3><u>Task Details</u></h3>    
<tr>
<td>Task Name : </td><td><b><input name="taskname" type="text" size=20></td></tr>
<tr><td>Assign By : </td><td><b><select name="aname">             <!--Name of the combo box-->     
<?php
//for each row we get from mysql, echo a form input
while ($row = mysql_fetch_array($result1)) {
echo "<option>$row[Name]</option>\n";
}
?></td></tr>
<tr><td>Responsible Staff : </td><td><select name="list">                <!--Name of the combo box-->     
<?php
//for each row we get from mysql, echo a form input
while ($row1 = mysql_fetch_array($result)) {
echo "<option>$row1[Name]</option>\n";
}
?>
</select><input type="button" value="Add!" onClick="testSelect(this.form);"></td></tr>  
<tr><td></td>
<td><textarea name="comments" cols="40" rows="4" ></textarea></td></tr>
<tr><td>Start Date :</td><td><input type="date" name="sdate" id="sdate" size=10><img src="images/cal.gif" onclick="javascript:NewCssCal('sdate','yyyymmdd')" style="cursor:pointer"/></td></tr>             
<tr><td>End Date :</td><td><input type="date" name="edate" id="edate" size=10 name="edate"/></input><img src="images/cal.gif" onclick="javascript:NewCssCal('edate','yyyymmdd')" style="cursor:pointer"/></td></tr>
<tr><td>Description</td><td><textarea name="comments1" cols="40" rows="5" onfocus="clearContents(this);">Describe Your Task....</textarea></td></tr>
</table>                       
<h2>Attach file here.  We will accept any file.  Maximum size is 20mb (20480kb).  Please check before uploading.</h2>
<br/>
    <input type="hidden" name="trig" value="true">
    <label for="file">File to upload:</label>
    <input type="file" name="file" id="file"/>
    <br/>
    <label for='title'>Give file a name:</label><input id='title' type='text' name='title'/>
    <br/>
    <input type='hidden' name='folder' value='attachment'/>
    <br/>
<input name='submit' type='submit' value='Send Reply'/>
</form>

Then once you done that the part that adds the task needs to be updated so it adds the task and returns the task_id then forwards it straight to the file uploading code so it has it available to write in the location into the DB.

It should be something like this:

if($_GET["taskname"] &&$_GET["aname"] && $_GET["comments"] && $_GET["sdate"] && $_GET["edate"] && $_GET["comments1"] && $_GET["attachment_1"])  //getting the value from the register form(if fail or empty) promt user
{
        require_once 'connection_details.php';                  //Calling the Connection Detail php file
        $tbl_name="task";                                       // Table name
        // Connect to server and select database.
        mysql_connect("$host", "$username", "$password")or die("cannot connect");
        mysql_select_db("$db_name")or die("cannot select DB");
        $taskname=$_GET["taskname"];
        $aname=$_GET["aname"];
        $rname=$_GET["comments"];
        $sdate=$_GET["sdate"];
        $edate=$_GET["edate"];
        $desc=$_GET["comments1"];
        $link=$_GET["attachment_1"];
        echo $taskname;
        echo $aname;
        echo $rname;
        echo $sdate;
        echo $edate;
        echo $desc;
        //sql commands to insert the data into the database
        $sql="INSERT INTO $tbl_name (TaskName,AssignBy,ResponsStaff,StartDate,EndDate,Description,AFile)VALUES('".$taskname."','".$aname."','".$rname."','".$sdate."','".$edate."','".$desc."','".$link."')";       //sql query defined
        $result=mysql_query($sql) or mysql_error();     
        $task_id = mysql_insert_id();<----- Note pulling the newly created task id for last insert
        //runs the query
        echo "<h1>you have registered sucessfully</h1>";
        $target_path = "Data/";
        for($i=1;$i<=5;$i++) {
            $attachments = 'attachment_'.$i;
            $attachmentdiv = 'attachmentdiv_'.$i;
            $FileName = $_FILES[$attachments]['name'];
            // Check if filename is empty
            if($FileName != "") {
                $FileType = $_FILES[$attachments]['type'];
                $FileExtension = strtolower(substr($FileName,strrpos($FileName,'.')+1));
                // Check for supported file formats
                if($FileExtension != "doc" && $FileExtension != "docx" && $FileExtension != "pdf" && $FileExtension != "txt") {
                    echo "<script type='text/javascript'>parent.document.getElementById('typeerrormessage').style.display = 'inline';</script>";
                }else{
                    $FileSize = round($_FILES[$attachments]['size']/1024);
                    // Check for file size
                    if($FileSize > 300) {
                        echo "<script type='text/javascript'>parent.document.getElementById('sizeerrormessage').style.display = 'inline';</script>";
                    }else{
                        $FileTemp = $_FILES[$attachments]['tmp_name'];
                        $FileLocation = $target_path.basename($FileName);
                        // Finally Upload
                        if(move_uploaded_file($FileTemp,$FileLocation)) {
                            // On successful upload send a message to corresponding attachmentdiv from which the file came from
                            $query = "insert into uploads (`task_id`,`fileloc`,`something`,`somethingelse`) VALUES({$task_id},'{$fileLocation}','Costa','5:45PM')";<--- inserting upload into db with locxation data
                            mysql_query($query) or die(mysql_error());
                            echo "<script type='text/javascript'>parent.document.getElementById('".$attachmentdiv."').innerHTML = '<input CHECKED type=\"checkbox\"><a href=\"http://abhinavsingh.com/webdemos/upload/".$FileName."\" target=\"_blank\"><font size=2><b>".$FileName."</b> <i>(".$FileType.")</i> ".$FileSize." Kb</font>';</script>";
                            echo "<script type='text/javascript'>parent.document.getElementById('typeerrormessage').style.display = 'none';</script>";
                            echo "<script type='text/javascript'>parent.document.getElementById('sizeerrormessage').style.display = 'none';</script>";
                        }else{
                            echo "There was an error uploading the file, please try again!";
                        }
                    }
                }
            }
        }
        header("refresh:1;url=MainPage.php" ); //links to main login page after one second delay
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.