sashavalentina Posted July 11, 2021 Share Posted July 11, 2021 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 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. Quote Link to comment https://forums.phpfreaks.com/topic/313067-error-occurs-while-updating-data-in-mysql-using-csv-file/ Share on other sites More sharing options...
Barand Posted July 11, 2021 Share Posted July 11, 2021 $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 "); 1 Quote Link to comment https://forums.phpfreaks.com/topic/313067-error-occurs-while-updating-data-in-mysql-using-csv-file/#findComment-1587938 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.