troubleshooting Question

capture a file upload name and insert it into mysql

Avatar of derekstattin
derekstattin asked on
PHPMySQL Server
5 Comments1 Solution314 ViewsLast Modified:
I am having trouble capturing the variable filename. Can you tell me how to catch this variable so I can insert it into the mysql table?


<form method="post" enctype="multipart/form-data" action="do_adddoc.php">
<p><strong>Your Username:</strong><br/>
<input type="text" name="topic_owner" size="50" maxlength="150"/></p>
<p><strong>Document Title:</strong><br/>
<input type="text" name="topic_title" size="50" maxlength="150"/></p>
<p><strong>Post Text:</strong><br/>
<textarea name="post_text" rows="8" cols="65" wrap="virtual"></textarea></p>
<input type="hidden" name="MAX_FILE_SIZE" value="51200"/>
<p><strong>File to Upload:</strong> <input type="file" name="fileupload" /></p>
<p><input type="submit" name="submit" value="upload!"></p>
</form>




<?php
//check for required fields from the form
if ((!$_POST["topic_owner"]) || (!$_POST["topic_title"]) || (!$_POST["post_text"])) {
      header("Location: adddoc.php");
      exit;
}
//create nice message for user
$display_block = "<p>Your document has <strong>".$_POST["topic_title"]."</strong> been uploaded. Please go to the document upload list to view uploaded documents</p>";
//upload file to directory
$file_dir = "upload";
   
foreach($_FILES as $file_name => $file_array) {
      echo "path: ".$file_array["tmp_name"]."<br/>\n";
      echo "name: ".$file_array["name"]."<br/>\n";
      echo "type: ".$file_array["type"]."<br/>\n";
      echo "size: ".$file_array["size"]."<br/>\n";
 
      if (is_uploaded_file($file_array["tmp_name"])) {
            move_uploaded_file($file_array["tmp_name"], "$file_dir/".$file_array["name"]) or die ("Couldn't copy");
            echo "Your file has been uploaded to the Members' Upload Area!<br/>";

$filename = $file_array["name"];

//create nice message for user
$display_block = "<p>Your document has been uploaded. Please go to the document upload list to view uploaded documents</p>";

//connect to server
$mysqli = mysqli_connect("");

$filename = $file_array["name"];

//create and issue the first query
$add_topic_sql = "INSERT INTO doc_topics (topic_title, topic_create_time, topic_owner, filename) VALUES ('".$_POST["topic_title"]."', now() , '".$_POST["topic_owner"]."' , '".$_POST["$filename"]."' )";
$add_topic_res = mysqli_query($mysqli, $add_topic_sql) or die(mysqli_error($mysqli));
//get the id of the last query
$topic_id = mysqli_insert_id($mysqli);

//create and issue the second query
$add_post_sql = "INSERT INTO doc_posts (topic_id,post_text,post_create_time,post_owner,filename) VALUES ('".$topic_id."', '".$_POST["post_text"]."', now(), '".$_POST["post_owner"]."', '".$_POST["filename"]."')";
$add_post_res = mysqli_query($mysqli, $add_post_sql) or die(mysqli_error($mysqli));

//close connection to mysqli
mysqli_close($mysqli);
      }
}
?>
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros