Adding file upload to a database insert

I have an insert record form which inserts content into a title and content field of a table (as well as id). I would like to add an upload form which will
Upload a file into a folder called 'docs'
Add the file name into a filed called 'doc' in the above table

Here is my code, could someone please modify it to add the upload functionality?

Many thanks

<?php require_once('../Connections/hudsonwalker_conn.php'); ?>

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

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  return $theValue;

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

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

  $insertSQL = sprintf("INSERT INTO jobs (content, title) VALUES (%s, %s)",
                       GetSQLValueString($_POST['content'], "text"),
                       GetSQLValueString($_POST['title'], "text"));

  mysql_select_db($database_hudsonwalker_conn, $hudsonwalker_conn);
  $Result1 = mysql_query($insertSQL, $hudsonwalker_conn) or die(mysql_error());
  $insertGoTo = "content.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  header(sprintf("Location: %s", $insertGoTo));

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Add a Vacancy</title>
<link href="hwadmin.css" rel="stylesheet" type="text/css" />


<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <table align="center">
    <tr valign="baseline">
      Insert job details
    <tr valign="baseline">
      <td><input type="text" name="title" value="Title" size="32"></td>
    <tr valign="baseline">
      <td><textarea name="content" cols="60" rows="50">Content</textarea>
    <tr valign="baseline">
      <td><input type="submit" value="Insert record"></td>
  <input type="hidden" name="MM_insert" value="form1">


Who is Participating?
mnb93Connect With a Mentor Commented:
This should get you started:
cursiveAuthor Commented:
Unfortunately I'm very low on time, as stated above,I would greatly appreciate someone modifying the above code.

Thank you
Julian MatzConnect With a Mentor Joint ChairpersonCommented:

What you're trying to do is very simple, but for someone to rewrite your code would be extremely time consuming. I've created some custom functions for a real estate site which upload, resize and delete images. You could integrate them with your code as PHP functions and customize to suit your needs. You can get the code here:

Also have a look at
for references...........
bahadirkocaogluConnect With a Mentor Commented:

Please view this document:

You can upload your all files to mysql as binary.
Really all you need to create is:
<!-- file upload -->
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload!" />
<!-- file upload -->
in the form

Then check that the file was uploaded correctly:
so make sure $_FILES['uploadedfile']['size'] > 0

$data = file_get_contents($_FILES['uploadedfile']['tmp_name']);

and insert this into your query (after escaping)
After that:
//Delete File

For column content use BLOB data type. BLOB is a binary large object that can hold a variable amount of data. MySQL have four BLOB data types, they are :

    * BLOB

Since BLOB is limited to store up to 64 kilobytes of data use MEDIUMBLOB so you can store larger files ( up to 16 megabytes ).

Just to add your GetSQLValueString function is quite flawed, you might want to have a look at mysql_real_escape_string.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.