Avatar of Adam
AdamFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

Amending code to upload a file to a folder on my server. It's not working.

Hello,

I'm trying to amend some code which was created by a developer for a website I own (the developer is currently not contactable).

Looking at the code she created, I'm trying to modify it for use in a new webpage, stored on a different server (which points to a different database etc). The code will do exactly the same in the new website as it did in the old, so I had hoped it would just be a case of changing the settings to connect to the new database. Unfortunately not.


My amended code is shown below (with my comments) - I've taken out what I think is the unimportant stuff for the purpose of this question:

<?php
session_start();
if($_POST['submit'] == ''){
?>


.....

<form enctype="multipart/form-data" action="<?php echo $_SERVER['file:///C|/Users/Adam/Documents/websites/orangutanenglish/PHP_SELF']; ?>" - I'm not sure what this paths refers to (My Comment)

 method="post"> <!-- JHCO: changed from $_SERVER['../PHP_SELF'] to $_SERVER['PHP_SELF'] -->
  <div align="center">
          <input type="hidden" name="MAX_FILE_SIZE" value="8000000" />
          <span class="style18">Select your profile picture</span><strong>:</strong>
          <input type="file" name="user_picture" />
          <input type="hidden" name="user_id" value="<?php echo $_SESSION['user_id']; ?>" />
          <input type="submit" name="submit" value="submit" />
        </div>
    </form>

      ....<?php
} else {
require_once('../Connections/sensei.php'); - this file exists (My Comment)

$upload_path = "/home/orangu8/public_html/upload"; - changed to new path (My Comment)

$extension = $_FILES['user_picture']['name'];
$extension = explode('.',$extension);
$extension = array_reverse($extension);
$extension = $extension[0];
$upload_file_name = "user_pic-".$_SESSION['user_id'].'.'.$extension;
$upload_full_path = $upload_path.'/'.$upload_file_name;

if(move_uploaded_file($_FILES['user_picture']['tmp_name'], $upload_full_path)){

$sqlUpdateUser = "UPDATE TestTable SET photoid='".$upload_file_name."', photo='1' WHERE autonumber=".$_POST['user_id'];

mysql_select_db($database_sensei, $sensei);
if(! ( mysql_query($sqlUpdateUser, $sensei))){
die('There was a problem!<br>Query being exectued:'.$sqlUpdateUser.'<br><br> MySQL error number:'.mysql_errno($sensei).'<br><br> MySQL message:'.mysql_error($sensei));
} else {
           echo '<script language="JavaScript">
           window.location="SenseiConnect_Add_Your_Profile2.php";
           </script>';
}
} else {
echo "there was a problem.";
}
 
}
?>

Open in new window



Anyway, the error I get when I try to upload a file to the location is -

There was a problem!
Query being exectued:UPDATE TestTable SET photoid='user_pic-.png', photo='1' WHERE autonumber=

MySQL error number:1064

MySQL message:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

If there's a simple tutorial somewhere about uploading files to locations on the server I'd be happy if somebody could point me towards it. Or if the code above just needs a tweak or two to get it working I'd be grateful if anybody could let me know what needs tweaked.

I'm not a developer by the way, just trying to re-use code I don't really understand.

thanks.


PHPAdobe DreamweaverHTML

Avatar of undefined
Last Comment
gplana
SOLUTION
Avatar of gplana
gplana
Flag of Spain image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of wmadrid1
wmadrid1
Flag of Colombia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
ASKER CERTIFIED SOLUTION
Avatar of maeltar
maeltar
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Adam
Adam
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Many thanks all very the very quick repsonse.

qplana, Roads Roads,

You were correct. There is a record insert form before the upload page. The idea is that certain text information is entered through that form, and then when the user clicks submit, there are taken to this (the upload form) where the user uploads a photograph (but to the same record in the database).

Comparing the previous files, it seemed that this line was missing from the 'new' version of the set up in the page prior to the the upload page:

$_SESSION['user_id'] = mysql_insert_id($sensei);

I have now added it, but disappointingly I still get the same error message.

I will try again and make the change to the action part also.  


wmadrid1 - I will add your suggested reporting text and post with the results.

Thanks all.
Avatar of Adam
Adam
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Thanks Maeltar,

I've made the changes you suggested. Nope I wasn't bypassing any stage. I'm not a programmer and am just trying to change some code a developer craeted for another website I own which is the same as this new one I'm trying to build. I have very limited understanding of what the code's doing so, yeah, it could be the simplest thing.

Anyway, I'm still getting the following message despite the changes:

There was a problem!
Query being exectued:UPDATE TestTable SET photoid='user_pic-.png', photo='1' WHERE autonumber=

MySQL error number:1064

MySQL message:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Have I set the session variably incorrectly. Other than the line of code

$_SESSION['user_id'] = mysql_insert_id($sensei);

Is there anything else required to set the session variable? 'sensei' is refering to the file sensei.php in the connectons folder (I assume?)...

thanks,
Avatar of Adam
Adam
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

After adding error reporting suggested by wmadrid1, I get the following error before I try to upload any file:

Notice: Undefined index: submit in /home/orangu8/public_html/senseiconnect.net/SenseiConnect_Add_Your_Profile2.php on line 7

Line 7 is

if($_POST['submit'] == ''){
Avatar of Adam
Adam
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Maeltar,

I've now tried changing


<form enctype="multipart/form-data" method="post">

from the suggested:

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>"  method="post" />

The error message changed a bit to

Notice: Undefined index: user_id in /home/orangu8/public_html/senseiconnect.net/SenseiConnect_Add_Your_Profile2.php on line 59
There was a problem!
Query being exectued:UPDATE TestTable SET photoid='user_pic-.png', photo='1' WHERE autonumber=
Notice: Undefined index: user_id in /home/orangu8/public_html/senseiconnect.net/SenseiConnect_Add_Your_Profile2.php on line 33


MySQL error number:1064

MySQL message:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=
Notice: Undefined index: user_id in /home/orangu8/public_ht' at line 1

thanks.
Avatar of Adam
Adam
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

line 33 is <input type="hidden" name="user_id" value="<?php echo $_SESSION['user_id']; ?>" />
Avatar of Adam
Adam
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Okay, I see I was missing the following line of code which was on the previous code: I have now added this. Sorry - I guess this is necessary to get the session started.

session_start();
if (isset($_POST['submit'])) {

Feel like I'm getting closer...
Avatar of Adam
Adam
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Okay, well that seems to be it working now. As I think everybody here could tell, it was a problem with the session variable - i.e. it not being defined. Many thanks to everybody who pointed this out to me. Much appreciated.
Avatar of Adam
Adam
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

thanks all!
Avatar of gplana
gplana
Flag of Spain image

Exactly, session_start() function should be called at the beggining if you want to use sessions on your php code.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo