在MySQL数据库中上传图像PHP错误

时间:2014-03-21 09:27:22

标签: php mysqli

if语句没有运行。我使用的教程工作正常但是当我在我的代码中实现它失败时。错误是Undefined index:thumbnailPic, HTML代码是:

<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>

PHP代码是:

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";

2 个答案:

答案 0 :(得分:0)

尝试这样的事情,可能这段代码可以帮到你一点

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

也是你的形式必须看起来像这个

<form action="insert_product.php" method="POST" enctype="multipart/form-data">
    <label>File: </label><input type="file" name="image" />
    <input type="submit" />
</form>

答案 1 :(得分:0)

这是您的原始代码 - 经过测试和运行。

1)提示文件名 2)上传文件并将其添加到数据库中。 3)将抛出异常数据库错误。

数据库连接是PDO而不是mysqli。我基于'bindParam'函数。

原始代码的问题: 1)用变量名拼写。 2)缺少文件的PDO :: PARAM_LOB。

我已经硬编码了除文件之外的所有参数。

<?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);
?>