Demo data which already have in the database table:
INSERT INTO `csvtbl` (`ID`, `SKU`, `Product_Name`, `Model`, `Make`, `Year_From`, `Year_To`) VALUES
(1, 'C2AZ-3B584-AR', 'Power Steering Pump Seal Kit (Eaton Pump)', 'Galaxie', 'Ford', '1960', '1965'),
(2, 'C2AZ-3B584-AR', 'Power Steering Pump Seal Kit (Eaton Pump)', 'Thunderbird ', 'Fordtrest', '1960', '1965');
I have using below code and inserted years with comma(,) separated in table:
INSERT INTO `diff_yearstbl` (`ID`, `SKU`, `Product_Name`, `Model`, `Make`, `Year`) VALUES
(1, 'C2AZ-3B584-AR', 'Power Steering Pump Seal Kit (Eaton Pump)', 'Galaxie', 'Ford', '1960,1961,1962,1963,1964,1965'),
(2, 'C2AZ-3B584-AR', 'Power Steering Pump Seal Kit (Eaton Pump)', 'Thunderbird ', 'Fordtrest', '1960,1961,1962,1963,1964,1965');
Years data are working good, but I want to insert data Make and Model like same as Years, But this time just insert the data with comma (,) separated in one row like below example using SKU field. So need to merge the above two record in one row like below record.
INSERT INTO `diff_yearstbl` (`ID`, `SKU`, `Product_Name`, `Model`, `Make`, `Year`) VALUES
(1, 'C2AZ-3B584-AR', 'Power Steering Pump Seal Kit (Eaton Pump)', 'Galaxie, Thunderbird', 'Ford, Fordtrest' '1960,1961,1962,1963,1964,1965');
Below are the code what I have done:
$query = "select Year_TO - Year_From as diff_years, ID, SKU,Product_Name,Model,Make,Year_From,Year_To from csvtbl";
$result = mysql_query($query, $connect );
//$result = $db->query($select_test)->fetchAll();
if (count($result) > 0)
{
while($QryRow = mysql_fetch_assoc($result))
{
$diff_years = $QryRow['diff_years'];
$Year_From = $QryRow['Year_From'];
$SKU = $QryRow['SKU'];
$Product_Name = $QryRow['Product_Name'];
$Model = $QryRow['Model'];
$Make = $QryRow['Make'];
$years= array();
for ($x = $QryRow['Year_From']; $x <= $QryRow['Year_To']; $x++)
{
$years[] = $x;
}
$query_insert = "INSERT INTO diff_yearstbl(SKU,Product_Name,Model,Make,Year) VALUES('".$SKU."','".$Product_Name."','".$Model."','".$Make."','".implode('|',$years)."')";
$s_insert = mysql_query($query_insert, $connect );
}
}
else
{
echo "<p>Nothing matched your query.</p>";
}
?>
Please help about the same.
$query_insert