douzhanglu4591 2016-07-26 10:59
浏览 85
已采纳

只是在mysql错误中插入空数组()

I want to store image name only inside mysql table but issue is that it's uploading blank array and giving error

array to string conversion.

if(isset($_POST['prd_submit']) && isset($_FILES['prd_image'])){
// Define Input Variables
    $name = user_input($_POST['prd_name']);
    $detail =  user_input($_POST['prd_detail']);
    $image = $_FILES['prd_image'];
    $buy_link = user_input($_POST['prd_link']);
    $price = user_input($_POST['prd_price']);
    $category = $_POST['prd_category'];
    $country = $_POST['prd_country'];
    // Control Error Inputs
    if(empty($name)){
        $name_err = "Name is missing";
    }
    if(empty($detail)){
        $detail_err = "Detail is missing";
    }
    if(empty($price)){
        $price_err = "Price is missing";
    }
    if(empty($buy_link)){
        $buy_link_err = "Link is missing";
    }
    // File Upload Function
    $OutFiles = array();
    foreach($image as $Index=>$Items){
        foreach($Items as $Key=>$Item){
            $OutFiles[$Key][$Index] = $Item;
        }
    }
    if($OutFiles[0]['error']){
        $image_err = $Errors[$OutFiles[0]['error']];
    }else{
        foreach($OutFiles as $Index=>$File){
            $UploadDir = $DocRoot.'/upload/';
            $imageName = $File['name'];
            //GETTING FILE EXTENTION
            $file_ext = explode('.',$imageName);
            $file_ext = $file_ext[count($file_ext)-1];
            //FILE NAME
            $filename = (rand()).'-'.(time()).'.'.$file_ext;
            //FILE EXTENTION ERROR
            if($file_ext != "jpg" && $file_ext != "png" && $file_ext != "jpeg" && $file_ext != "gif"){
                $error = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            }elseif(move_uploaded_file($File['tmp_name'],$UploadDir.$filename)){
                $OutFiles[$Index]['name'] = $filename;
                $uploadok++;
            }elseif($uploadok == 0){
                $error = "Sorry File is Not Upload";
            }else{
                $uploadok--;
                $error = "Sorry File is Not Upload";
            }
        }
    }
    // Insert DB
    if($name_err == '' && $detail_err == '' && $image_err == '' && $price_err == '' && $buy_link_err == ''){            
        $Code = 0;
        try{
            $insert_data = ("INSERT INTO product (name,country,detail,image,price,buy_link,category,date_posted) VALUES ('$name','$country','$detail','$image','$price','$buy_link','$category','$date')");
            $insert_data = $conn->query($insert_data);
        }catch(PDOException $E){            
            $Code = $E->getCode();
        }
        if($Code == 0){
            $error =  "<div class='alert alert-success'>Your Product Registration Request Has Submitted!</div>";
        }elseif($Code == 23000){
            $error =  "<div class='alert alert-info'>Duplicate Entry</div>";
        }else{
            $error = "Unabel to enter data";
        }
    }

To much confuse what thing i'm doing wrong in it and if implode array but how i can implode i just need name only.

  • 写回答

1条回答 默认 最新

  • dongyin6576 2016-07-26 11:08
    关注

    Change $image To $filename in your INSERT query.

    Because, $image = $_FILES['prd_image']; is an array and you wanted to store the file name which is just uploaded to upload folder. So, use $filename which is uploaded using elseif(move_uploaded_file($File['tmp_name'],$UploadDir.$filename)){

    Query

    $insert_data = "INSERT INTO product (name,country,detail,image,price,buy_link,category,date_posted) VALUES ('$name','$country','$detail','$filename','$price','$buy_link','$category','$date')";
    

    Uploading Multiple File : Move your INSERT Query inside foreach. It will insert into table on every successful upload.

    foreach ($OutFiles as $Index => $File) {
      $UploadDir = $DocRoot . '/upload/';
      $imageName = $File['name'];
      //GETTING FILE EXTENTION
      $file_ext = explode('.', $imageName);
      $file_ext = $file_ext[count($file_ext) - 1];
      //FILE NAME
      $filename = (rand()) . '-' . (time()) . '.' . $file_ext;
      //FILE EXTENTION ERROR
      if ($file_ext != "jpg" && $file_ext != "png" && $file_ext != "jpeg" && $file_ext != "gif") {
        $error = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
      } elseif (move_uploaded_file($File['tmp_name'], $UploadDir . $filename)) {
        $OutFiles[$Index]['name'] = $filename;
        $insert_data = "INSERT INTO product (name,country,detail,image,price,buy_link,category,date_posted) VALUES ('$name','$country','$detail','$filename','$price','$buy_link','$category','$date')";
        $insert_data = $conn->query($insert_data);
        $uploadok++;
      } elseif ($uploadok == 0) {
        $error = "Sorry File is Not Upload";
      } else {
        $uploadok--;
        $error = "Sorry File is Not Upload";
      }
    }
    

    And, remove try/catch from below as now it's INSERTING on every UPLOAD.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源