PHP import CSV file using LOAD DATA LOCAL INFILE with an HTML form

23 hours ago 2
ARTICLE AD BOX

I've reviewed these two answers that are most relevant PHP MySQL LOAD DATA INFILE Help and Load data in file from form upload

But I am still puzzled as to:

• How to pass the uploaded file to the mysql_query? (file.csv is just a placeholder).

• Since I'm not using a separate PHP file for the form action (I want to keep all code on one page), do I call the function $import_csv from the form action?

I don't want to use Ajax as I want all code on one page, and I want the page to reload after the file submit. Using PHP 8+, MySQL 8. Database credentials are globally available (this is a WordPress plugin).

<?php if( isset($_POST['submit']) ) { $import_csv = mysql_query("LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE table_name FIELDS TERMINATED BY ','"); } ;?> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="uploadcsv" accept=".csv" required> <input type="submit" value="Upload"> </form>
Read Entire Article