PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 14th, 2012, 09:57 PM
BitZoid's Avatar
BitZoid BitZoid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 98 BitZoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 20 m 38 sec
Reputation Power: 1
Name uploaded file appropriately

Using MySQL and PHP to manipulate.

I have a page where a user can submit a file and it stores the file location in the database, and the file on the server. I'm trying to properly name the file as it is uploaded, as the auto incremented sql id field of the brand newly created mysql record(row) that is created when i submit the file and the details of record at the same time. When the form is submitted however, the record is just then created, and the auto incremented ID is then created after the image upload - how can I name this file appropriately to match the record?

Reply With Quote
  #2  
Old November 14th, 2012, 10:00 PM
BitZoid's Avatar
BitZoid BitZoid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 98 BitZoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 20 m 38 sec
Reputation Power: 1
I Want to name the file the same as mdl_key which is the auto incremented integer in the mysql Model table
RELEVANT CODE::


PHP Code:
// Check if form has been submitted.
if (isset($_POST['submitted'])) {

    
$errors = array(); // initilize error array.

    //HANDLE FILE UPLOADS

        // PDF FILES
            //This is the directory where images will be saved
                
$target "./pdf/";
                
$target $target basename$_FILES['pdf']['name']);


    
// handle drop boxes
    
$man_key $_POST['manufacturer'];
    
$cls_key $_POST['class'];
    
$sub_cls_key $_POST['subclass'];
    
$pdf=($_FILES['pdf']['name']);

    if (empty(
$_POST['video'])) {
        
$video NULL;
    } else {
        
$video trim($_POST['video']);
    }

    if (empty(
$_POST['description'])) {
        
$description NULL;
    } else {
        
$description trim($_POST['description']);
    }


        
// Check for a model key
    
if (empty($_POST['model'])) {
        
$errors[] = 'You forgot to enter a model.';
    } else {
        
$model trim($_POST['model']);
    }

    if ((empty(
$_POST['rent'])) && (empty($_POST['sell']))) {
        
$errors[] = 'You must select either Rentable or Sellable checkbox.';
    }


    if (
$_POST['rent']) {
        
$rent 1;

            if (
$_POST['rentprice']) {
                
$rentprice $_POST['rentprice'];
            } else {
                
$errors[] = 'If you select rentable, you must enter a rental price.';
                
$rentprice 0;

            }

    } else {
        
$rent 0;
        
$rentprice 0;
    }

    if (
$_POST['sell']) {
        
$sell 1;

            if (
$_POST['sellprice']) {
                
$sellprice $_POST['sellprice'];
            } else {
                
$errors[] = 'If you select sellable, you must enter a sale price.';
                
$sellprice 0;
            }

    } else {
        
$sell 0;
        
$sellprice 0;
    }


    if (empty(
$errors)) { // Everything is ok

        /// Add inventory item
        
require_once ('../mysql_connect.php'); // Connect to the DB.

        // Make Query
        
$queryInsert "INSERT INTO model (man_key, cls_key, sub_cls_key, model, rent, sell, rentprice, sellprice, description, video, pdf)
        VALUES ('
$man_key', '$cls_key', '$sub_cls_key', '$model', '$rent', '$sell', '$rentprice', '$sellprice', '$description', '$video', '$pdf')";
        
$resultInsert = @mysql_query ($queryInsert); // Run Query


        
if(move_uploaded_file($_FILES['pdf']['tmp_name'], $target))
        {

            
//Tells you if its all ok
        
echo "The file "basename$_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
        } else {

        
//Gives and error if its not
        
echo "Sorry, there was a problem uploading your file.";
        } 

Reply With Quote
  #3  
Old November 15th, 2012, 07:45 AM
BitZoid's Avatar
BitZoid BitZoid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 98 BitZoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 20 m 38 sec
Reputation Power: 1
I guess I could put the uploaded pdf file name directly in the database record, so it forms a relationship with the rest of the new record - instead of using the mdl_key as the image name, to make the relationship.

But I'm rusty with array's. How would I do this?

Reply With Quote
  #4  
Old November 15th, 2012, 08:35 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,875 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 3 h 47 m 15 sec
Reputation Power: 813
Hi,

I don't see this "mdl_key" anywhere in your code. But the function mysql_insert_id() will return the ID of the last row you inserted.

Reply With Quote
  #5  
Old November 15th, 2012, 08:46 AM
BitZoid's Avatar
BitZoid BitZoid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 98 BitZoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 20 m 38 sec
Reputation Power: 1
Quote:
Originally Posted by Jacques1
Hi,

I don't see this "mdl_key" anywhere in your code. But the function mysql_insert_id() will return the ID of the last row you inserted.


The mdl_key is auto inc. and wasn't nessecary to include in query

Thanks, this is useful. Since the pdf file that is uploaded with the record is inserted into the table at the time of the record - how should I go about utilizing this? Should I remove the $pdf fields from the original queryInsert = "", then use the mysql_insert_id() and rename the file to mysql_insert_id value directly below that. Finally, run a separate queryInsertFILE = "" with the $pdf variable placed in the record from here, so as to include the proper file name of the newly created rows auto inc ID?

Reply With Quote
  #6  
Old November 15th, 2012, 08:57 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,875 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 3 h 47 m 15 sec
Reputation Power: 813
When the pdf file has the row ID as the name, then you don't need to store the file name in the table.

Another option would be to give the file a random name (which is more secure) and store it in the table. But you have to make sure that the name is actually random (e. g. use openssql_random_pseudo_bytes) so that there are no collisions.

Reply With Quote
  #7  
Old November 15th, 2012, 09:04 AM
BitZoid's Avatar
BitZoid BitZoid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 98 BitZoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 20 m 38 sec
Reputation Power: 1
Great, then I can just make the retrieval of the records pdf file something like echo "$folder . '/' . $mdl_key . '.pdf'";

When I'm doing this method, I will remove the $pdf field from the queryInsert. How do I handle renaming the file after the record is inserted though? move_uploaded_file?

Reply With Quote
  #8  
Old November 15th, 2012, 09:16 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,875 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 3 h 47 m 15 sec
Reputation Power: 813
Quote:
Originally Posted by BitZoid
When I'm doing this method, I will remove the $pdf field from the queryInsert. How do I handle renaming the file after the record is inserted though? move_uploaded_file?


Yes. I mean, the query and the file don't really have anything to do with each other. It's just that you need to wait with the renaming until the row has been inserted to get the ID.

Reply With Quote
  #9  
Old November 15th, 2012, 09:19 AM
BitZoid's Avatar
BitZoid BitZoid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 98 BitZoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 20 m 38 sec
Reputation Power: 1
Quote:
Originally Posted by Jacques1
Yes. I mean, the query and the file don't really have anything to do with each other. It's just that you need to wait with the renaming until the row has been inserted to get the ID.


Thank you for your help. Got it.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > After Submitted but before page load...

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap