Answer the question
In order to leave comments, you need to log in
Write a new one or overwrite if any?
The data is taken from excel, how to correctly implement a query with a check for a new record and overwrite if there is already a record?
include 'bd.php'; // подключаемся к базе данных
$loadfile = "price.xlsx"; // получаем имя загруженного файла
require_once $_SERVER['DOCUMENT_ROOT']."/excels/PHPExcel/IOFactory.php"; // подключаем класс для доступа к файлу
$objPHPExcel = PHPExcel_IOFactory::load($_SERVER['DOCUMENT_ROOT']."/1c/".$loadfile);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) // цикл обходит страницы файла
{
$highestRow = $worksheet->getHighestRow(); // получаем количество строк
$highestColumn = $worksheet->getHighestColumn(); // а так можно получить количество колонок
for ($row = 1; $row <= $highestRow; ++ $row) // обходим все строки
{
$cell1 = $worksheet->getCellByColumnAndRow(0, $row);
$cell2 = $worksheet->getCellByColumnAndRow(1, $row);
$cell3 = $worksheet->getCellByColumnAndRow(2, $row);
$cell4 = $worksheet->getCellByColumnAndRow(3, $row);
$cell5 = $worksheet->getCellByColumnAndRow(4, $row);
$cell6 = $worksheet->getCellByColumnAndRow(5, $row);
$cell7 = $worksheet->getCellByColumnAndRow(6, $row);
$cell8 = $worksheet->getCellByColumnAndRow(7, $row);
// проверить если нет то записать:
$sql = "INSERT INTO `price` (`id`,`art`, `id_s`, `count`, `name`, `name_s`, `sum_i`, `sum_u`)
VALUES
('$cell1','$cell2','$cell3','$cell4','$cell5','$cell6','$cell7','$cell8')"; //запись
$query = mysql_query($sql) or die('Ошибка чтения записи: '.mysql_error());
}
}
Answer the question
In order to leave comments, you need to log in
INSERT ON DUPLICATE KEY UPDATE
https://www.mysqltutorial.org/mysql-insert-or-upda...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question