2013-09-02 7 views
0

인터넷에서 정보를 수집하고이 작은 스크립트를 작성하여 CSV 파일을 MySQL 데이터베이스로 가져 왔습니다. 나는 그것이 옳다고 생각하지만, 효과가 없다.PHP로 MySQL에 CSV 가져 오기

내가 뭘 잘못하고 있니?

<?php 

//connect to the database 
$connect = mysql_connect("localhost","testdb","testdb"); 
mysql_select_db("testdb",$connect); //select the table  

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 class12 (htno, subcode, subname, int_marks, ext_marks, result, credits) VALUES 
       (
        '".addslashes($data[0])."', 
        '".addslashes($data[1])."', 
        '".addslashes($data[2])."' 
        '".addslashes($data[3])."' 
        '".addslashes($data[4])."' 
        '".addslashes($data[5])."' 
        '".addslashes($data[6])."' 
        '".addslashes($data[7])."' 
       ) 
      "); 
     } 
    } while ($data = fgetcsv($handle,3000,",","'")); 
    // 

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

가능하다면, 누군가에 코드를 포함하시기 바랍니다 후 가져 오기 (.csv 확장자가없는) 파일 이름을 가진 SQL 테이블을 자동을 만듭니다. 도움이

당신이 here.You가있는 테이블 이름을 넣어 가지고 할 2.May 경우

+0

http://stackoverflow.com/questions/18470359/importing-csv-to-mysql-via-php?rq=1 유사 – ZenOut

+0

어떤 오류가 발생합니까? – Miguelo

+0

@Miguelo 그냥 비어있는 페이지를 보여줍니다 .. – BlackCoder

답변

1

당신은 내가 그것을 fine.Vote 작업 년대 code을 테스트 한

   '".addslashes($data[4])."' 
       '".addslashes($data[5])."' 
       '".addslashes($data[6])."' 
       '".addslashes($data[7])."'` 

다음 줄에 쉼표를 놓친 데이터베이스 이름을 작성하십시오.

mysql_select_db("testdb",$connect); //select the table

+0

실제로는 작동했지만 :) 여전히 빈 페이지를 다시 돌려 놓습니다.'header ('Location : import.php? success = 1')를 대체하는 에코 성공 메시지를 작성할 수 있습니까? ; die;' – BlackCoder

+0

할 수 있습니다. 현재 파일 이름은'import.php'입니까? – ZenOut

+0

예. 그것의 import.php – BlackCoder

0

는 mysql_query ('.addslashes ($ 데이터 [0]). " "htno, 서브 코드, 하위 이름, int_marks, ext_marks, 결과 크레딧) ( 을 된 값 (class12 INTO 삽입' ' ($ data [1]). ", ".addslashes ($ data [2]). " ' ".addslashes ($ data [3]). "' '".addslashes ($ data [4]). " ".addslashes ($ data [5]). " ' ".addslashes ($ data [6]). "' '".addslashes ($ data [7] ]). "// 나는이 테이블이 7 열 밖에 없다고 생각한다.

+0

SO에 오신 것을 환영합니다. 정확하게 소스 코드의 개요를 기억하십시오. –