0

I have a form with an input file, I want the user to upload a sql file, read it with php and import it to my database

My form code is

    <div class="form-row" >
       <div class="form-group col-md-4">
         <input type="file" accept=".sql" class="form-control" name="sql">
       </div>
       <div class="form-group col-md-4">
         <button type="submit" formaction="upload_sql.php" style="margin-top:8%" class="btn btn-primary" name="upload_sql">Upload</button>
       </div>
     </div>

But I don't know how to import that file into my database, I tried with "mysql -u root -p database < file.sql"

  • 2
    You probably want them to upload a CSV file, not a SQL file. If your user uploads a SQL file, you will not be able to reliably execute it safely, i.e. without things like drop tables or grant users. The CSV file will allow you to accept data in some kind of bulk format, and import it safely. PHP has built in methods for processing CSV data, see https://www.php.net/manual/en/function.fgetcsv.php – Alex Barker Aug 30 '19 at 21:05
  • Agreed. But I guess if OP would like to stick to his method, an SQL file is basically just a long SQL command, isn't it? So I guess upload the file, read it's contents, and execute that string as a mysql query. Bit of a security nightmare for sure, but it would work. – Andrew Aug 30 '19 at 21:11

1 Answers1

0

Mysql import SQL file with terminal

mysql -u username -p database_name < file.sql

With PHP

system('mysql -u username -p database_name < file.sql');

dılo sürücü
  • 3,821
  • 1
  • 26
  • 28