Import CSV into MYSQL via PHP

I am importing a CSV file into my administration area and I want to add files to my database. My PHP code for import.php :

 <?php include_once('../include/connection.php'); if ($_FILES[csv][size] > 0) { //get the csv file $file = $_FILES[csv][tmp_name]; $handle = fopen($file,"r"); //loop through the csv file and insert into database do { if ($data[0]) { mysql_query("INSERT INTO mobi(promo_title, promo_content, promo_image, promo_link, promo_cat, promo_name) VALUES ( '".addslashes($data[0])."', '".addslashes($data[1])."', '".addslashes($data[2])."', '".addslashes($data[3])."', '".addslashes($data[4])."', '".addslashes($data[5])."' ) "); } } while ($data = fgetcsv($handle,1000,",","'")); // //redirect header('Location: import.php?success=1'); die; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Import a CSV File with PHP & MySQL</title> </head> <body> <?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?> <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> Choose your file: <br /> <input name="csv" type="file" id="csv" /> <input type="submit" name="Submit" value="Submit" /> </form> </body> </html> 

The code displays correctly, but when I select my test file and click submit, I have the following problems:

 Warning: mysql_query() [function.mysql-query]: Access denied for user 'root'@'localhost' (using password: NO) in admin/import.php on line 23 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in admin/import.php on line 23 Warning: mysql_query() [function.mysql-query]: Access denied for user 'root'@'localhost' (using password: NO) in admin/import.php on line 23 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in admin/import.php on line 23 Warning: mysql_query() [function.mysql-query]: Access denied for user 'root'@'localhost' (using password: NO) in admin/import.php on line 23 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in admin/import.php on line 23 Warning: mysql_query() [function.mysql-query]: Access denied for user 'root'@'localhost' (using password: NO) in admin/import.php on line 23 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in admin/import.php on line 23 Warning: Cannot modify header information - headers already sent by (output started at admin/import.php:1) in admin/import.php on line 29 

Can anyone see the reason?

my connection.php code is:

 <?php try { $pdo = new PDO('mysql:host=localhost;dbname=*********', '*********', '*********'); }catch (PDOException $e){ exit('Database Error.'); } ?> 

note that my other pages, like add.php, delete.php, edit.php etc., work with the same include_once for connection.php

SO DO NOT WATCH ANYTHING WITH MY DATABASE CONNECTION.

Thank you.

+4
source share
4 answers

You are using PDO to establish a database connection. But the $pdo no longer in use!

To execute the SQL statement, you need to use it as $pdo->query("INSERT INTO mobi ...")

Instead, you use mysql_query to send the MySQL query. Therefore, you receive the following warnings:

Access denied for user 'root'@'localhost' (using password: NO)

A link to the server could not be established

Also make sure that you do not output any lines, etc. before your header() function to remove this warning: headers already sent .

+1
source

The credentials of your database may be incorrect. Check if the server is accessible.

and the header information already sent is caused by a space before your php code

  <?php include_once('../include/connection.php'); 

from

 <?php include_once('../include/connection.php'); 
0
source

The credentials for your database are incorrect. You should also look at mysqli's object-oriented approach to databases and use mysqli_real_escape_string instead of addlashes. The header error is the result of a mysql error and output to the page.

0
source

Have you tried using php to upload a file, so you don't need to scroll through it?

I would recommend using require_once instead of include_once . __DIR__ also always good to have an inclusion path in front of you. That way you can tell where the file from the current file is located, not the current current script.

It will look something like this:

 <?php require_once __DIR__ . '/../include/connection.php'; if ($_FILES[csv][size] > 0) { //get the csv file $file = $_FILES[csv][tmp_name]; mysql_query(" LOAD DATA LOW_PRIORITY LOCAL INFILE '$file' REPLACE INTO TABLE mobi FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\n' (promo_title, promo_content, promo_image, promo_link, promo_cat, promo_name) SET promo_name = TRIM(BOTH '\r' FROM promo_name); "); } 
0
source

Source: https://habr.com/ru/post/1499180/


All Articles