I have a database, that has a type_code in it. There are 3 types of products and certain numbers and costs associated with each type. The type codes are not present in sequential order in the database. they are random and i have a csv file with only 2 columns separated by , . one for prd_num and the the for cost. cost is determined by the prd_num, which is determined by the type_code so i cannot manually do this. i tried the php code below but it is messing up the database. The database is already present so i am trying to update with the type_code and not insert.

$conn = mysql_connect("localhost","username","password");
   $db = mysql_select_db("db_name");

   $fh = fopen("sheet1.csv", "r");
   while ($line = fgetcsv($fh, 1000, ",")) {
      $num = $line[0];
      $cost = $line[1];


      $query = "update products SET p_num='$num', cost='$cost' where type_code='1' ";
      $result = mysql_query($query);
   }

   fclose($fh);
   mysql_close();

Your query is updating all records with type_code='1' in each loop. Maybe you want this:

update products SET cost='$cost' where p_num='$num'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.