How come cannot automatic insert the image into my database?
Posted: Mon May 17, 2004 7:20 am
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>";
?>
?>