How to solve the problem of not being able to update the "Date Column": How to correctly upload a CSV file when updating a MySQL database table
P粉529581199
P粉529581199 2023-08-28 13:46:51
0
1
486

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"].'

P粉529581199
P粉529581199

reply all (1)
P粉817354783

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.

prepare($query); // 循环遍历csv剩余的行 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $stmt->bind_param('sss', $data[0],$data[1],$data[2]); $stmt->execute(); } fclose($handle); header("location: index.php?updation=1"); exit; // 重定向后一定要使用exit } else { $message = ''; } } else { $message = ''; } }

NOTE: I assume all 3 columns are of type text.

$stmt->bind_param('sss', $data[0],$data[1],$data[2]); ^^^

Ifdate_idis of type integer, you can change it to'ssi', although 3swill usually work fine too.

Reference:

fgetcsv
mysqli_prepare
mysqli_bind_param

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!