【问题标题】:How to remove double quotes stored in the database while reading through fgetcsv通过fgetcsv读取时如何删除存储在数据库中的双引号
【发布时间】:2015-05-27 10:23:49
【问题描述】:

这是我的代码。每当我执行此操作时,数据都会以双引号作为“公园大道”存储在数据库中。

if ($_FILES["csv_file"]["size"] > 0) 
{
    //get the csv file
    $file = $_FILES["csv_file"]["tmp_name"];
    $handle = fopen($file,"r");
    //loop through the csv file and insert into database
    do {
        if ($data[0]) {
            mysql_query("INSERT INTO add_product(product_id, product_name, date) VALUES
                (
                    '".addslashes($data[0])."',
                    '".addslashes($data[1])."',
                    '".addslashes($data[2])."'
                )
            ");
        }
    } while ($data = fgetcsv($handle,1000,",","'"));
    //
    //redirect
    header('Location: http://abcd.adisoftronics.in/index.php/abc/csv_1?success=1');     
    die;
}

【问题讨论】:

  • 使用 str_replace('"',' ',$data[0]);

标签: php mysql fgetcsv


【解决方案1】:

最简单的方法是手动替换它们。

 if ($data[0]) {
   mysql_query("INSERT INTO add_product(product_id, product_name, date) VALUES
  (
  '".str_replace('"','',addslashes($data[0]))."',
  '".str_replace('"','',addslashes($data[1]))."',
  '".str_replace('"','',addslashes($data[2]))."'
   )
 ");
}

如果你想取出更多的字符,你应该检查像 htmlspecialchar() 这样的函数。

【讨论】:

  • 在应用此代码时,我们不需要应用while条件或添加while条件......请给我完整的代码以直接在页面中实现..
  • 该代码直接来自您的问题。只需用我所做的更改替换该部分即可。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-16
相关资源
最近更新 更多