E
E
Evgeniy2022-04-19 20:11:16
PHP
Evgeniy, 2022-04-19 20:11:16

How to change the values ​​in the database from a .csv file in php?

Good afternoon, please tell me how to change the values ​​in the database from the csv file?

We have file.csv
A unique field in the database - login I figured out

how to add data from csv, using the code below.
But I didn’t understand how to replace the data, if, for example, passwords change

<?php
  header('Content-type: text/html; charset=utf-8'); 

  $mysqli = new mysqli('localhost', 'bd_login', 'bd_password', 'bd_name');
  $logPswd = fopen('file.csv', 'r');
  
  
  while( !feof($logPswd) ) {
    $mass = fgetcsv($logPswd, 512, ',' );	
    
    $j = count($mass);
    
    if($j > 1){ 
      
      $mysqli->query("INSERT INTO `users` ( `id`, `login`, `password` ) VALUES ( '{$mass[0]}', '{$mass[1]}', '{$mass[2]}'  ) ");
    }
     
  }
  fclose($logPswd);
  $mysqli-close(); 
?>


I read about UPDATE SET, etc., but something didn’t work out ..
everything is bad with php
Tell me how to do it right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2022-04-19
@Zheleznov

https://stackoverflow.com/questions/548541/insert-...

V
Vitaly Artemyev, 2022-04-19
@Vitaly48

We must learn to ask the right questions.
Now it sounds like this: How to change the values ​​in the database from a .csv file in php?
And should so: How to update the data in a DB
After all csv has no relation to a problem, you already successfully read the data from a csv file.
Here is the answer to your question:
https://www.w3schools.com/mysql/mysql_update.asp

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question