0

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();
2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by smantscheff
0

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'
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.