Jump to content


Photo

Help with PHP script please


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

#1 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 04 September 2006 - 05:36 PM

Hi all,

Please could somebody help me out with this script.

I want it to upload all the details into the db, and the image into a folder, storing the image name, size and path on server into db.

For some reason though, it doesn't upload the image, or store any of the image details in db, just the other details.

Here is the code...

<?php require_once('../../Connections/localhost.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
$target_path = "../plants/";

$target_path = $target_path . basename( $_FILES['userfile']['image_name']); 

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['userfile']['image_name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
if(isset($_POST['upload']))
{
$fileImagename = $_FILES['userfile']['image_name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileImagesize = $_FILES['userfile']['image_size'];
$fileImagetype = $_FILES['userfile']['image_type'];
$filePlantname = $_POST['userfile']['plant_name'];
$filePlanttype = $_POST['userfile']['plant_type'];
$filePlantmonth = $_POST['userfile']['planting_months'];
$fileBloomseason = $_POST['userfile']['blooming_season'];
$filePlantdesc = $_POST['userfile']['plant_description'];
}

$filePath = $uploadDir . $fileImagename;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}


if(!get_magic_quotes_gpc())
{
$fileImagename = addslashes($fileImagename);
$filePath = addslashes($filePath);
} 
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "upload")) {
  $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)",
                       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));
}


?>

<?php
mysql_select_db($database_localhost, $localhost);
$query_Recordset1 = "SELECT plant_name, plant_type, planting_months, blooming_season, plant_description FROM plants";
$Recordset1 = mysql_query($query_Recordset1, $localhost) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<link href="../../css/stylesheet.css" rel="stylesheet" type="text/css">
<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="upload" id="upload">
  <p align="center"><span class="bottomnav">Plant Name</span><br>
    <input name="plant_name" type="text" class="logintext" id="plant_name" size="20" maxlength="50">
  </p>
  <p align="center"><span class="bottomnav">Plant Type </span><br>
    <input name="plant_type" type="text" class="logintext" id="plant_type" size="20" maxlength="50">
  </p>
  <p align="center"><span class="bottomnav">Planting Month(s)</span><br>
    <input name="planting_months" type="text" class="logintext" id="planting_months" size="20" maxlength="20">
</p>
  <p align="center"><span class="bottomnav">Blooming Season</span><br>
    <input name="blooming_season" type="text" class="logintext" id="blooming_season" size="20" maxlength="20">
</p>
  <p align="center"><span class="bottomnav">Plant Description</span><br>
    <textarea name="plant_description" cols="40" rows="6" class="logintext" id="plant_description"></textarea>
  </p>
  <p align="center"><span class="bottomnav">Image</span><br>
    <input name="userfile" type="file" class="loginbutton" id="userfile">
  </p>
  <p align="center">
    <input name="Submit" type="submit" class="loginbutton" value="Submit">
  </p>
  <input type="hidden" name="MM_insert" value="upload">
</form>
<?php
mysql_free_result($Recordset1);
?>

Thanks in advance all,
Steve

#2 Barand

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

Posted 04 September 2006 - 07:11 PM

$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)",
                      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"));


The above insert statement has no mention of the uploaded file details
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

#3 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 04 September 2006 - 07:16 PM

Biut they are stated above with the file pointers....How can i correct this?

thanks for help & reply :)

#4 Barand

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

Posted 04 September 2006 - 07:32 PM

<?php
$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,
    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

#5 Barand

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

Posted 04 September 2006 - 07:45 PM

Also,

These lines
$filePlantname = $_POST['userfile']['plant_name'];
$filePlanttype = $_POST['userfile']['plant_type'];
$filePlantmonth = $_POST['userfile']['planting_months'];
$fileBloomseason = $_POST['userfile']['blooming_season'];
$filePlantdesc = $_POST['userfile']['plant_description'];

should be
$filePlantname = $_POST['plant_name'];
$filePlanttype = $_POST['plant_type'];
$filePlantmonth = $_POST['planting_months'];
$fileBloomseason = $_POST['blooming_season'];
$filePlantdesc = $_POST['plant_description'];

But as tou never refer to those variables you may as well remove the lines
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

#6 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 04 September 2006 - 08:00 PM

Many Thanks for the reply mate,

Now it seems that it isn't even showing the form, just comes up with the error, "There was an error uploading the file, please try again!Error uploading file"
Think i've got something in the wrong place now :(

Here is the code now...

<?php require_once('../../Connections/localhost.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$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!";
}
if(isset($_POST['upload']))
{
$fileImagename = $_FILES['userfile']['image_name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileImagesize = $_FILES['userfile']['image_size'];
$fileImagetype = $_FILES['userfile']['image_type'];
$filePlantname = $_POST['plant_name'];
$filePlanttype = $_POST['plant_type'];
$filePlantmonth = $_POST['planting_months'];
$fileBloomseason = $_POST['blooming_season'];
$filePlantdesc = $_POST['plant_description'];
}

$filePath = $uploadDir . $fileImagename;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}


if(!get_magic_quotes_gpc())
{
$fileImagename = addslashes($fileImagename);
$filePath = addslashes($filePath);
} 
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "upload")) {
$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,
    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));

?>

<?php
mysql_select_db($database_localhost, $localhost);
$query_Recordset1 = "SELECT plant_name, plant_type, planting_months, blooming_season, plant_description FROM plants";
$Recordset1 = mysql_query($query_Recordset1, $localhost) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<link href="../../css/stylesheet.css" rel="stylesheet" type="text/css">
<form method="POST" enctype="multipart/form-data" name="upload" id="upload">
  <p align="center"><span class="bottomnav">Plant Name</span><br>
    <input name="plant_name" type="text" class="logintext" id="plant_name" size="20" maxlength="50">
  </p>
  <p align="center"><span class="bottomnav">Plant Type </span><br>
    <input name="plant_type" type="text" class="logintext" id="plant_type" size="20" maxlength="50">
  </p>
  <p align="center"><span class="bottomnav">Planting Month(s)</span><br>
    <input name="planting_months" type="text" class="logintext" id="planting_months" size="20" maxlength="20">
</p>
  <p align="center"><span class="bottomnav">Blooming Season</span><br>
    <input name="blooming_season" type="text" class="logintext" id="blooming_season" size="20" maxlength="20">
</p>
  <p align="center"><span class="bottomnav">Plant Description</span><br>
    <textarea name="plant_description" cols="40" rows="6" class="logintext" id="plant_description"></textarea>
  </p>
  <p align="center"><span class="bottomnav">Image</span><br>
    <input name="userfile" type="file" class="loginbutton" id="userfile">
  </p>
  <p align="center">
    <input name="Submit" type="submit" class="loginbutton" value="Submit">
  </p>
  
</form>
<?php
mysql_free_result($Recordset1);
?>

Thanks again,
Steve

#7 Barand

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

Posted 04 September 2006 - 08:10 PM

All processing except form display should be inside

if(isset($_POST['upload']))
{
          // processing code here
}

form code here
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

#8 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 04 September 2006 - 08:17 PM

So it is mean't to be like this...

if(isset($_POST['upload']))
{
$fileImagename = $_FILES['userfile']['image_name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileImagesize = $_FILES['userfile']['image_size'];
$fileImagetype = $_FILES['userfile']['image_type'];
$filePlantname = $_POST['plant_name'];
$filePlanttype = $_POST['plant_type'];
$filePlantmonth = $_POST['planting_months'];
$fileBloomseason = $_POST['blooming_season'];
$filePlantdesc = $_POST['plant_description'];
} ?>
<link href="../../css/stylesheet.css" rel="stylesheet" type="text/css">
<form method="POST" enctype="multipart/form-data" name="upload" id="upload">
  <p align="center"><span class="bottomnav">Plant Name</span><br>
    <input name="plant_name" type="text" class="logintext" id="plant_name" size="20" maxlength="50">
  </p>
  <p align="center"><span class="bottomnav">Plant Type </span><br>
    <input name="plant_type" type="text" class="logintext" id="plant_type" size="20" maxlength="50">
  </p>
  <p align="center"><span class="bottomnav">Planting Month(s)</span><br>
    <input name="planting_months" type="text" class="logintext" id="planting_months" size="20" maxlength="20">
</p>
  <p align="center"><span class="bottomnav">Blooming Season</span><br>
    <input name="blooming_season" type="text" class="logintext" id="blooming_season" size="20" maxlength="20">
</p>
  <p align="center"><span class="bottomnav">Plant Description</span><br>
    <textarea name="plant_description" cols="40" rows="6" class="logintext" id="plant_description"></textarea>
  </p>
  <p align="center"><span class="bottomnav">Image</span><br>
    <input name="userfile" type="file" class="loginbutton" id="userfile">
  </p>
  <p align="center">
    <input name="Submit" type="submit" class="loginbutton" value="Submit">
  </p>
  
</form>
<?php
$filePath = $uploadDir . $fileImagename;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}


if(!get_magic_quotes_gpc())
{
$fileImagename = addslashes($fileImagename);
$filePath = addslashes($filePath);
} 
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "upload")) {
$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,
    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));

?>
Thanks again,
Steve

#9 Barand

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

Posted 04 September 2006 - 08:21 PM

That bit's fine. It's the code above that that gives the error because, until you submit the form there is no uploaded file.

So
$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!";
}

also needs to be inside that if() condition
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

#10 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 04 September 2006 - 08:30 PM

Ok, so i have now got my form back :) Yey....Thanks!!

But, it comes up with 'error uploading file', and nothing goes into the database, or the folder  ::)
Sorry for being a pain :)

Steve

<?php require_once('../../Connections/localhost.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}


if(isset($_POST['upload']))
{
$fileImagename = $_FILES['userfile']['image_name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileImagesize = $_FILES['userfile']['image_size'];
$fileImagetype = $_FILES['userfile']['image_type'];
$filePlantname = $_POST['plant_name'];
$filePlanttype = $_POST['plant_type'];
$filePlantmonth = $_POST['planting_months'];
$fileBloomseason = $_POST['blooming_season'];
$filePlantdesc = $_POST['plant_description'];

$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!";
}
} 
?>
<html>
<link href="../../css/stylesheet.css" rel="stylesheet" type="text/css">
<form method="POST" enctype="multipart/form-data" name="upload" id="upload">
  <p align="center"><span class="bottomnav">Plant Name</span><br>
    <input name="plant_name" type="text" class="logintext" id="plant_name" size="20" maxlength="50">
  </p>
  <p align="center"><span class="bottomnav">Plant Type </span><br>
    <input name="plant_type" type="text" class="logintext" id="plant_type" size="20" maxlength="50">
  </p>
  <p align="center"><span class="bottomnav">Planting Month(s)</span><br>
    <input name="planting_months" type="text" class="logintext" id="planting_months" size="20" maxlength="20">
</p>
  <p align="center"><span class="bottomnav">Blooming Season</span><br>
    <input name="blooming_season" type="text" class="logintext" id="blooming_season" size="20" maxlength="20">
</p>
  <p align="center"><span class="bottomnav">Plant Description</span><br>
    <textarea name="plant_description" cols="40" rows="6" class="logintext" id="plant_description"></textarea>
  </p>
  <p align="center"><span class="bottomnav">Image</span><br>
    <input name="userfile" type="file" class="loginbutton" id="userfile">
  </p>
  <p align="center">
    <input name="Submit" type="submit" class="loginbutton" value="Submit">
  </p>
  
</form>
</html>
<?php

$filePath = $uploadDir . $fileImagename;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}


if(!get_magic_quotes_gpc())
{
$fileImagename = addslashes($fileImagename);
$filePath = addslashes($filePath);
} 
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "upload")) {
$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,
    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));

?>

<?php
mysql_select_db($database_localhost, $localhost);
$query_Recordset1 = "SELECT plant_name, plant_type, planting_months, blooming_season, plant_description FROM plants";
$Recordset1 = mysql_query($query_Recordset1, $localhost) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>

<?php
mysql_free_result($Recordset1);
?>


#11 Barand

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

Posted 04 September 2006 - 09:12 PM

try
<?php 
require_once('../../Connections/localhost.php'); 

if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
}


if(isset($_POST['upload']))
{
    $fileImagename = $_FILES['userfile']['name'];
    $fileImagesize = $_FILES['userfile']['size'];
    $fileImagetype = $_FILES['userfile']['type'];

    $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!";
    }
    
    $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,
    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;

} 
?>
<html>
<link href="../../css/stylesheet.css" rel="stylesheet" type="text/css">
<form method="POST" enctype="multipart/form-data" name="upload" id="upload">
  <p align="center"><span class="bottomnav">Plant Name</span><br>
    <input name="plant_name" type="text" class="logintext" id="plant_name" size="20" maxlength="50">
  </p>
  <p align="center"><span class="bottomnav">Plant Type </span><br>
    <input name="plant_type" type="text" class="logintext" id="plant_type" size="20" maxlength="50">
  </p>
  <p align="center"><span class="bottomnav">Planting Month(s)</span><br>
    <input name="planting_months" type="text" class="logintext" id="planting_months" size="20" maxlength="20">
</p>
  <p align="center"><span class="bottomnav">Blooming Season</span><br>
    <input name="blooming_season" type="text" class="logintext" id="blooming_season" size="20" maxlength="20">
</p>
  <p align="center"><span class="bottomnav">Plant Description</span><br>
    <textarea name="plant_description" cols="40" rows="6" class="logintext" id="plant_description"></textarea>
  </p>
  <p align="center"><span class="bottomnav">Image</span><br>
    <input name="userfile" type="file" class="loginbutton" id="userfile">
  </p>
  <p align="center">
    <input name="Submit" type="submit" class="loginbutton" value="Submit">
  </p>
  
</form>
</html>

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

#12 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 04 September 2006 - 11:16 PM

No good mate. I don't get the error anymore, but nothing else happens either :(

Steve

#13 Barand

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

Posted 04 September 2006 - 11:24 PM

Just noticed you don;t post anything named "upload"

Change
if(isset($_POST['upload']))
to
if(isset($_POST['Submit']))
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

#14 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 05 September 2006 - 04:03 PM

Thanks for all the help mate!

Got a slight problem....The file has been uploadedUnknown column 'flower.jpg' in 'field list'......And still nothing gets uploaded, either into the folder, or the database.

<?php 
require_once('../../Connections/localhost.php'); 

if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
}


if(isset($_POST['Submit']))
{
    $fileImagename = $_FILES['userfile']['name'];
    $fileImagesize = $_FILES['userfile']['size'];
    $fileImagetype = $_FILES['userfile']['type'];

    $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!";
    }
    
    $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,
	GetSQLValueString($_POST['path'], "text"),
    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;

} 
?>
<html>
<link href="../../css/stylesheet.css" rel="stylesheet" type="text/css">
<form method="POST" enctype="multipart/form-data" name="upload" id="upload">
  <p align="center"><span class="bottomnav">Plant Name</span><br>
    <input name="plant_name" type="text" class="logintext" id="plant_name" size="20" maxlength="50">
  </p>
  <p align="center"><span class="bottomnav">Plant Type </span><br>
    <input name="plant_type" type="text" class="logintext" id="plant_type" size="20" maxlength="50">
  </p>
  <p align="center"><span class="bottomnav">Planting Month(s)</span><br>
    <input name="planting_months" type="text" class="logintext" id="planting_months" size="20" maxlength="20">
</p>
  <p align="center"><span class="bottomnav">Blooming Season</span><br>
    <input name="blooming_season" type="text" class="logintext" id="blooming_season" size="20" maxlength="20">
</p>
  <p align="center"><span class="bottomnav">Plant Description</span><br>
    <textarea name="plant_description" cols="40" rows="6" class="logintext" id="plant_description"></textarea>
  </p>
  <p align="center"><span class="bottomnav">Image</span><br>
    <input name="userfile" type="file" class="loginbutton" id="userfile">
  </p>
  <p align="center">
    <input name="Submit" type="submit" class="loginbutton" value="Submit">
  </p>
  
</form>
</html>

Sorry to be a pain,
Steve

#15 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 05 September 2006 - 04:12 PM

Ok, by changing
$fileImagename = $_FILES['userfile']['name'];
    $fileImagesize = $_FILES['userfile']['size'];
    $fileImagetype = $_FILES['userfile']['type'];
to
    $fileImagename = $_FILES['userfile']['image_name'];
    $fileImagesize = $_FILES['userfile']['image_size'];
    $fileImagetype = $_FILES['userfile']['image_type'];
I got rid of the error, but now i have an sql error because i don't think it is storing the path, image_name, image_size, or image_type.

How can i change this?

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 ' , , NULL, 'info', 'info', 'info', 'info', 'info')' at line 1

#16 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 05 September 2006 - 05:41 PM

Been looking and messing now and still can't fatham it out.
God, i've got a lot to learn still lol

#17 Barand

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

Posted 05 September 2006 - 06:41 PM

The FILES array has indexes name, type, size and not image_name etc, so those need changing back.

The problem was the sprintf() statement - all those %s need to be inside quotes otherwise, if SQL comes across a string value, like flower.jpg, ot assumes it is a column name whereas if it is 'flower.jpg' it assumes it is data.

try this
<?php 
require_once('../../Connections/localhost.php'); 

if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
}


if(isset($_POST['upload']))
{
    $fileImagename = $_FILES['userfile']['name'];
    $fileImagesize = $_FILES['userfile']['size'];
    $fileImagetype = $_FILES['userfile']['type'];

    $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!";
    }
    
    $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,
    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;

} 
?>
<html>
<link href="../../css/stylesheet.css" rel="stylesheet" type="text/css">
<form method="POST" enctype="multipart/form-data" name="upload" id="upload">
  <p align="center"><span class="bottomnav">Plant Name</span><br>
    <input name="plant_name" type="text" class="logintext" id="plant_name" size="20" maxlength="50">
  </p>
  <p align="center"><span class="bottomnav">Plant Type </span><br>
    <input name="plant_type" type="text" class="logintext" id="plant_type" size="20" maxlength="50">
  </p>
  <p align="center"><span class="bottomnav">Planting Month(s)</span><br>
    <input name="planting_months" type="text" class="logintext" id="planting_months" size="20" maxlength="20">
</p>
  <p align="center"><span class="bottomnav">Blooming Season</span><br>
    <input name="blooming_season" type="text" class="logintext" id="blooming_season" size="20" maxlength="20">
</p>
  <p align="center"><span class="bottomnav">Plant Description</span><br>
    <textarea name="plant_description" cols="40" rows="6" class="logintext" id="plant_description"></textarea>
  </p>
  <p align="center"><span class="bottomnav">Image</span><br>
    <input name="userfile" type="file" class="loginbutton" id="userfile">
  </p>
  <p align="center">
    <input name="Submit" type="submit" class="loginbutton" value="Submit">
  </p>
  
</form>
</html>

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

#18 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 05 September 2006 - 06:47 PM

Thanks for that, although it still returns that there are too few arguments in the sprintf function. Is this because there is no information being collected about the image size, path, name etc?

Thanks,
Steve

#19 Barand

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

Posted 05 September 2006 - 06:54 PM

image_name, image_type, image_size, path, plant_name, plant_type, planting_months, blooming_season, plant_description

= 9 fields.

    $fileImagename,
    $fileImagetype,
    $fileImagesize,
    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")

= 8 fields

value missing for path - needs to go after $fileImagesize.

Is it $uploaddir that you want in there?


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

#20 Stevis2002

Stevis2002
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 05 September 2006 - 06:56 PM

Yes, so i would have to add a
$uploaddir
?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users