Below is my script for import data to mysql:
foreach ($file_data as $row) {
$sku = $row[$_POST["sku"]];
$title = $row[$_POST["title"]];
$slug = $row[$_POST["title"]];
$product_type = "physical";
$description = $row[$_POST["description"]];
}
if(isset($sku))
{
$query = "
INSERT INTO products
(sku, slug, product_type)
VALUES ".implode(",", $sku).",".implode(",", $slug).",".implode(",", $product_type)."
";
$statement = $connect->prepare($query);
if($statement->execute())
{
echo 'Data Imported Successfully';
}
}
And now can anyone help me how to now load $title
and $description
to second table product_details
?
@update @Mehrwarz
foreach ($file_data as $row) {
$sku = $row[$_POST["sku"]];
$title = $row[$_POST["title"]];
$slug = $row[$_POST["slug"]];
$product_type = "physical";
$description = $row[$_POST["description"]];
if (isset($sku)) {
$statement = $connect->prepare("INSERT INTO products
(sku, slug, product_type)
VALUES '$sku','$slug','$product_type'");
$statement2 = $connect->prepare("INSERT INTO product_details
(title, description)
VALUES '$title','$description'");
if (!$statement->execute()) {
$error = 'None or part of the data was updated';
}
}
}
echo $error ?? 'Data Updated Successfully';
CodePudding user response:
Maybe you can push any array
<?php
$query_array = [];
foreach ($file_data as $row) {
$sku = $row[$_POST["sku"]];
$title = $row[$_POST["title"]];
$slug = $row[$_POST["title"]];
$product_type = "physical";
$description = $row[$_POST["description"]];
if(isset($sku)){
$query = 'INSERT INTO products
(sku, slug, product_type)
VALUES SET title="'.$title.',sku="'.$sku.'", etc.. #add more"';
$query = rtrim($query,',');
array_push($query_array,$query);
}
}
foreach ($query_array as $key => $value) {
$mysqlconn->query($value);
}
CodePudding user response:
This may work for you.
$T1Values = "";
$T2Values = "";
foreach ($file_data as $row) {
$T1Values .= "('" . $row[$_POST['sku']] . "','" . $row[$_POST['slug']] . ",'" . $row[$_POST['product_type']] . "),";
$T2Values .= "('" . $row[$_POST['title']] . "','" . $row[$_POST['description']] . "'),";
}
if (isset($sku)) {
$T1Values = trim(',', $T1Values);
$T2Values = trim(',', $T2Values);
$query1 = "INSERT INTO products (sku, slug, product_type) VALUES($T1Values)";
$statement = $connect->prepare($query1);
if ($statement->execute()) {
$query2 = "INSERT INTO product_details (title, product_details) VALUES($T2Values)";
$statement = $connect->prepare($query2);
if ($statement->execute()) {
echo "All records inserted successfully";
}
}
}