Jump to content

import a csv file into mysql database


xclusivzik

Recommended Posts



<!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>




<form action="" method="post" enctype="multipart/form-data" name="csv" id="csv">
  Choose your file: <br />
  <input name="csv" type="file" id="csv" />
  <input type="submit" name="Submit" value="Submit" />
</form>


</body>


but i keep having this error 

Notice: Undefined index: csv in C:\wamp\www\youngsoul\upload\file.php on line 7

Link to comment
https://forums.phpfreaks.com/topic/292545-import-a-csv-file-into-mysql-database/
Share on other sites

sowi About that, here is the code

<?php 
include ('db_connection.php');
//connect to the database


//


if ($_FILES['csv']['size'] > 0) {


    //get the csv file
    $file = $_FILES[csv][tmp_name];
    $handle = fopen($file,"r");
    if ($handle !== FALSE) {
    while (!feof($handle)) {
        $data = fgetcsv($handle,10000,",","'");
        if ($data[0])
       {
            mysql_query("INSERT INTO data (name, Groups,phone numbers) VALUES
                (
                    '".addslashes($data[0])."',
                    '".addslashes($data[1])."',
                    '".addslashes($data[2])."'
                )
            ");
        }
    }
        fclose($handle);
    } 
    //redirect
    header('Location: file.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>




<form action="" method="post" enctype="multipart/form-data" name="csv" >
  Choose your file: <br />
  <input name="csv" type="file" id="csv" />
  <input type="submit" name="Submit" value="Submit" />
</form>


</body>
</html>

If you are just going to be importing csv data straigh into the database with no further processsing required then use a LOAD DATA INFILE query.

 

Also please wrap you code in [/code][/code] tags or click the <> button in the editor when posting code 

If you are just going to be importing csv data straigh into the database with no further processsing required then use a LOAD DATA INFILE query.

 

That's a very, very bad idea. In fact, there's something wrong with your server configuration if your application can use this query.

 

The LOAD DATA INFILE query requires the FILE permission. Granting this to your application means that it can both read and write external files. It's easy to see that an SQL injection attack may now compromise the entire server: The attacker just has to grab critical passwords from your filesystem or create a malicious script.

 

Sure, the Unix account of the MySQL database should not have unlimited access to the filesystem. But if the database server is already misconfigured, I wouldn't bet on that.

 

Long story short: You do not want LOAD DATA INFILE. If it's active, turn it off. Then use a plain INSERT query.

so what do you suggest is wrong with the code , the query refuses to work, even when i use this code

<?php 
$con=mysqli_connect("localhost","root","","book") or die(mysql_error());
 
 
 
if (isset($_POST['submit'])) {
 
    //get the csv file
    $file = $_FILES['file']['tmp_name'];
    $handle = fopen($file,"r");
    
   
        while (($filelop= fgetcsv($handle,10000,",")) !==false)
        
       {
        $name = $filelop[0];
        $group = $filelop[1];
        $phone = $filelop[2];
 
            $sql=mysql_query("INSERT INTO data (name, Groups, phone_number) VALUES ('$name','$group','$phone)");
  if ($sql) {
  echo "uploaded";
  }
 
        }
    }
       
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Import a CSV File with PHP & MySQL</title>
</head>
 
<body>
 
 
<form action="file.php" method="post" enctype="multipart/form-data" >
  Choose your file: <br />
  <input name="csv" type="file" id ="csv" />
  <input type="submit" name="Submit" value="Submit" />
</form>
 
</body>
</html>

You have a mysqli connection but you are using mysql_query(). You can't mix the two libraries. Use mysqli_query().

 

Better would by to create a mysqli prepared statement and repeatedly execute that with the new data values each time.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.