...

View Full Version : Adding a reference to an album upload



Jenny Dithe
12-08-2010, 08:25 AM
Hi,

I have a script that works fine for uploading an album but I want to add a reference number and I can't seem to do this. The reference number is generated from another page and sent to this page as part of a query string q=reference number.

I will just paste below the relevant parts of the script from the main page, it is all on one page, let me know if you do need the whole script to makes sense of it.

The code is as follows:



$ref=$HTTP_GET_VARS["q"];

//edited out

function save($data) {
if($sql="INSERT INTO photos (Id, ref, subcategory, photo, date)
VALUES ('".$_SESSION['Id']."', '".$data['ref']."', '".$data['photo']."', '".$data['filename']."', NOW())"){
echo "This is " . $sql . "<br />";
return true;

}

//edited out

if(move_uploaded_file($_FILES['file']["tmp_name"][$key], UPLOADS_DIR . $newFileName.$fileType)) {
if($this->createThumb(UPLOADS_DIR . $newFileName.$fileType, $fileType, THUMBS_DIR . $newFileName.$fileType, 200, 200)) {
$data['ref'] = $ref;
$data['photo'] = $key;
$data['filename'] = $newFileName.$fileType;
if($this->save($data)) {
$this->status = 'Photo #'.$key.' is uploaded and saved to the db<br />';
} else {


Now I have tried all variations:



$data['ref1'] = '.$ref.';
$data['ref2'] = "$ref";
$data['ref3'] = $ref;
$data['ref4'] = "{$ref}";
$data['ref5'] = ".$ref.";


I have even tried changing the insert command:


if($sql="INSERT INTO blogphotos (Id, ref, subcategory, photo, date)
VALUES ('".$_SESSION['Id']."', '".$ref."', '".$data['photo']."', '".$data['filename']."', NOW())"){


Nothing works. But if I echo $ref, it shows wonderfully.

I'm also not sure where I have gone wrong in my logic here?

poyzn
12-08-2010, 12:03 PM
what type is the ref field in db and value of the ref variable?

Jenny Dithe
12-08-2010, 12:11 PM
The ref field in the database is currently set as varchar, even though at the moment it is a set of random numbers (I am debating changing it to a mix of letters and numbers which is why I haven't changed the varchar to int.)

I did do a test changing it to int, just in case, and that didn't work.

When I echo $sql out it just has '' for the ref value. So the value isn't actually getting to INSERT INTO command, but if I write echo $ref, elsewhere in the script, it does echo out. So I am assuming I have the wrong phrasing for the insert into, but as I said I tried everything I could think of.

poyzn
12-08-2010, 12:35 PM
oh, just put next code inside the save function


$ref=$HTTP_GET_VARS["q"];

and make '".$ref."' in the sql syntax
or next code into the upload function


$data['ref'] = $HTTP_GET_VARS["q"];

Jenny Dithe
12-08-2010, 03:13 PM
It won't for some reason come up with the function.

What I currently have is:


<?php

$ref=$HTTP_GET_VARS['q'];
echo "this is 1 " . $ref . "<br />";

//connect to db code

function save($data) {
$dref=$ref;
echo "this is dref" . $dref . "<br />";
if($sql="INSERT INTO photos (Id, ref, subcategory, photo, date)
VALUES ('".$_SESSION['Id']."', '{$dref}', '".$data['photo']."', '".$data['filename']."', NOW())"){
echo "This is " . $sql . "<br />";
return true;

}


And the echo is:
this is 1 1074615275128082806527590
this is dref
This is INSERT INTO photos (Id, ref, subcategory, photo, date) VALUES ('1893475024', '', '0', '32838e374bd6429.jpg', NOW())
Photo #0 is uploaded and saved to the db

I originally changed it to the below which I believe is what you suggested:




//connect to db code

function save($data) {

$ref=$HTTP_GET_VARS['q'];
if($sql="INSERT INTO photos (Id, ref, subcategory, photo, date)
VALUES ('".$_SESSION['Id']."', '".$ref."', '".$data['photo']."', '".$data['filename']."', NOW())"){
echo "This is " . $sql . "<br />";
return true;


And then changed it to the above to check that it was only within the function that it wouldn't be echoed out.

I have no idea why it would work outside the function and not within, unless I have misunderstood where to place the code.

poyzn
12-08-2010, 05:44 PM
Read here
http://php.net/manual/en/language.variables.scope.php

Jenny Dithe
12-09-2010, 04:57 AM
Thank you. Problem very easily solved with that great link.

I'm afraid I had come across globals before, but not having needed them I hadn't really understood their purpose until now.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum