The if statement is not running .It works fine with the tutorial I have followed but when i have implemented it in my code it fails.The error is Undefined index: thumbnailPic,
The HTML code is :
<label >Thumbnail Picture<text>*</text></label><br>
<input type="file" name="thumbnailPic" id="pic"><br>
<label >Original Picture<text>*</text></label><br>
<input type="file" name="originalPic" id="pic"><br>
The PHP code is :
if (is_uploaded_file($_FILES['thumbnailPic']['tmp_name']) != false) {
$spID="NULL";
$Quant=$_POST['quantity'];
$Siz=$_POST['Size'];
$imgfp = fopen($_FILES['thumbnailPic']['tmp_name'],'rb');
$stmt = $connection->prepare("INSERT INTO stitchedproduct(sp_id,quantity,size,p_id,color_id,sp_thumbnail,sp_OriginalPic) VALUES (? ,?, ?, ?,?,?,?)");
$stmt->bindParam(1, $spID);
$stmt->bindParam(2, $Quant);
$stmt->bindParam(3, $Siz);
$stmt->bindParam(4, $ProductID);
$stmt->bindParam(5, $colour);
$stmt->bindParam(6, $imgfp);
$stmt->bindParam(7, $imgfp);
$stmt->execute();
}
else
echo "Error uploading image";
try something like this,may be this code could help you a bit
$image = addslashes(file_get_contents($_FILE['image']['tmp_name'])); //SQL Injection defence!
$image_name = addslashes($_FILES['image']['name']);
$sql = "INSERT INTO `product_images` (`id`, `image`, `image_name`) VALUES ('1', '{$image}', '{$image_name}')";
if (!mysql_query($sql)) { // Error handling
echo "Something went wrong! :(";
}
also ur form must look like something this
<form action="insert_product.php" method="POST" enctype="multipart/form-data">
<label>File: </label><input type="file" name="image" />
<input type="submit" />
</form>
Here is your original code - tested and working.
1) Prompts for filenames
2) Uploads files and adds them to the database.
3) will throw and exception on a database error.
The db connection is PDO not mysqli. I based this on the 'bindParam' function.
Issues with the original code:
1) spelling with variable names.
2) missing PDO::PARAM_LOB for the files.
I have hard coded all the parameters apart from the files.
<?php session_start(); ?>
<?php if (empty($_FILES)): // show the form... ?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Upload images</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<label >Thumbnail Picture<text>*</text></label><br>
<input type="file" name="thumbnailPic" id="thumbpic"><br>
<label >Original Picture<text>*</text></label><br>
<input type="file" name="originalPic" id="origpic"><br>
<input type="submit" />
</form>
</body>
</html>
<?php endif;?>
<?php if (empty($_FILES)) {
exit; // leave this script...
} ?>
<?php // process the input files...
// start file processing...
/* debug */ var_dump($_FILES); // show what we got as files...
// database connection...
$dsn = 'mysql:host=localhost;dbname=testmysql';
$username = 'test';
$password = 'test';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$connection = new PDO($dsn, $username, $password, $options);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (is_uploaded_file($_FILES['thumbnailPic']['tmp_name'])) {
// $spID = "NULL"; i made it auto increment
$Quantity = 3; // $_POST['quantity'];
$Size = 23; //$_POST['Size'];
$ProductID = 'My Test Product1';
$colour = 'yucky green';
$imgThumb = fopen($_FILES['thumbnailPic']['tmp_name'],'rb');
$imgOriginal = fopen($_FILES['originalPic']['tmp_name'],'rb');
$stmt = $connection->prepare("INSERT INTO stitchedproduct(quantity, size, p_id, color_id, sp_thumbnail, sp_OriginalPic) "
. " VALUES (?, ?, ?, ?, ?, ?)");
// $stmt->bindParam(1, $spID); auto increment
$stmt->bindParam(1, $Quantity, PDO::PARAM_INT);
$stmt->bindParam(2, $Size);
$stmt->bindParam(3, $ProductID);
$stmt->bindParam(4, $colour);
$stmt->bindParam(5, $imgThumb, PDO::PARAM_LOB);
$stmt->bindParam(6, $imgOriginal, PDO::PARAM_LOB);
$connection->beginTransaction();
$stmt->execute();
$connection->commit();
}
else
echo "Error uploading image";
unset($connection);
?>