Jump to content

Conditional insertion of one field into table


davidcriniti

Recommended Posts

Hi everyone,

 

I'm trying to create a site where teachers can upload educational animations (swfs) and source files (.fla's) to a site as a shared resource for others to use in their classrooms.

 

I've concatinated a random number to both the source file and animation file to circumvent the problem of files with same names being uploaded.

 

However,  I have one small problem. The source file is not a mandatory upload, so sometimes the $source_file is null. However, with the random number concatinated, $source_fileX is not null.

 

I tried to write some code saying that if $source_file is null, then $source_fileX should be null, else $source_fileX should be random_number concatinated with $source_file , but it doesn't seem to be working.

 

The animation files are uploading fine. It's just the source files that are not. Nor is $source_fileX being inserted into the source_file field in the database.

 

Code below. Thanks in advance.

 


<?php

$keywords = $_POST["keywords"];
$subject = $_POST["subject"];
$description = $_POST["description"];
$website = $_POST["website"];
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$school = $_POST["school"];
$animation_file = $_POST["animation_file"];
$source_file = $_POST["source_file"];
$random_digit=rand(00000, 9999);

	$animation_fileX=$random_digit . $_FILES['animation_file']['name'];
					if($animation_fileX!="")			
					{
						if (!copy($_FILES['animation_file']['tmp_name'], "uploads/$animation_fileX"))
						{
							echo "failed to copy \n";
						}						
					}

if   ($source_file!="")
{ 
$source_fileX=$random_digit . $_FILES['source_file']['name'];
}
else
{
$source_fileX="";
}

					if($source_fileX!="")			
					{
						if (!copy($_FILES['source_file']['tmp_name'], "source_file_uploads/$source_fileX"))
						{
							echo "failed to copy \n";
						}						
					}


//**********************SEND TO DATABASE****************************

include 'mysql_connect.php'; 

$query = "INSERT INTO animation_uploads (date, animation_file, source_file, keywords, subject, description, firstname, lastname, school, website)" . "VALUES (NOW(), '$animation_fileX', '$source_fileX',  '$keywords', '$subject' , '$description', '$firstname', '$lastname', '$school' , '$website')";
//if($query){echo 'data has been placed'}
mysql_query($query) or die(mysql_error());

//***********************END OF DATABASE CODE***********************



 

just to add belt and braces

i'd still check if a file named $animation_fileX already existed in the database

 

the object is to give files a unique name,

sods law dictates that eventually there will be an existing file

and it will happen when demo-ing to a client

 

to do this look at using a call back function whilst generating $animation_fileX - so:

 

generate $animation_fileX

check if exists in DB

if not return $animation_fileX

else call function again

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.