68 lines
3.2 KiB
PHP
68 lines
3.2 KiB
PHP
<?php
|
|
if (!isset($_SESSION['user_id'])) exit; //페이지 직접 실행 방지
|
|
|
|
include_once "_common.php";
|
|
|
|
// 테이블 존재 유무 확인 쿼리
|
|
$query_check_table = "SHOW TABLES LIKE '{$fg['bakery_product_table']}'";
|
|
$result_check_table = sql_query($query_check_table);
|
|
echo "테이블 존재 확인 쿼리 실행 결과...................." . json_encode($result_check_table) . "<br>";
|
|
|
|
if (sql_num_rows($result_check_table) == 0) {
|
|
$query_create_table = "CREATE TABLE {$fg['bakery_product_table']} (
|
|
idx INT AUTO_INCREMENT PRIMARY KEY,
|
|
product_name VARCHAR(255) NOT NULL,
|
|
barcode INT NOT NULL UNIQUE,
|
|
used TINYINT(1) DEFAULT 1
|
|
)";
|
|
$result_create_table = sql_query($query_create_table);
|
|
echo "테이블 생성 쿼리 실행 결과...................." . json_encode($result_create_table) . "<br>";
|
|
} else {
|
|
echo "테이블이 이미 존재합니다.<br>";
|
|
}
|
|
|
|
// 중복을 제외하고 업데이트하는 쿼리 이전에 존재하는 데이터의 카운트를 가져옴
|
|
$query_count_inventory = "SELECT COUNT(*) as total FROM {$fg['bakery_inventory_table']}";
|
|
$result_count_inventory = sql_query($query_count_inventory);
|
|
$row_count_inventory = sql_fetch_array($result_count_inventory);
|
|
echo "{$fg['bakery_inventory_table']}에서 존재하는 데이터의 총 수...................." . $row_count_inventory['total'] . "<br>";
|
|
|
|
// 중복을 제외하고 업데이트하는 쿼리를 실행
|
|
$query_update = "INSERT IGNORE INTO fg_manager_bakery_product_info (product_name, barcode, used)
|
|
SELECT DISTINCT product_name, barcode, 1
|
|
FROM fg_manager_bakery_inventory AS inv
|
|
WHERE NOT EXISTS (
|
|
SELECT 1
|
|
FROM fg_manager_bakery_product_info AS prod
|
|
WHERE prod.barcode = inv.barcode
|
|
)";
|
|
|
|
$result_update = sql_query($query_update);
|
|
if ($result_update) {
|
|
echo "중복을 제외하고 업데이트 쿼리 실행 성공" . "<br>";
|
|
// 필드명을 변경하는 쿼리 추가
|
|
$query_alter_table = "ALTER TABLE {$fg['bakery_inventory_table']} CHANGE product_name product_name_old VARCHAR(255) NOT NULL";
|
|
$result_alter_table = sql_query($query_alter_table);
|
|
|
|
echo "'{$fg['bakery_inventory_table']}'의 'product_name' 필드명이 'product_name_old'로 변경되었습니다.<br>";
|
|
|
|
// INSERT된 데이터의 수를 가져옴
|
|
$query_count_inserted = "SELECT COUNT(*) as total FROM {$fg['bakery_product_table']} WHERE used = 1";
|
|
$result_count_inserted = sql_query($query_count_inserted);
|
|
$row_count_inserted = sql_fetch_array($result_count_inserted);
|
|
|
|
echo "{$fg['bakery_product_table']}에 삽입된 데이터의 수...................." . $row_count_inserted['total'] . "<br>";
|
|
|
|
// 중복 데이터를 제거한 데이터의 수
|
|
$inserted_rows = $row_count_inserted['total'] - $row_count_inventory['total'];
|
|
echo "중복을 제거한 후 기록된 데이터의 수...................." . max($inserted_rows, 0) . "<br>";
|
|
|
|
} else {
|
|
echo "업데이트에 실패했습니다. 종료합니다.<br>";
|
|
// 업데이트가 실패했다면 디버깅을 위해 쿼리를 출력함함
|
|
print_r($query_update);
|
|
echo "<br>";
|
|
}
|
|
|
|
|