Inserting records with mysqli_insert_id
I am trying to get data from a csv file and into a mysql database. My objective for example is to get the first few records and add them to a table then get the primary id as well as a few other rcords and add them to a different table and so on.
Th ecode I have so far works but it is buggy and inserts blank rows apparently randomly. Any ideas why? Also this is just sample data in the real app I would have more data and more tables to populate.
<?php
$link = mysqli_connect("localhost", "user", "pass", "test");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error()); exit();}
$objCSV = fopen("registerSales.csv", "r"); //get data from csv file
while (($objArr = fgetcsv($objCSV, 10000, ",")) !== FALSE) {
$strSQL = "INSERT INTO sample ";
$strSQL .= "(product_id,price,sku_id) ";
$strSQL .= "VALUES ";
$strSQL .= "('".$objArr[0]."','".$objArr[1]."','".$objArr[2]."') ";
echo "$strSQL";
//$strSQL .=",'".$objArr[3]."','".$objArr[4]."','".$objArr[5]."') ";
$objQuery = mysqli_query($link, $strSQL);
$i = mysqli_insert_id($link);
//rerun block with new id val
$strSQL = "INSERT INTO table2 ";
$strSQL .= "(row1, row2)"; //"(product_desc,product_sku_id) ";
$strSQL .= "VALUES ";
$strSQL .= "('".$i."','".$objArr[1]."') ";
$objQuery = mysqli_query($link, $strSQL);
echo "$strSQL" . "<br />";
} //end while
fclose($objCSV);
?>