MySQL Forums
Forum List  »  PHP

Inserting records with mysqli_insert_id
Posted by: Mike Grunow
Date: March 15, 2013 07:44AM

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);
?>

Options: ReplyQuote


Subject
Written By
Posted
Inserting records with mysqli_insert_id
March 15, 2013 07:44AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.