Website Membership Plugin -- Uploading Image(s)

I followed your example. It works to upload my image but there is an issue. When I click update without a new image to upload, it removes my old image. Are other people getting this?

Re: [gkornbluth] Website Membership Plugin -- Uploading Image(s)

By Jason - December 13, 2011

Hi Jerry,

Is the image being successfully uploaded to a file on the server? If so, you can then get it into CMSB using the saveUploadFromFilepath() function.

In this example, I'm assuming that the name of your upload field is "image":

$userNum = mysql_insert_id();

saveUploadFromFilepath('customer_uploads', 'image', $userNum, '123456789' , $path_of_uploaded_file);


Hope this helps get you started.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Website Membership Plugin -- Uploading Image(s)

Thanks Jason,

Again a perfect (and elegant) solution. (and another recipe for the cookbook)

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [Jason] Website Membership Plugin -- Uploading Image(s)

Hi Jason,

Seems I fix one thing and I break another.

In post 91302 you helped me to update a record using a where clause to filter which record would be updated.

This works fine except for inserting an image into the record when updating. (I thought the same code you suggested here would also work for a record update) BTW: The new image file does get saved to the server.

I'm using this to upload an image where none existed before. I guess another question will be, will this replace an image if one exists?

The code I'm using to update the record is below. (complete file is attached)

Hope it's an easy fix.

Thanks,

Jerry Kornbluth
/load records
list($customer_uploadsRecords, $customer_uploadsMetaData) = getRecords(array(
'tableName' => 'customer_uploads',
));

mysql_query("UPDATE `{$TABLE_PREFIX}customer_uploads` SET

email = '".$email."',
first_name = '".$first_name."',
last_name = '".$last_name1."',
street_address = '".$street_address."',
city = '".$city."',
state = '".$state."',
zip = '".$zip."',

sales_id = '".$sales_id."',
createdDate = NOW(),
updatedDate = NOW(),
createdByUserNum = '0',
updatedByUserNum = '0'
WHERE activation_code = '".mysql_escape($activation_code)."'")
or die("MySQL Error updating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
$userNum = mysql_insert_id();
@saveUploadFromFilepath('customer_uploads', 'uploads', $userNum, '123456789' , $path_of_uploaded_file);
$errors='Thank You<br /><br />Your Portrait Order Has Been Successfully Placed.';
}

?>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Attachments:

ordernow3_001.php 12K

Re: [gkornbluth] Website Membership Plugin -- Uploading Image(s)

By Jason - December 16, 2011

Hi Jerry,

So you're trying to save an upload to all the records that just got updated. Is that right?

I think the problem will have to do with $userNum. Since your update query could be updating multiple records, mysql_insert_id() won't be sufficient.

What you could try is to retrieve all the records that were just updated, and perform the saveUploadFromFilePath() call on each record.

example:

$customerUploadRecords = mysql_select("customer_upload", "activation_code = '".mysql_escape($activation_code)."'");

foreach ($customerUploadRecords as $record) {
@saveUploadFromFilepath('customer_uploads', 'uploads', $record['num'], '123456789' , $path_of_uploaded_file);
}


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Website Membership Plugin -- Uploading Image(s)

Thanks again Jason,

I only needed it for the single image in the single record. but I didn't change the code you suggested. and it worked perfectly.
_______

I hope you don't mind my asking one more small question.

I've managed to implement a check for an existing image in the html returned to the customer, but I'm having trouble checking the database and not allowing a second upload with the code you suggested. (right now it keeps adding images)

Everything I've tried made quite a mess.

Any suggestions?

Jerry Kornbluth

The code I used: entire file attached:
<?php if (!$customer_uploadsRecords): ?>
<span class="body-text-11">We're sorry.<br /><br />Activation Code "<?php echo @$activation_code; ?>" wasn't found in our system.<br /><br />Please check the number you entered, and then click <a href="upload.php">HERE</a> to try again, or <a href="contact.php">HERE</a> to contact customer support. <br/><br/></span><?php else: ?>
<?php foreach ($customer_uploadsRecords as $record): ?>
<?php if (sizeof($record['uploads']) >= 1): ?><span class="body-text-bold-11">There's already an image uploaded for the order with Activation Code, "<?php echo @$activation_code; ?>".<br /><br />Sorry, you can't upload a second image.<br /><br />Click <a href="purchase.php">HERE</a> to place a new order, or <a href="contact.php">HERE</a> to contact customer support.</span>


<?php else: ?>
<span class="body-text-bold-11"><?php echo @$errors;?></span><br />
<span class="body-text-11"><?php echo $common_informationRecord['image_upload_confirmation_page_text'] ?></span><br />
<span class="body-text-11"><?php if (@$name_of_uploaded_file): ?><br />Uploaded Image File Name: "<?php echo @$name_of_uploaded_file; ?>"<?php else: ?><br />No Image was uploaded with this order.<?php endif ?>

</span></p>
<span class="body-text-11">Activation Code: "<?php echo @$activation_code; ?>"</span> <?php endif ?> <?php endforeach; ?><?php endif ?>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Attachments:

ordernow3_002.php 13K

Re: [gkornbluth] Website Membership Plugin -- Uploading Image(s)

By Jason - December 19, 2011

Hi Jerry,

In the foreach loop, you can query the uploads table to check to see if there are any uploaded records for the current record.

Try this:

foreach ($customerUploadsRecords as $record) {
if (mysql_count('uploads', "tableName = 'customer_uploads' AND fieldName = 'uploads' AND recordNum = '".mysql_escape($record['num'])."'")) {
$errors .= "You have already uploaded an image for this record! <br/>\n";
}
else{
@saveUploadFromFilepath('customer_uploads', 'uploads', $record['num'], '123456789' , $path_of_uploaded_file);
}
}
$errors .= 'Thank You<br /><br />Your Portrait Order Has Been Successfully Updated.';


You'll notice that I also changed the last line with $errors to append to the string, instead of assigning a value to it. This is so any errors don't get overwritten.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/