Jump to content

upload multiple images in mysql not working


twelvecodespent

Recommended Posts

I've looked around at a bunch of sample code and cannot seem to get this to work. I'm trying to create a table with the values provided in a form along with an uploaded image. Here is what I have.

 

<?php
//Set variables from form
$title = $_POST["title_textfield"];
$description = $_POST["description_textbox"];
$price = $_POST["price_textfield"];
$time = date("ymdHis");
$me = $myuserID

/Create Table
$con = mysql_connect("mydatabase.mysql.com","databaseAdmin","Adminpass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }


//check if table exists 

// Create table
$time = date("ymdHis");
$tablename = "1_$me$time";
mysql_select_db("db_01", $con);
$sql = "CREATE TABLE $tablename
(
ID int NOT NULL AUTO_INCREMENT, 
imageData LONGBLOB NOT NULL ,
PRIMARY KEY(comicID),
Owner varchar(15),
Status varchar(15),
AgeRestriction varchar(25),
Title varchar(25),
Description varchar(100),
Price varchar(15)
)";

// Execute query
mysql_query($sql,$con);
//input info into table
mysql_query("INSERT INTO $tablename (Owner, Status, Title, Description, Price)
VALUES ('$myid', 'Pending', '$title', '$description', '$price')");

//Image control!!!!
$maxFileSize = "2000000"; // 2 MB file size
//Initializing the image types. 
$image_array = array("image/jpeg","image/jpg","image/gif","image/bmp","image/pjpeg","image/png"); // valid image type

//store the file type of the uploading file.
$fileType = $_FILES['userfile']['type'];
$nname= $_FILES['userfile']['name'];
echo "$nname";
//Initializing the  msg variable. Although , not necessary. 
$msg = ""; 
     

// if Submit button is clicked
if(@$_POST['Submit'])
{
// check for the image type , before uploading
if (in_array($fileType, $image_array)) 
{

// check whether the file is uploaded
if(is_uploaded_file($_FILES['userfile']['tmp_name']))
{

// check whether the file size is below 2 mb
        if($_FILES['userfile']['size'] < $maxFileSize)
            {

// reading the image data
                  $imageData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));

// inserting into the database
        $sql = "INSERT INTO $tablename (imageData) VALUES ('$imageData')";

              
        mysql_query($sql) or die(mysql_error());
                $msg = " Data successfully uploaded";
         }
     else 
         {
     
             $msg = " Error :  File size exceeds the maximum limit ";
                 
             }
   }
}
else
{
$msg = "Error: Not a valid image ";
}

}
mysql_close($con);
?>

 

All the fields populate just fine except the imageData field which shows "[bLOB - 0 Bytes]" Any help would be GREATLY appreciated. Thank you for even taking the time to look.

 

Cordially,

TcS

Try Adding:

  $imageData = file_get_contents($_FILES['userfile']['tmp_name']);

  $imageData = mysql_real_escape_string($imageData );

<?php
//Set variables from form
$title = $_POST["title_textfield"];
$description = $_POST["description_textbox"];
$price = $_POST["price_textfield"];
$time = date("ymdHis");
$me = $myuserID

/Create Table
$con = mysql_connect("mydatabase.mysql.com","databaseAdmin","Adminpass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }


//check if table exists 

// Create table
$time = date("ymdHis");
$tablename = "1_$me$time";
mysql_select_db("db_01", $con);
$sql = "CREATE TABLE $tablename
(
ID int NOT NULL AUTO_INCREMENT, 
imageData LONGBLOB NOT NULL ,
PRIMARY KEY(comicID),
Owner varchar(15),
Status varchar(15),
AgeRestriction varchar(25),
Title varchar(25),
Description varchar(100),
Price varchar(15)
)";

// Execute query
mysql_query($sql,$con);
//input info into table
mysql_query("INSERT INTO $tablename (Owner, Status, Title, Description, Price)
VALUES ('$myid', 'Pending', '$title', '$description', '$price')");

//Image control!!!!
$maxFileSize = "2000000"; // 2 MB file size
//Initializing the image types. 
$image_array = array("image/jpeg","image/jpg","image/gif","image/bmp","image/pjpeg","image/png"); // valid image type

//store the file type of the uploading file.
$fileType = $_FILES['userfile']['type'];
$nname= $_FILES['userfile']['name'];
echo "$nname";
//Initializing the  msg variable. Although , not necessary. 
$msg = ""; 
     

// if Submit button is clicked
if(@$_POST['Submit'])
{
// check for the image type , before uploading
if (in_array($fileType, $image_array)) 
{

// check whether the file is uploaded
if(is_uploaded_file($_FILES['userfile']['tmp_name']))
{

// check whether the file size is below 2 mb
        if($_FILES['userfile']['size'] < $maxFileSize)
            {

// reading the image data
                  $imageData = file_get_contents($_FILES['userfile']['tmp_name']);
                  $imageData = mysql_real_escape_string($imageData );

// inserting into the database
        $sql = "INSERT INTO $tablename (imageData) VALUES ('$imageData')";

              
        mysql_query($sql) or die(mysql_error());
                $msg = " Data successfully uploaded";
         }
     else 
         {
     
             $msg = " Error :  File size exceeds the maximum limit ";
                 
             }
   }
}
else
{
$msg = "Error: Not a valid image ";
}

}
mysql_close($con);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.