Jump to content

PHP Upload file field insert question


adrianle

Recommended Posts

I've got the standard PHP file up load functionality working properly. The problem is that while the other fields contained in the same upload form get INSERTed into mySQL properly as expected, the filename field does not.  everything LOOKS fine to me.. and all I can think of is that for some reason, the field TYPE (which is to say:  type="file") is the roadblock. Is there something in PHP that says "yes, I know you're a file to be uploaded, but I won't actually recognize the value in this field as something I can insert into the filename column".  ???? Help!

Link to comment
Share on other sites

Hi.. sorry, not following. Are you making an obtuse comment suggesting that you would have preferred to see code of some sort? I really wasn't looking for something that in-depth, it was just a general question about the "file" field types...  I didn't want to load up a ton of code if this was a known "issue"..  Some guidance is appreciated.

Link to comment
Share on other sites

It wasn't obtuse by any definition of the word. If you want help with your code, post the code within

 . . . 

tags. "The file name won't insert" could have many causes, and there's no point making it a guessing game for those who would try to help you.

Link to comment
Share on other sites

So here's the form field in question: 

 <form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="form1">
  <table width="500" border="0" cellspacing="0" cellpadding="4">
    <tr>
      <td>Choose Tag Image: </td>
      <td><input name="uploadedfile" type="file" /></td>
    </tr>
    <tr>...and the rest of the form code continues as normal

 

and here's the insert... the behavior is that the column that's supposed to take the value from "uploadedfile" is NULL after insert:

<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
		$theValue = ($theValue != "") ? "'" . date("Y-m-d",strtotime($theValue)) . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if (!isset($mm_abort_edit) || !$mm_abort_edit) {
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO TAGS (ImageName, `Description`, Difficulty, LM, BU, BM, MT, VPLUS, Short, Voice, Favorite, Added) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['uploadedfile'], "text"),
                       GetSQLValueString($_POST['Desc'], "text"),
                       GetSQLValueString($_POST['Difficulty'], "int"),
                       GetSQLValueString($_POST['LM'], "text"),
                       GetSQLValueString($_POST['BU'], "text"),
                       GetSQLValueString($_POST['BM'], "text"),
                       GetSQLValueString($_POST['MT'], "text"),
                       GetSQLValueString($_POST['VPLUS'], "text"),
                       GetSQLValueString($_POST['Short'], "text"),
                       GetSQLValueString($_POST['Voice'], "text"),
                       GetSQLValueString(isset($_POST['Favorite']) ? "true" : "", "defined","1","0"),
                       GetSQLValueString($_POST['Added'], "date"));

  mysql_select_db($database_DNSTags, $DNSTags);
  $Result1 = mysql_query($insertSQL, $DNSTags) or die(mysql_error());

  $insertGoTo = "list_tags_new.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}}
?>

Link to comment
Share on other sites

The file name won't be in the $_POST array, it will be in the $_FILES array. You should also consider adding logic that checks whether $_FILES['uploadedfile']['error'] is anything other than 0 (zero) as that would indicate there was an error with the actual upload, and give you some insight as to what happened. More on file upload error codes here: http://www.php.net/manual/en/features.file-upload.errors.php

Link to comment
Share on other sites

So the code $_FILES['uploadedfile']['name'] you mentioned isn't IN my INSERT code.. so I'm unsure where you're getting this from. It does exist in the upload portion of the code but i'm not there yet. I'm just trying to get the insert working.

Link to comment
Share on other sites

Well I tried having the upload chunk in with the insert, and it DOES upload the file, but the filename is still null in the table.. 

 
$target_path = "tagImages/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}

 

...is what I'm using...

Link to comment
Share on other sites

Here's the entire chunk currently ... 

<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
		$theValue = ($theValue != "") ? "'" . date("Y-m-d",strtotime($theValue)) . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if (!isset($mm_abort_edit) || !$mm_abort_edit) {
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

  
  
$target_path = "tagImages/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded"; $uploaded_file = $target_path; // or $_FILES['uploadedfile']['name']


  $insertSQL = sprintf("INSERT INTO TAGS (ImageName, TagTitle, Difficulty, LM, BU, BM, MT, VPLUS, Short, Voice, Favorite, Added) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['uploadedfile'], "text"),
                       GetSQLValueString($_POST['TagTitle'], "text"),
                       GetSQLValueString($_POST['Difficulty'], "int"),
                       GetSQLValueString($_POST['LM'], "text"),
                       GetSQLValueString($_POST['BU'], "text"),
                       GetSQLValueString($_POST['BM'], "text"),
                       GetSQLValueString($_POST['MT'], "text"),
                       GetSQLValueString($_POST['VPLUS'], "text"),
                       GetSQLValueString($_POST['Short'], "text"),
                       GetSQLValueString($_POST['Voice'], "text"),
                       GetSQLValueString(isset($_POST['Favorite']) ? "true" : "", "defined","1","0"),
                       GetSQLValueString($_POST['Added'], "date"));

  mysql_select_db($database_DNSTags, $DNSTags);
  $Result1 = mysql_query($insertSQL, $DNSTags) or die(mysql_error());

} else{
    echo "There was an error uploading the file, please try again!";
}
  $insertGoTo = "list_tags_new.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.