Jump to content


Photo

Help with PHP script please


  • This topic is locked This topic is locked
37 replies to this topic

#21 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 05 September 2006 - 06:59 PM

Yes, change to

   $insertSQL = sprintf("INSERT INTO plants (image_name, image_type, image_size, path, plant_name, plant_type, planting_months, blooming_season, plant_description) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
    $fileImagename,
    $fileImagetype,
    $fileImagesize,
    $uploaddir,
    GetSQLValueString($_POST['plant_name'], "text"),
    GetSQLValueString($_POST['plant_type'], "text"),
    GetSQLValueString($_POST['planting_months'], "text"),
    GetSQLValueString($_POST['blooming_season'], "text"),
    GetSQLValueString($_POST['plant_description'], "text"));


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#22 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 05 September 2006 - 07:01 PM

Thanks mate,

Giving me yet another error now, and this time i have absolutely zero idea what's causing it :(

The file has been uploadedYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'test'', ''test'', ''test'', ''test'', ''test'')' at line 1

#23 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 05 September 2006 - 07:05 PM

After that sprintf() call I posted above, add

echo $insertSQL ;

and post the output
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#24 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 05 September 2006 - 07:07 PM

Right then, Here is the output....Hope i placed it in right place!

The file has been uploadedINSERT INTO plants (image_name, image_type, image_size, path, plant_name, plant_type, planting_months, blooming_season, plant_description) VALUES ('blush_delieux.jpg', 'image/pjpeg', '4006', '/plants/', ''test'', ''test'', ''test'', ''test'', ''test'')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 'test'', ''test'', ''test'', ''test'', ''test'')' at line 1

Thatnks for that btw....didn't know about that little trick :)

#25 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 05 September 2006 - 07:13 PM

OK I see it.

Dreamweaver's GetSQLValueString() is already putting single quotes around text values, so the last 5 %s in the sprintf() don't need them.

Change to

$insertSQL = sprintf("INSERT INTO plants (image_name, image_type, image_size, path, plant_name, plant_type, planting_months, blooming_season, plant_description) VALUES ('%s', '%s', '%s', '%s', %s, %s, %s, %s, %s)",
    $fileImagename,
    $fileImagetype,
    $fileImagesize,
    $uploaddir,
    GetSQLValueString($_POST['plant_name'], "text"),
    GetSQLValueString($_POST['plant_type'], "text"),
    GetSQLValueString($_POST['planting_months'], "text"),
    GetSQLValueString($_POST['blooming_season'], "text"),
    GetSQLValueString($_POST['plant_description'], "text"));

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#26 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 05 September 2006 - 07:25 PM

Thanks for the help.

I had a permissions and directory error after that, but i sorted that one out, but then i get this error and i don't know what this means ...

The file has been uploaded
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\admin\includes\add_plant.php:45) in C:\Program Files\xampp\htdocs\admin\includes\add_plant.php on line 69

BTW....All data is going into db correctly now, although the image still isn't being uploaded/moved.

#27 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 05 September 2006 - 07:34 PM

The problem i now with

header(sprintf("Location: %s", $insertGoTo));

You can't call header once  data has already been output to the browser. In your case you have sent the "Your file has been uploaded message"

I'd remove the message. As you immediately want to go to another page the user wouldn't see it anyway.

Or, instead of header(), output a meta-refresh tag with a delay of a couple of seconds so user can read message before it goes to the nex page.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#28 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 05 September 2006 - 07:41 PM

Thanks again.....I have removed the message, but the page doesn't get forwarded on, and the image still isn't getting uploaded.

    mysql_select_db($database_localhost, $localhost);
    $Result1 = mysql_query($insertSQL, $localhost) or die(mysql_error());
    $insertGoTo = "plants.php";
    if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
    }
    header(sprintf("Location: %s", $insertGoTo));
    exit;


#29 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 05 September 2006 - 07:54 PM

Are you getting an error message still?

The file isn't uploaded because that section of code is still using the non-existent 'image_name' index instead of 'name'

    $uploaddir = '/plants/';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['image_name']);

    if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir)) {
        echo "The file ".  basename( $_FILES['userfile']['image_name']).
        " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }


On reading that bit of code again, you need to change code so that in the event the upload fails you do not update the db.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#30 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 05 September 2006 - 07:58 PM

Thanks once again....I changed the image_name to name, but the code you mentioned is non existent now as i commented it out due to the header problem.

#31 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 05 September 2006 - 08:02 PM

As a minimum you need this to store the uploaded file

$uploaddir = '/plants/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir);

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#32 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 05 September 2006 - 08:07 PM

Changed the layout so that if there is an error then it dies, but it still enters the info into the db, it doesn't mention any error, but the image still isn't in the correct directory :(

    $uploaddir = '\plants';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); 

    if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir)) {


    
    $insertSQL = sprintf("INSERT INTO plants (image_name, image_type, image_size, path, plant_name, plant_type, planting_months, blooming_season, plant_description) VALUES ('%s', '%s', '%s', '%s', %s, %s, %s, %s, %s)",
    $fileImagename,
    $fileImagetype,
    $fileImagesize,
	$uploaddir,
    GetSQLValueString($_POST['plant_name'], "text"),
    GetSQLValueString($_POST['plant_type'], "text"),
    GetSQLValueString($_POST['planting_months'], "text"),
    GetSQLValueString($_POST['blooming_season'], "text"),
    GetSQLValueString($_POST['plant_description'], "text"));

    mysql_select_db($database_localhost, $localhost);
    $Result1 = mysql_query($insertSQL, $localhost) or die(mysql_error());
    $insertGoTo = "plants.php";
    if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
    }
    header(sprintf("Location: %s", $insertGoTo));
    exit;
    } else{
        echo "There was an error uploading the file, please try again!";
    }
} 
?>


#33 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 05 September 2006 - 08:11 PM

Is the uploaddir path ok, or should it be a complete path?

Thanks,
Steve

#34 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 05 September 2006 - 08:18 PM

    if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir)) {

Oops - should be $uploadfile


If that doesn't work, try

    if(move_uploaded_file($_FILES['userfile']['tmp_name'], realpath($uploadfile))) {


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#35 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 05 September 2006 - 08:30 PM

BTW, now you've changed the value of $uploaddir, you will get something like

\plantsflower.jpg

in $uploadfile instead of

/plants/flower.jpg.

Better to use '/' instead of '\' even on Win systems.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#36 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 05 September 2006 - 08:35 PM

Barand....You my friend are a complete legend...a saint of all patient programmers!!!
With a little messing, i got rid of the error statement completely...don't need it, due to the fact that if there is a problem, the script wont go onto the forwarding page anyway, so i will now there is a prob, as the script is just to enter the info into the db anyway, and only i will be doing that!

I can manage the data layout and all that jazz myself for the users end.

So, Thanks once more for the patience and understanding you have with us dunces newbies.

Steve

#37 bm4499

bm4499
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 27 October 2008 - 04:55 AM

Hi All,

Does anyone has an example of the code in the /Connections/localhost.php file?

thanks in advance

bm

#38 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 27 October 2008 - 06:30 AM

Why are you bumping a two year old topic?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users