• Notice regarding image hosts and attachments. See here.

PHP/MySQL - Inserting data into MySQL DB when using uploadify to upload files

Associate
Joined
21 Oct 2006
Posts
1,615
Location
Cambridge
Hi Guys,

I'm writing a client area in PHP / MySQL backend - so far everything is tickety boo.

Login, Registration, Profile and Chat area all work fine.

I'm making a "documents" area for clients to upload & view their own files.

I'm using "uploadify" to make the upload process a bit more seamless. Uploadify itself is working perfectly, and the files are uploading correctly. Great. So the problem?

I'd like to insert some data into the MySQL DB upon a successful file upload.

Documents.php:
PHP:
<?php

session_start();

if(!isset($_SESSION['username'])){
    header( 'Location: login.php' );
}

require('db.php'); // Connect to the database since we'll be needing it later.

// Get Clients ID
$client_ID = mysql_query("SELECT 'client_ID'
				   FROM 'clients'
				    WHERE username='".$_SESSION['username']."'");

// Displays all current files
$getDocuments = mysql_query("SELECT * FROM uploads2 WHERE ID='".$client_ID."'");

while($row = mysql_fetch_array($getDocuments, MYSQL_ASSOC))
{
    echo "Download: <a href='" . $row['URL'] . "'>" . $row['NAME'] . "</a> | " . $row['SIZE'] . " | " . $row['CONTENT_TYPE'] . "<br /><br />";
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Client Documents</title>

<!--UPLOADIFY -->
<link href="./uploadify/uploadify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="./uploadify/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="./uploadify/swfobject.js"></script>
<script type="text/javascript" src="./uploadify/jquery.uploadify.v2.1.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  $('#file_upload').uploadify({
    'uploader'  : './uploadify/uploadify.swf',
    'script'    : './uploadify/uploadify.php',
    'cancelImg' : './uploadify/cancel.png',
    'folder'    : './uploads',
    'multi': true,
	'buttonText': 'Select Files',
	'checkScript': './uploadify/check.php',
	'displayData': 'speed',
	'onAllComplete'  : function(){ alert("Thank you. All files have been uploaded successfully."); },
	'simUploadLimit': 2
  });
});
</script>
<!-- UPLOADIFY END-->

</head>
<body>

<form>
<input id="file_upload" name="file_upload" type="file" />
<a href="javascript:$('#file_upload').uploadifyUpload()">Start Upload</a> |  <a href="javascript:$('#file_upload').uploadifyClearQueue()">Clear Queue</a> 
</form>

</body>
</html>

As you can see below, i'm using
PHP:
mysql_query("INSERT INTO uploads2(ID, URL, NAME, SIZE, CONTENT_TYPE) VALUES ('$client_ID', '$targetFile', '$fileName', '$fileSize' , '$fileType')");
to insert into the DB at time of successful upload.

However, it's not working. Any ideas?

Uploadify.php
PHP:
if (!empty($_FILES)) {
	$tempFile = $_FILES['Filedata']['tmp_name'];
	$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
	$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
	
	//$fileName = $_FILES['uploadify']['name'];
    //$fileSize = $_FILES['uploadify']['size'];
    //$fileType = $_FILES['uploadify']['type'];
	$fileName = test;
    $fileSize = test;
    $fileType = test;
	
	// $fileTypes  = str_replace('*.','',$_REQUEST['fileext']);
	// $fileTypes  = str_replace(';','|',$fileTypes);
	// $typesArray = split('\|',$fileTypes);
	// $fileParts  = pathinfo($_FILES['Filedata']['name']);
	
	// if (in_array($fileParts['extension'],$typesArray)) {
		// Uncomment the following line if you want to make the directory if it doesn't exist
		mkdir(str_replace('//','/',$targetPath), 0755, true);
		
		move_uploaded_file($tempFile,$targetFile);
		mysql_query("INSERT INTO uploads2(ID, URL, NAME, SIZE, CONTENT_TYPE) VALUES ('$client_ID', '$targetFile', '$fileName', '$fileSize' , '$fileType')");
		//echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
		
		
	// } else {
	// 	echo 'Invalid file type.';
	// }
}
?>
 
Why waste time trying to debug it yourself when mysql will tell you where the issue is.

Use the php function mysql_error() to show it.

e.g.

Code:
        mysql_query("INSERT INTO uploads2(ID, URL, NAME, SIZE, CONTENT_TYPE) VALUES ('$client_ID', '$targetFile', '$fileName', '$fileSize' , '$fileType')") OR die(mysql_error());
 
Why waste time trying to debug it yourself when mysql will tell you where the issue is.

Use the php function mysql_error() to show it.

e.g.

Code:
        mysql_query("INSERT INTO uploads2(ID, URL, NAME, SIZE, CONTENT_TYPE) VALUES ('$client_ID', '$targetFile', '$fileName', '$fileSize' , '$fileType')") OR die(mysql_error());

I've tried this but it doesn't produce an error.

I've changed the mysql statement to;

PHP:
mysql_query("INSERT INTO uploads2(ID, URL, NAME, SIZE, CONTENT_TYPE) VALUES ('8', '8', '8', '8' , '8')") OR die(mysql_error());

So now it should simply enter 8 into each field, but it doesn't argh.
 
try to echo the mysql error on a new line rather than using it in an or statement

Also try echoing the query rather than running it, then pasting it into phpmyadmin to see if the query is the issue
 
Make sure you have a 'OR die(mysql_error())' on the mysql_query() and add 'onComplete': function(a, b, c, d, e){ alert(d);} which will display the reponse of uploadify.php (ie: show errors on the page).

Secondly, make sure you're connecting to your DB from within uploadify.php - ie add require('db.php'); before you make the query. And make sure you close the connection!

Also note that uploadify has the habbit of falling over if the upload script, uploadify.php in this case, doesn't reponsed with a '1' on a successful upload. So add a echo "1" at the bottom of the script.
 
Yes thanks mate, the original MySQL issue was down to not being able to read from the DB as you suggested - I'd connected to the DB in the file that was including uploadify.php but that wasn't enough.

A second issue is that due to a bug in flash uploadify can't access any session data directly, so I had to set the PHP session ID via 'scriptData', to be able to start the session and obtain my client_ID..!

In short, yes :D
 
Yes thanks mate, the original MySQL issue was down to not being able to read from the DB as you suggested - I'd connected to the DB in the file that was including uploadify.php but that wasn't enough.

A second issue is that due to a bug in flash uploadify can't access any session data directly, so I had to set the PHP session ID via 'scriptData', to be able to start the session and obtain my client_ID..!

In short, yes :D

You can send some information to your upload script using scriptData - helps when passing info between your page and your upload script. You could even use on the onComplete event to create a hidden form field contain the uploaded file id and client id.
 
Back
Top Bottom