Avatar of derekstattin
derekstattin
 asked on

capture a file upload name and insert it into mysql

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);
      }
}
?>
PHPMySQL Server

Avatar of undefined
Last Comment
derekstattin

8/22/2022 - Mon
Zyloch

From what I see, you have the file name in $filename. I'm not sure what $_POST["$filename"] is supposed to mean...
derekstattin

ASKER
(topic_id,post_text,post_create_time,post_owner,filename) VALUES ('".$topic_id."', '".$_POST["post_text"]."', now(), '".$_POST["post_owner"]."', '".$_POST["filename"]."')";

I am trying to insert the name of the document that is being uploaded The same name that is being echoed by. echo "name: ".$file_array["name"]."

Do I need to substitute the POST for a different command. Or use $_POST["name"] in the place of $_POST["filename"]?
Zyloch

Use $filename in place of $_POST["filename"]
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Zyloch

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
derekstattin

ASKER
Thank you, you solved the problem!!!

I moved this peice of code above the foreach loop.

(Though php connect was inside the foreach loop, the name of the file was still posted to server.)

//connect to server
$mysqli = mysqli_connect;

If you have any other suggestions please let me know!

//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/>";