The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
After Submitted but before page load...
Discuss After Submitted but before page load... in the PHP Development forum on Dev Shed. After Submitted but before page load... PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 14th, 2012, 09:57 PM
|
 |
Contributing User
|
|
Join Date: Nov 2012
Posts: 98
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?
|

November 14th, 2012, 10:00 PM
|
 |
Contributing User
|
|
Join Date: Nov 2012
Posts: 98
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.";
}
|

November 15th, 2012, 07:45 AM
|
 |
Contributing User
|
|
Join Date: Nov 2012
Posts: 98
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?
|

November 15th, 2012, 08:35 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
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.
|

November 15th, 2012, 08:46 AM
|
 |
Contributing User
|
|
Join Date: Nov 2012
Posts: 98
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?
|

November 15th, 2012, 08:57 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
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.
|

November 15th, 2012, 09:04 AM
|
 |
Contributing User
|
|
Join Date: Nov 2012
Posts: 98
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?
|

November 15th, 2012, 09:16 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
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.
|

November 15th, 2012, 09:19 AM
|
 |
Contributing User
|
|
Join Date: Nov 2012
Posts: 98
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|