davidcriniti Posted January 18, 2012 Share Posted January 18, 2012 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*********************** Quote Link to comment https://forums.phpfreaks.com/topic/255324-conditional-insertion-of-one-field-into-table/ Share on other sites More sharing options...
litebearer Posted January 19, 2012 Share Posted January 19, 2012 the basic idea is to add the random number AFTER you ascertain if there is a file to upload Quote Link to comment https://forums.phpfreaks.com/topic/255324-conditional-insertion-of-one-field-into-table/#findComment-1309079 Share on other sites More sharing options...
bspace Posted January 19, 2012 Share Posted January 19, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/255324-conditional-insertion-of-one-field-into-table/#findComment-1309083 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.