0

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.

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

it's update file name inside database but only one image name as i have uploaded upto 10
I didn't get it. Please elaborate once @MuhammadHamzaNisar
it is updating now only one image name. But i'm uploading multiple image and it's inserting only one image name inside database
Wait for 1-2 minutes. That's what I asked in comment. @MuhammadHamzaNisar
thanks for your i convert filename to an array and then implode this list with comma
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.