영카트 상품 대량 코드 관련 문의
본문
안녕하세요 그누보드는 초보입니다.
영카트 대량등록관련하여 작성한 코드를 문의드립니다. 제가 생각하기에는 제대로 작성한 것 같은데 작동하지 않네요.
https://sir.kr/qa/113796 의 글을 참고하여 영카트 DB 대량 등록용으로 변경중인데요.
(해당글은 그누보드 게시글 대량등록 코드입니다.)
제대로 맞게 변경한것 같은데 실제로 DB 에 등록이 되지 않습니다. 실제로 실행하고나면, g5_shop_item 테이블에 DB 거의 전체가 공란으로 한줄이 추가되고, 업로드 하려는 개수만큼 추가가 되지 않습니다. (it_id 값도 빈칸입니다.)
제가 보기에는 array 에서는 문제 없는데, 아래의 //변수 정리 단계에서, 어레이에서 변수로 값이 넘가지 않는것 같습니다.
//변수 정리
$it_id = $newpost['$it_id'];
확인해보니,
echo $line[0]; 하면 조회되는 값이
echo $newpost[it_id]; 하면 조회 되지 않습니다. 또
echo $it_idl; 이래도 조회가 되지 않습니다.
아래 코드가 제대로 작동하면 문제 없을것 같은데요. 도대체 제가 뭘 잘못하고 잇는지 모르겠습니다.
$it_id = $newpost['$it_id'];
phpadmin 에서 각 필드에 값을 지정한후 sql 실행탭에서 실행 했을때는 문제없이 DB가 등록이 되었습니다.
여하튼, 아래 코드를 실행하면 다음과 같은 워닝이 뜨기는 합니다.
Warning: Undefined array key "$it_id" in C:\xampp\htdocs\x4\itemup.php on line 21
Warning: Undefined array key "$ca_id" in C:\xampp\htdocs\x4\itemup.php on line 22
Warning: Undefined array key "$ca_id2" in C:\xampp\htdocs\x4\itemup.php on line 23
Warning: Undefined array key "$ca_id3" in C:\xampp\htdocs\x4\itemup.php on line 24
아래코드는 실제 코드인데요, 보기 편하라고 필드를 많이 삭제하였습니다. 실제로는 g5_item_shop 의 모든 필드를 다 작성하였는데 앞에 3개만 남기고 삭제하였습니다. 고수분들의 조언 요청드립니다. 감사합니다~!
<?php
//https://sir.kr/qa/113796
define('_INDEX_', true);
include_once('./_common.php');
/*include_once('./c5up.php');*/
/*include_once('./c5-image.php');*/
//ver1.0 150414 @_untitle_d
function insert_write($newpost)
{
global $g5;
//변수 정리
$it_id = $newpost['$it_id'];
$ca_id = $newpost['$ca_id'];
$ca_id2 = $newpost['$ca_id2'];
$ca_id3 = $newpost['$ca_id3'];
//글 입력하기
$sql = " insert into 'g5_shop_item'
set it_id = $it_id ,
ca_id= $ca_id ,
ca_id2='$ca_id2',
ca_id3='$ca_id3'
";
sql_query($sql);
}
ini_set('auto_detect_line_endings', true);
$oldMessage = '||||';
$deletedFormat = PHP_EOL;
//read the entire string
$str=file_get_contents('sam.php');
//replace something in the file string - this is a VERY simple example
$str=str_replace($oldMessage, $deletedFormat,$str);
//write the entire string
file_put_contents('sam.php', $str);
$file=fopen("sam.php","r");
/* */
while (($line = fgetcsv($file,10000,"^")) !==FALSE ) {
$newpost = array(
'it_id' => $line[0],
'ca_id' => $line[1],
'ca_id2' => $line[2]
);
}
insert_write($newpost);
/* insert_update($newpost);*/
/*insert_del($newpost);*/
/*imageup($imageup);*/
fclose($file);
//insert_write($newpost);
?>
답변 1
저렇게 $newpost배열에 값을 담으면 while{ }이 끝나면 $newpost에는 맨 마지막 행 하나만 달랑 남습니다
insert_write($newpost); 를 while loop내에 넣어야겠네요
$newpost[] = array(~~~); 이렇게 담아서 insert_write()에서 for나 foreach 를 이용해서 하거나....