gpt4 book ai didi

PHP 不将记录从 CSV 插入到 MySQL 表中,其中内容特殊字符

转载 作者:行者123 更新时间:2023-11-29 21:50:19 25 4
gpt4 key购买 nike

我正在将记录从 CSV 插入到 mysql 表中,但 php 代码会跳过 CSV 文件中包含“'”的所有内容。它与正常数据一起工作正常与 Jams Bond 一起工作正常,但跳过此数据:jams bond

function readCSV($csvFile)
{
$file_handle = fopen($csvFile, 'r');
while (!feof($file_handle) )
{
$line_of_text[] = fgetcsv($file_handle, 1024);
fclose($file_handle);
}

$csvFile = $target_file;
$csv = readCSV($target_file);
for($i=0;$i<sizeof($csv);$i++)
{
if($i>=0)
{
if($csv[$i][3]!='')
{
$dd=date('Y-m-d H:i:s', strtotime($csv[$i][2]));
mysqli_query($conn123,"insert into call_detail_record (date, caller_id_from,extension, caller_id_to, duration, cost, destination, cost_rate, cost_centre) values('".$dd."','".$csv[$i][3]."','".$csv[$i][4]."','".$csv[$i][5]."','".$csv[$i][6]."','".$csv[$i][7]."','".$csv[$i][8]."','".$csv[$i][10]."','".$csv[$i][11]."')");
}
}
if($i==(sizeof($csv)-1))
{
?>
<div class="form-group">
<label><font color="#009933">File Uploaded Successfully...</font></label>
</div>
<?php
unlink($target_file);
}
}

最佳答案

更改代码以转义引号

$dd=date('Y-m-d H:i:s', strtotime($csv[$i][2]));

/* escaping */
$csv[$i][3] = mysqli_real_escape_string($conn123, $csv[$i][3]);
$csv[$i][4] = mysqli_real_escape_string($conn123, $csv[$i][4]);
$csv[$i][5] = mysqli_real_escape_string($conn123, $csv[$i][5]);
$csv[$i][6] = mysqli_real_escape_string($conn123, $csv[$i][6]);
$csv[$i][7] = mysqli_real_escape_string($conn123, $csv[$i][7]);
$csv[$i][8] = mysqli_real_escape_string($conn123, $csv[$i][8]);
$csv[$i][10] = mysqli_real_escape_string($conn123, $csv[$i][10]);
$csv[$i][11] = mysqli_real_escape_string($conn123, $csv[$i][11]);

mysqli_query($conn123,"insert into call_detail_record
(date, caller_id_from, extension, caller_id_to,
duration, cost, destination, cost_rate, cost_centre)
values ('".$dd."','".$csv[$i][3]."','".$csv[$i][4]."','".$csv[$i][5]. "','".
$csv[$i][6]."','".$csv[$i][7]."','".$csv[$i][8]."','".
$csv[$i][10]."','".$csv[$i][11]."')");

同样用于查询...

$mysearch = = mysqli_real_escape_string($conn123, "andrew's");

mysqli_query($conn123,"SELECT * from call_detail_record WHERE
caller_id_from ='" . $mysearch . "'");

关于PHP 不将记录从 CSV 插入到 MySQL 表中,其中内容特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33709746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com