The main problem in my code is that I cannot update the "date" from the csv file into the mysql database table using php. The line of code $date = mysqli_real_escape_string($connect, $data[1]); is the main problem here. I'm looking for any alternative query for this particular line of code.
This is the csv file: https://drive.google.com/file/d/1EdMKo-XH7VOXS5HqUh8-m0uWfomcYL5T/view?usp=sharing
This is the complete code:
Please select only CSV files'; } } else { $message =''; } } if(isset($_GET["updation"])){ $message =''; } $query = "SELECT * FROM my_table"; $result = mysqli_query($connect, $query); ?>Update Mysql database by uploading CSV file using PHP
Update Mysql database by uploading CSV file using PHP
Birthss
Date Birth '.$row ["date"].' '.$row ["births"].' '; } ?>
First, you need to read and discard the header row in the CSV. Then, using appropriate, prepared, and parameterized queries, you can update the database correctly. Since the dates in the .csv file are in the correct format, no action is required, but this may not be the case with other CSV files, and often the dates will need to be reformatted before they can be stored correctly into the table.
NOTE: I assume all 3 columns are of type text.
If
date_id
is of type integer, you can change it to'ssi'
, although 3s
will usually work fine too.Reference:
fgetcsv
mysqli_prepare
mysqli_bind_param