Home > Net >  I'm trying to upload an image into mysql database with PHP, but its not going through
I'm trying to upload an image into mysql database with PHP, but its not going through

Time:12-06

here is my input table code

<form action="actions/insert.php" method="POST" enctype="multipart/form-data">
    
      <div class="mb-3">
        <label for="exampleFormControlInput1" class="form-label">Product Name</label>
        <input type="text" name="product_name" class="form-control" id="exampleFormControlInput1">
      </div>

      <div class="mb-3">
        <label for="exampleFormControlTextarea1" class="form-label">Product Short Description</label>
        <textarea class="form-control" name="short_description" style="height: 200px; resize: none;"></textarea>
      </div>

      <div class="row">
        <div class="mb-3 col s4">
          <label for="exampleFormControlInput1" class="form-label">Product Price</label>
          <input type="number" class="form-control" name="product_price" id="exampleFormControlInput1">
        </div>

        <div class="mb-3 col s4">
          <label for="exampleFormControlInput1" class="form-label">Discounted Price</label>
          <input type="number" class="form-control" name="discount_price" id="exampleFormControlInput1">
        </div>

        <div class="mb-3 col s4">
          <label for="exampleFormControlInput1" class="form-label">Stock Quantity</label>
          <input type="number" class="form-control" name="stock_quantity" id="exampleFormControlInput1">
        </div>
      </div>

      <div class="row">
          <div class="mb-4 col s4">
            <label for="exampleFormControlInput1" class="form-label">Product Brand</label>
              <select class="form-select" name="brand" aria-label="Default select example">
                <option selected>Select Brand</option>
                <option value="LG">LG</option>
                <option value="Hisense">Hisense</option>
                <option value="Haier Thermocool">Haier Thermocool</option>
              </select><br>
          </div>

          <div class="mb-4 col s4">
            <label for="exampleFormControlInput1" class="form-label">Product Categories</label>
              <select class="form-select" name="categories" aria-label="Default select example">
                <option selected>Select Category</option>
                <option value="Men's Wears">Men's Wears</option>
                <option value="Women's Wears">Women's Wears</option>
                <option value="Watches">Watches</option>
              </select><br>
          </div>

          <div class="mb-4 col s4">
            <label for="exampleFormControlInput1" class="form-label">Product Tags</label>
              <select class="form-select" name="tags" aria-label="Default select example">
                <option selected>Select Tag</option>
                <option value="Clothings">Clothings</option>
                <option value="Smart Watches">Smart Watches</option>
                <option value="Home Appliances">Home Appliances</option>
              </select><br>
          </div>
      </div>

      <div class="form-group">
        <label for="exampleFormControlInput1" class="form-label">Upload Product Image</label>
        <input type="file" name="cover_image" class="form-control">
      </div>

      <div class="mb-3">
        <label for="exampleFormControlTextarea1" class="form-label">Product Description</label>
        <textarea class="form-control" id="editor1" name="full_description" rows="3"></textarea>
      </div>

      <button class="btn btn-primary" type="submit" name="add_product">Publish</button>

  </form>

And below is my insert query code

if (isset($_POST['add_product'])) {
//Get User Id
$user = $_SESSION['user_email'];
$DB->select("users","*","user_email = '$user'");
$result = $DB->sql;

foreach ($result as $user_id) 
{
    $id = $user_id['user_id'];
}

$product_id         = rand(time(), 100000000);
$user_id            = $id;
$product_name       = mysqli_real_escape_string($DB->mysqli, $_POST['product_name']);
$short_description  = mysqli_real_escape_string($DB->mysqli, $_POST['short_description']);
$full_description   = mysqli_real_escape_string($DB->mysqli, $_POST['full_description']);
$product_price      = mysqli_real_escape_string($DB->mysqli, $_POST['product_price']);
$discount_price     = mysqli_real_escape_string($DB->mysqli, $_POST['discount_price']);
$brand              = mysqli_real_escape_string($DB->mysqli, $_POST['brand']);
$categories         = mysqli_real_escape_string($DB->mysqli, $_POST['categories']);
$tags               = mysqli_real_escape_string($DB->mysqli, $_POST['tags']);
$stock_quantity     = mysqli_real_escape_string($DB->mysqli, $_POST['stock_quantity']);
$on_sale            = 1;
$cover_image        = $_FILES['cover_image']['name'];

$image_extension = pathinfo($cover_image, PATHINFO_EXTENSION);
$image_rand = rand(time(), 100000000);
$image_rename = 'prod_'.date('Y/m/d')."_".$image_rand; // Generate random image name

$image_new_name = $image_rename.".".$image_extension; // Newly generated image name with file extension
$new_location = "../images/products/".$image_new_name; //Product Cover image upload path
$image_tmp = $_FILES['cover_image']['tmp_name'];

$date = date("Y-m-d h:i:s A");

$move_uploaded_file = move_uploaded_file($image_tmp, $new_location);
        
if ($move_uploaded_file) 
    {
        $DB->insert('products',[

                'product_id'=>$product_id,
                'user_id'=>$user_id,
                'product_name'=>$product_name,
                'product_short_descrip'=>$short_description,
                'product_description'=>$full_description,
                'normal_price'=>$product_price,
                'discount_price'=>$discount_price,
                'on_sale'=>$on_sale,
                'stock_quantity'=>$stock_quantity,
                'brand'=>$brand,
                'categories'=>$categories,
                'tags'=>$tags,
                'product_image'=>$image_new_name,
                'date_added'=>$date
            ]);

        if ($DB == true) 
            {
                $_SESSION['success'] = "Product added Successfully.";
                header('location:../all-products.php');
            }
        else 
            {
                $_SESSION['success'] = "Operation failed.";
                header('location:../all-products.php');
            }
    }
else
    {
        $_SESSION['error'] = "Image upload failed. Please try again.";
        header('location:../new-products.php');
    }

}

The problem now is that all the data got inserted into the database, but the uploaded image doesn't get moved to the destination folder. When I used the print_r() funtion to chech the $move_uploaded_file variable, here is the response I receive from the system:

Warning: move_uploaded_file(../images/products/prod_2021/12/05_819609249.jpg): Failed to open stream: No such file or directory in C:\xampp\htdocs\my-shop\admin\actions\insert.php on line 113

Warning: move_uploaded_file(): Unable to move "C:\xampp\tmp\phpA94.tmp" to "../images/products/prod_2021/12/05_819609249.jpg" in C:\xampp\htdocs\my-shop\admin\actions\insert.php on line 113

But I'm pretty sure that the file path is correct and I even tried it with another file, but I still receive thesame response. The part that gives me most concern is that the code works well for other form, but doesn.t work on this particular one.

I'm confused. I've been on this for three days pls. I'm out of ideas.

Thanks

CodePudding user response:

The slashes in your renamed filename are causing problem. (it makes the system thinks that there are further sub-directories, which may not (and most likely does not) actually exist).

If there is NO directory structure in place and you have not explicitly instructed to "make directory", it is impossible to save the target file into the target directory)

Hence, change your line

$image_rename = 'prod_'.date('Y/m/d')."_".$image_rand; 

to

$image_rename = 'prod_'.date('Y_m_d')."_".$image_rand; 
  • Related