-1

Sorry guys, I mean my real code is that in my PC. And here doesn't accept PHP code, just HTML, CSS and Java Script. I paste how HTML

I just copy and paste from my notepad++

My code:

$conn = new PDO("mysql:host=localhost;dbname=fabio", "root", "");
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 $filename = ($_FILES['arquivocsv2']["tmp_name"]);
$abraArq = fopen($filename,"r");
$import = $conn->prepare("INSERT INTO relatorio (DocumentoSD,Descricao,CodCliente,Cliente,Regiao,DataDocumento,Material,Condicoes,Plano)VALUES (:DocumentoSD, :Descricao, :CodCliente, :Cliente, :Regiao, :DataDocumento, :Material, :Condicoes, :Plano)");

$import->bindParam(':DocumentoSD', $DocumentoSD,PDO::PARAM_STR);
$import->bindParam(':Descricao', $Descricao,PDO::PARAM_STR);
$import->bindParam(':CodCliente', $CodCliente,PDO::PARAM_STR);
$import->bindParam(':Cliente', $Cliente,PDO::PARAM_STR);
$import->bindParam(':Regiao', $Regiao,PDO::PARAM_STR);
$import->bindParam(':DataDocumento', $DataDocumento,PDO::PARAM_STR);
$import->bindParam(':Material', $Material,PDO::PARAM_STR);
$import->bindParam(':Condicoes', $Condicoes,PDO::PARAM_STR);
$import->bindParam(':Plano', $Plano,PDO::PARAM_STR);

while (($items = fgetcsv($abraArq, 2048, ';')) !== FALSE) {

$DocumentoSD = $items[0];
$Descricao = $items[4];
$CodCliente = $items[5];
$Cliente = $items[6];
$Regiao = $items[7];
$DataDocumento = $items[10];
$Material = $items[11];
$Condicoes = $items[17];
$Plano = $items[29];
   // Execute prepared query
$import->execute();}
fabinho
  • 31
  • 5
  • 2
    Well, first, `$DocumentoSD = $items[];` is missing a key. Second, you are binding by value, not by reference. So you are just binding null everywhere because when you bind, those variables don't have a value. You want to use [bindParam](http://php.net/manual/en/pdostatement.bindparam.php) instead of `bindValue`. Bind value just takes the current value and binds that. Bind param, binds a reference to that variable so when the value changes, so does the bind value. – Jonathan Kuhn Apr 28 '16 at 16:04
  • A bit of sensible code indentation would make life easier for all of us – RiggsFolly Apr 28 '16 at 16:06
  • Where is the close option that say `complete pile of dudu` I suggest you reboot and [bootstrap from here](http://php.net/manual/en/book.pdo.php) _its a very secret resource, dont tell anybody I told you about it_ – RiggsFolly Apr 28 '16 at 16:07
  • Sorry! I forgot to put a value there, but in my real code it's correct – fabinho Apr 28 '16 at 16:08
  • 1
    @fabinho Post your "real code" then. – Mike Apr 28 '16 at 16:10
  • Jonathan, I changed for bindParam but show the error: Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xE9dito' for column 'Descricao' – fabinho Apr 28 '16 at 16:11
  • the PDO isn't accepting specia charecters – fabinho Apr 28 '16 at 16:12
  • What is this supposed to be doing? `$DocumentoSD = $items[]` – Mike Apr 28 '16 at 16:12
  • 1
    `Incorrect string value`. That should be enough to figure this out. You are binding one of those as a string, but passing in a value that isn't a string. Whether that means the variable type is wrong or the column type is wrong I'm not sure. But what is the value you are trying to bind and what is the data type for that column in the database? – Jonathan Kuhn Apr 28 '16 at 16:13
  • @Jonathan Kuhn every column is varchar and I want to import how string – fabinho Apr 28 '16 at 17:21
  • @fabinho What does `var_dump($Descricao);` output? – Mike Apr 28 '16 at 17:48
  • @Mike output words with character especial like this: "Reprovado Cr�dito" – fabinho Apr 28 '16 at 17:56
  • 1
    See: http://stackoverflow.com/a/11013986 – Mike Apr 28 '16 at 18:00
  • Tks.. I could just preparing before define the parameters like this: $DocumentoSD = utf8_encode($items[0]); $Descricao = utf8_encode($items[4]); – fabinho Apr 28 '16 at 18:35

1 Answers1

1

You are binding by value, not by reference. So you are just binding null everywhere because when you bind, those variables don't have a value. You want to use bindParam instead of bindValue.

bindValue just takes the current value and binds that. When you are doing the binding, those variables haven't been set so their value is null.

bindParam, binds a reference to that variable so when the value changes, so does the bound value.

Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43
  • 1
    You might regret getting involved with this one, but good luck anyway! – RiggsFolly Apr 28 '16 at 16:14
  • I would be willing to bet that this is the real code, but just a small subset of a much larger block of code and they wanted to only show what was appropriate, as they should. – Jonathan Kuhn Apr 28 '16 at 16:14
  • @Mike It seems unlikely that the real code would use `bindParam` and that `bindValue` is a copying error. – Barmar Apr 28 '16 at 17:11
  • @fabinho Are you still working on this? If not, feel free to either mark this answer as accepted or post a new answer yourself if this doesn't answer your question adequately. – Mike Apr 28 '16 at 17:15
  • @Barmar I assumed the OP had copied a non-live version of the code that had been edited. No idea though. – Mike Apr 28 '16 at 17:26