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.

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

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试