English 中文(简体)
从Excel进口的零值更新为零
原标题:Updating Values as Zero from Excel Import
  • 时间:2012-05-24 17:10:24
  •  标签:
  • php

我用跟踪代码 将CSV输入到我的SQL

 if($_POST[ sform ] == "1")
{
     $fname = $_FILES[ upload ][ name ];        
     $chk_ext = explode(".",$fname);        
     if(strtolower($chk_ext[1]) == "csv")
     { 
        $filename = $_FILES[ upload ][ tmp_name ];
        $row = 1;
        if (($handle = fopen($filename, "r")) !== FALSE)
        {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
              {
                $row++;
                $data_entries[] = $data ;
              }
              fclose($handle);
         }
         foreach($data_entries as $line)
         {
              $sql = "INSERT into testposts(ID,UID,title,tags,desc) values(  , $data[0] , $data[1] , $data[2] , $data[3] )";
$conn->execute($sql);
          $conn->execute($line);
         }
         $msg = "Successfully Imported";
     }
     else
     {
         $error = "Invalid File";
     }   
}

当我尝试更新时, 所有值都会被更新为 0

我的 Csv 文件如下( 例如)

1,title,tags,send2

不包括 PID 值,因为它是自动递增的

所以我试了第二次变换

if($_POST[ sform ] == "1")
{
     $fname = $_FILES[ upload ][ name ];        
     $chk_ext = explode(".",$fname);        
     if(strtolower($chk_ext[1]) == "csv")
     { 
 $filename = $_FILES[ upload ][ tmp_name ];
     $handle = fopen($filename, "r");

     while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
     {
              $sql = "INSERT into testposts(ID,UID,title,tags,desc) values(  , $data[0] , $data[1] , $data[2] , $data[3] )";
          mysql_query($sql) or die(mysql_error());
          //$conn->execute($sql);
         }
         $msg = "Successfully Imported";
     }
     else
     {
         $error = "Invalid File";
     }   
}

在我的第二个代码中,它正确地上传了预期的值,但插入了两次。

任何帮助都会感激不尽,谢谢

问题回答

我对第二版的作品感到惊讶:

$sql = "INSERT into testposts(ID,UID,title,tags,desc) values(  , $data[0] , $data[1] , $data[2] , $data[3] , $data[4] )";

您的数值多于列名。 是否有 < code>$data[ 4] ?

如果您已启用警告, 您的数据可以包含php警告, 这些警告可以包含 字符, 打破查询 。

您应该切换到 PDO / 已准备好的语句, 以避免 Sql 输入问题和错误, 如这些错误 。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签