在MySQL数据库PHP中上传图片错误 [英] Uploading Image in MySQL database PHP Error

查看:79
本文介绍了在MySQL数据库PHP中上传图片错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if语句未运行.在我遵循的教程中正常运行,但是当我在代码中实现它时失败了.错误是未定义的索引:thumbnailPic, HTML代码是:

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>

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

推荐答案

这是您的原始代码-经过测试,可以正常工作.

Here is your original code - tested and working.

1)提示输入文件名 2)上传文件并将其添加到数据库. 3)会因数据库错误而引发异常.

1) Prompts for filenames 2) Uploads files and adds them to the database. 3) will throw and exception on a database error.

数据库连接是PDO,而不是mysqli.我基于"bindParam"功能.

The db connection is PDO not mysqli. I based this on the 'bindParam' function.

发出原始代码: 1)用变量名拼写. 2)文件缺少PDO :: PARAM_LOB.

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

这篇关于在MySQL数据库PHP中上传图片错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆