Jump to content

Recommended Posts

I need help here. I am creating a system where the user will be able to update the product stock by uploading the stock of the products according to the id that has been assigned to the product. 
this is how my products table in  looks like in mysql database

image.png.db556deaee8cdc60b31298a69f2db6b0.png 

I tried the code below but all i could not update my data into my database. And there's not error shown on my code. I do not know what is wrong with my codes. Please help me.
below the code that i use for uploading the CSV file to the data of my product_stock. 

<?php 
include 'conn.php';
if(isset($_POST["add_stock"]))
{
 if($_FILES['product_file']['tmp_name'])
 {
  $filename = explode(".", $_FILES['product_file']['tmp_name']);
  if(end($filename) == "csv")
  {
   $handle = fopen($_FILES['product_file']['tmp_name'], "r");
   while($data = fgetcsv($handle))
   {
    $product_id = mysqli_real_escape_string($conn, $data[0]);
    $product_stock = mysqli_real_escape_string($conn, $data[1]);  
    $product_status = 1 ;
    $query = "UPDATE products SET `product_stock` = '$product_stock', `product_status` = '$product_status' WHERE id = '$product_id'";
    mysqli_query($conn, $query);
   }
   fclose($handle);
   header("location: upload-product.php?updation=1");
  }
  else
  {
   echo '<script>alert("An error occur while uploading product. Please try again.")
   window.location.href = "upload-product.php"</script>';
  }
 }
 else
 {
  echo '<script>alert("No file selected! ")
  window.location.href = "upload-product.php"</script>';
 }
}
if(isset($_GET["updation"]))
{
  echo '<script>alert("Product Stock Updated successfully!")</script>';
}
?>

 <div class="col-12">
            <div class="card card-user">
              <div class="card-header">
                <h5 class="card-title">Update Product Stock</h5>
                <div class="card-body">
                <div class="form-group">
				<label for="file">Update Products stock File (.csv file)</label>
                <a href="assets/templates/product-template.xlsx" title="Download Sample File (Fill In Information and Export As CSV File)" class="mx-2">
				<span class="iconify" data-icon="fa-solid:download" data-inline="false">
				</a>
                </div>
                <form class = "form" action="" method="post" name="uploadCsv" enctype="multipart/form-data">
                <div>
                <input type="file" name="product_file" accept=".csv">
                <div class="row">
                <div class="update ml-auto mr-auto">
                <button type="submit" class="btn btn-primary btn-round" name="add_stock"> Import .cvs file</button>
                </div>
                </div>
                </div>
                </div>
                </form>
                </div>
              </div>

This is the template that i require user to key in and saved it in CSV format before uploading it. 
image.png.9aac2e20961f0449be48122d091e0234.png

$product_stock, $product_status and $product_id are all nemeric so do not put them in side quotes within your query string. Any extra whitespace will prevent a match.

It is more efficient to prepare a query once and bind the parameters then execute it within the loop. You are doing it the slowest way possible.

However, the most efficient way would be to put the data in array then use a single multi-record insert.

    $fp = fopen('myfile.csv', 'r');
    $rec = fgetcsv($fp);                         // header rec
    while ($rec = fgetcsv($fp)) {
        $data[] = vsprintf("(%d, %d)", $rec);    // store in array
    }
    // now insert/update the array data into the table
    $conn->query("INSERT INTO products (id, product_stock) 
                  VALUES " . join(',', $data)
                  . " ON DUPLICATE KEY UPDATE
                         product_stock = VALUES(product_stock)
                       , product_status = 1
                  ");

 

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.