Page 1 of 1

How come cannot automatic insert the image into my database?

Posted: Mon May 17, 2004 7:20 am
by teckyan888

Code: Select all

<?php
<?

require ("globals.php");
require ("common.php");


if(isset($_POST["upload"])) 
{ 
$CategoryID = $_POST['CategoryID'];
$ProductName = $_POST['ProductName']; 
$Description = $_POST['Description']; 
$userfile = $_POST['userfile']; 

if(!$CategoryID){ 
echo '<li>You have not entered CategoryID.<br />' 
.'Please try again.<br />'; ReturnToBack();
exit();
}

elseif(!$ProductName){ 
echo '<li>You have not entered Product Name.<br />' 
.'Please try again.<br />'; ReturnToBack();
exit();
}

elseif(!$Description){ 
echo '<li>You have not entered description.<br />' 
.'Please try again.<br />'; ReturnToBack();
exit();
} 

$imgf = $_FILES['userfile']['name']; // name of file in user's machine
$imgt = $_FILES['userfile']['type']; // type of uploaded file
$imgs = $_FILES['userfile']['size']; // size of uploaded file, in bytes
$imgtf = $_FILES['userfile']['tmp_name'];  // name of the file temporarily 
                                           // stored in server
$fileError = $_FILES['userfile']['error']; // error code in uploading file, 
                                           // 0 - success

if ($fileError != UPLOAD_ERROR_OK)
{
    echo 'Error in uploading: ';
    switch ($fileError)
    {
        case UPLOAD_ERR_INI_SIZE:
            echo 'File exceeded upload_max_filesize';
            break;
        case UPLOAD_ERR_FORM_SIZE:
            echo 'File exceeded max_file_size';
            break;

        case UPLOAD_ERR_PARTIAL:
            echo 'File only partially uploaded';
            break;

        case UPLOAD_ERR_NO_FILE:
            echo 'No file uploaded';
            break;
    }
}
else
{  
    print "Uploaded file summary:<br>";
    print "image file name in temp dir: $imgtf <br>";
    print "name on user machine: $imgf <br>";
    print "size: $imgs <br>";
    print "type: $imgt <br>";
    print "dir: $myRD<br>";
    print "error: $fileError<br>";

// $fileDir = "../../cart/".$imgf; // CAUSING ERROR...


// $fileDir = "./".$imgf; // 2 level up? script in htdocs/kokch
                       // image loaded in Apache2/htdocs/kokch

// $fileDir = "imgFolder/".$imgf; // current folder's subfolder! script in htdocs/kokch
                       // image loaded in htdocs/kokch/imgFolder

// $fileDir = ".".$imgf; // 2 level up? script in htdocs/kokch
                       // image loaded in Apache2/htdocs/kokch
                        // BUT with file named .dot7.jpg!!

//$fileDir = "/".$imgf; // 5 level up? script in htdocs/kokch
                       // image loaded in c:/ProgramFiles/ApacheGroup/Apache2/htdocs/kokch

//$fileDir = "../".$imgf; // 3 level up? script in htdocs/kokch
                       // image loaded in ApacheGroup/Apache2/htdocs/kokch

$fileDir = "image/".$imgf; // current folder's sub-subfolder! 
                       // script in htdocs/kokch
                       // image loaded in htdocs/kokch/imgFolder/image 

// move to proper destination...
if (move_uploaded_file($imgtf,$fileDir))
	print "success<br>";
else
	print "failed<br>";
}

//This connects to the mysql server and selects the databse 

$con = mysql_connect($hostName, $userName, $password); 
$db = $databaseName; 
mysql_select_db($db, $con)or die("There was an error connecting to the database. Please send the following information to the site administrator.<br /><br />".mysql_error()); 

//This inserts the information in to the database 

$sql = "insert into Product(CategoryID, ProductName, Description, userfile) values ('$CategoryID','$ProductName', '$Description', '$userfile')"; 

mysql_query($sql)or die("There was an error adding your information to the database. Please send the following information to the site administrator.<br /><br />".mysql_error());

echo 'You are successful to insert new picture in your product!<br />';

//This closed the connection to the database 
mysql_close($con); 
} 

print "<h1>done...</h1>";

?>

?>
I already tried it many time but the userfile also cannot insert into my database...Actually everything can insert except the userfile only...Anyone who know how to solve it? ...Thanks....

Posted: Mon May 17, 2004 8:03 am
by leenoble_uk
You can't put an image into a mysql database like this.
If you *really* want to put your image in the db it will need to be a BLOB field and you would put the $_FILES['userfile']['tmp_name'] into it I think although in all honesty I can't remember since I don't keep images in the db any more.
It is generally preferable to move your uploaded file to another directory and put the path to this image in your database:

Code: Select all

move_uploaded_file($_FILES['userfile']['tmp_name'],"/path/to/writeable/directory/in/your/website/$filenameOfYourChoice");
and then just put the $fileNameOfYourChoice in the database then you can just put:

Code: Select all

<img src="/path/to/writeable/directory/in/your/website/<?php echo $fileNameOfYourChoice; ?>">
edit: it won't let me put the end PHP tag (questionmark-right angle bracket) within the image tag for some reason.

Posted: Mon May 17, 2004 9:53 am
by pickle
Look here for how to read in and read out images from MySQL:

viewtopic.php?p=107457