0

Here is the code I am using for multiple image upload for every entry in the database; id, image, name, date. By using this code, the image name is inserting correctly but I have a problem with the name field. I want to insert the name using $product_name= $_POST['pro_name']; but it only inserts 'array' in name field.

<?php
include('../config.php');
if (isset($_POST['submit'])) {
    $product_name = $_POST['pro_name'];

    $j = 0;     // Variable for indexing uploaded image.
    $target_path = "uploads/";     // Declaring Path for uploaded images.
    for ($i = 0; $i < count($_FILES['file']['name']) && $product_name < count($product_name); $i++) {
        // Loop to get individual element from the array
        $validextensions = array("jpeg", "jpg", "png");      // Extensions which are allowed.
        $ext = explode('.', basename($_FILES['file']['name'][$i]));   // Explode file name from dot(.)
        $file_extension = end($ext); // Store extensions in the variable.
        $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];     // Set the target path with a new name of image.
        $j = $j + 1;      // Increment the number of uploaded images according to the files in array.
        if (($_FILES["file"]["size"][$i] < 100000000)     // Approx. 100kb files can be uploaded.
                && in_array($file_extension, $validextensions)) {

            if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {

                $finel_name = explode('/', $target_path);
                $image_name_final = $finel_name[1];
                $jajsj = "insert into spec_product set image='$image_name_final', name='$product_name'";
                $janson = mysql_query($jajsj) or die(mysql_error());
                // If file moved to uploads folder.
                echo $j . ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
            } else {     //  If File Was Not Moved.
                echo $j . ').<span id="error">please try again!.</span><br/><br/>';
            }
        } else {     //   If File Size And File Type Was Incorrect.
            echo $j . ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
        }
    }
}
?>

2 Answers 2

1

The value is inserting as array because the variable $product_name is not a string but an array. Whenever an array is used where a string was expected e.g.: concatenation of a string or in your case the query statment: insert into spec_product set image='$image_name_final', name='$product_name'"; PHP will automatically convert the array into its default string value "Array".

Make sure that $product_name is not an array but a string which contains name of the product you want to insert in the table.

Regards, Nitin Thakur

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

1 Comment

thanks nitin but i am also using for ** ($i = 0; $i < count($_FILES['file']['name']) && $product_name < count($product_name); $i++) ** in for loop to count the array where i am wrong
0

replace in your code

for ($i = 0; $i < count($_FILES['file']['name']); $i++)
    {

and add this before your query execute

$product_name_final= $product_name[$i];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.