0
Dear, I have a routine that creates a txt file with some information of a query in mysql, but when the query returns some text with quotes, the problem begins. When displaying the data on the screen, information that has double quotes usually appears, for example (text "with" quotation marks), but when this data is saved in a txt file the quotation marks are replaced by "e (quote; text with quote). I don’t know how to fix this problem. Follows the code:
<?PHP
include_once("../../function/valida.php");
include_once('../../function/funcoes.php');
$ItemQTD = $_POST['Item'];
$B12 = $_POST['nfeB12'];
$CST = $_POST['CST'];
$IPI=$_POST['IPI'];
$PIS=$_POST['PIS'];
$B15a=$_POST['B15a'];
$COFINS=$_POST['COFINS'];
$B9 = $_POST['B9'];
$B15 = $_POST['B15'];
$X26_382 = $_POST['X26_382'];
$X26_383 = $_POST['X26_383'];
$X26_387 = $_POST['X26_387'];
$X26_386 = $_POST['X26_386'];
$B29 = $_POST['B29'];
$B02 = $_POST['B02'];
$B02 = split(":",$B02);
$b116 = $_POST['116b'];
echo "I116b=$b116";
$Z401 = $_POST['Z401'];
$Y07 = $_POST['Y07'];
$Y07[1]=MudaData($Y07[1]);
if(!empty($Y07[3])){
$Y07[3]=MudaData($Y07[3]);
}
if(!empty($Y07[5])){
$Y07[5]=MudaData($Y07[5]);
}
$colunas ="
A2,A3,A4,
B6,B7,B8,B9,B10,B11,B12,B13,B14,B15,B15A,B16,B25,B26,B27,B28,B29,B291,B292,B29A,B29B,B29C,B29D,
B02,
C32,C33,C46,C47,C48,C49,C49A,C02,C0535,C0536,C0537,C0538,C0539,C0540,C0541,C0542,C0543,C0544,C0545,
E65,E77A,E78,E79,E791,E79A,E02,E03A,E0567,E0568,E0569,E0570,E0571,E0572,E0573,E0574,E0575,E0576,E0577,
G91,G92,G93,G94,G95,G96,G97,G02,
H99,
I101,I102,I103,I104,I105,I106,I107,I108,I109,I110,I111,I112,I113,I113A,I114,I115,I116,I116A,I116B,I128A,I128B,
N02166,N02167,N02168,N02169,N02170,N02171,N03173,N03174,N03175,N03176,N03177,N03178,N03179,N03180,N03181,
N03182,N03183,N03184,N06203,N06204,N0620401,N0620402,
O247,O248,O249,O250,O251,O07,O08,O10254,O10257,
Q02269,Q02270,Q02271,Q02272,Q04,
S02295,S02296,S02297,S02298,S04,
W02328,W02329,W0232901,W02330,W02331,W02332,W02333,W02334,W02335,W02336,W02337,W02338,W02339,W02340,W02341,W02341A,
X,X03361,X03362,X03363,X03364,X03365,X04,X26382,X26383,X26384,X26385,X26386,X26387,
Y07396_1,Y07397_1,Y07398_1,Y07396_2,Y07397_2,Y07398_2,Y07396_3,Y07397_3,Y07398_3,Z400,Z401";
$selectId = "select id from temp_nf";
$selectId = mysql_query("select distinct id,b12 from temp_nf");
while($row = mysql_fetch_array($selectId)){
$query = "select $colunas from temp_nf where id=$row[id] order by i128b";
$cont=0;
$NFe = mysql_fetch_assoc(mysql_query($query));
//substituir valores vazios para null
foreach($NFe as $x){
if(empty($x)){
if($cont==9){
$x="'0000001'";
}else{
$x="null";
}
}else{
$x = "'".$x."'";
}
$str2.=$x.",";
$cont++;
}
$sql_query="insert into financeiro_nf (".$colunas.",data_modif,usuario) value (".$str2."now(),'$_SESSION[Usuario]')";
if(isset($sql_query)){
mysql_query($sql_query,$conexao);//usado o utf8 para que seja interpretado os char especiais na query
$id=mysql_insert_id();
//if ($error = mysql_errno()) echo("<script type\"text/javascript\">alert(\"Erro 1 : ".mysql_errno().", contate o administrador\");</script>");
if ($error = mysql_errno()) die("<script type\"text/javascript\">alert(\"Erro 1 : ".mysql_errno().", contate o administrador\");history.back();</script>");
echo $update."</br>";
if(mysql_affected_rows() > 0)
{
}else
{
echo "<script>alert(\"1 - Nenhum arquivo afetado \");</script>'";
};
};
$NFe['I103'] = str_replace('"','',$NFe['I103']);
$I.="
H|$NFe[H99]|
I|$NFe[I101]|$NFe[I102]|$NFe[I103]|$NFe[I104]|$NFe[I105]|$NFe[I106]|$NFe[I107]|$NFe[I108]|$NFe[I109]|$NFe[I110]|$NFe[I111]|$NFe[I112]|$NFe[I113]|$NFe[I113A]|$NFe[I114]|$NFe[I115]|$NFe[I116]|$NFe[I116A]|$b116|$NFe[I128A]|$NFe[I128B]|
M|
N|";
if($CST=="00"){
$I.="
N02|$NFe[N02166]|$NFe[N02167]|$NFe[N02168]|$NFe[N02169]|$NFe[N02170]|$NFe[N02171]|";
}else if($CST=="10"){
$I.="
N03|$NFe[N03173]|$NFe[N03174]|$NFe[N03175]|$NFe[N03176]|$NFe[N03177]|$NFe[N03178]|$NFe[N03179]|$NFe[N03180]|$NFe[N03181]|$NFe[N03182]|$NFe[N03183]|$NFe[N03184]|";
}else if($CST=="40"){
$I.="
N06|$NFe[N06203]|$NFe[N06204]|$NFe[N0620401]|$NFe[N0620402]|";
};
$I.="
O|$NFe[O247]|$NFe[O248]|$NFe[O249]|$NFe[O250]|$NFe[O251]|";
if($IPI=="SIM"){
$ipi = number_format(($NFe['O10254']*0.1),3);
$I.="
O07|$NFe[O07]|".Arredonda($ipi)."|
O10|$NFe[O10254]|$NFe[O10257]|";
}else{
$I.="
O08|$NFe[O08]|";
}
$I.="
Q|";
if(!empty($NFe['E79'])){
$I.="
Q04|$NFe[Q04]|";
}else{
if($PIS=="SIM"){
$I.="
Q02|$NFe[Q02269]|$NFe[Q02270]|$NFe[Q02271]|$NFe[Q02272]|";
}
}
$I.="
S|";
if($COFINS=="SIM"){
$I.="
S02|$NFe[S02295]|$NFe[S02296]|$NFe[S02297]|$NFe[S02298]|";
}else{
$I.="
S04|$NFe[S04]|";
}
$str2="";
$sql_update = "
update financeiro_nf set
B9='$B9',
B15='$B15',
B29='$B29',
B02='$B02[1]',
I116b='$b116',
X26382='$X26_382',
X26383='$X26_383',
X26387='$X26_387',
X26386='$X26_386',
Y07397_1='$Y07[1]',
Y07398_1='$Y07[2]',
Y07397_2='$Y07[3]',
Y07398_2='$Y07[4]',
Y07397_3='$Y07[5]',
Y07398_3='$Y07[6]',
Z401='$Z401'
where id='$id'
";
$update=str_replace("''","null",$sql_update);
if(isset($update)){
mysql_query($update,$conexao);//usado o utf8 para que seja interpretado os char especiais na query
$id=mysql_insert_id();
if ($error = mysql_errno()) die($update."<script type\"text/javascript\">alert(\"Erro 2 : ".mysql_errno().", contate o administrador\");</script>");
if(mysql_affected_rows() > 0)
{
$msg = "NFe Gerada com sucesso";
$aux=1;
}else
{
$msg="2 - Nenhum arquivo afetado";
$aux=0;
};
};
$str2="";
$update="";
};
if($aux==1){
$i=0;
//Atualiza status de Item
foreach($ItemQTD as $x){
$split=explode("|",$x);
$Itempk[$i]=$split[0];
if(!empty($split[1])){
$un[$Itempk[$i]]=$split[1];
}
$i++;
}
foreach($Itempk as $x){
$sql = "select unidades,n_ped_doberfk from financeiro_item where itempk=$x";
$QTD_Item = mysql_fetch_assoc(mysql_query($sql));
$UnAtual = $QTD_Item['unidades']-$un[$x];
if($UnAtual==0){
$Faturado = "update financeiro_item set status='FATURADO',NFe='$B12',unidades=$un[$x] where itempk=$x";
mysql_query($Faturado);
if(mysql_affected_rows() > 0)
{
//echo "<script type\"text/javascript\">alert(\"OK\");</script>";
}else
{
echo $Faturado;
echo "<script type\"text/javascript\">alert(\"ERRO STATUS FATURADO , contate o administrador\");</script>";
};
}else{
$PARCIAL = "update financeiro_item set status='PARCIAL',NFe='$B12',unidades=$un[$x] where itempk=$x";
mysql_query($PARCIAL);
if(mysql_affected_rows() > 0)
{
//echo "<script type\"text/javascript\">alert(\"OK\");</script>";
}else
{
echo "<script type\"text/javascript\">alert(\"ERRO STATUS PARCIAL, contate o administrador\");</script>";
};
}
$sqlPedido="select count(status) as cont from financeiro_item
where
n_ped_doberfk=$QTD_Item[n_ped_doberfk]
and
status=\"PENDENTE\" or status = \"PARCIAL\"";
$cont= mysql_fetch_assoc(mysql_query($sqlPedido));
if($cont['cont']=="0"){
$up_ped = "update financeiro_pedido set status=\"FATURADO\" where n_ped_dober='$QTD_Item[n_ped_doberfk]'";
mysql_query($up_ped);
if(mysql_affected_rows() > 0)
{
//echo "<script type\"text/javascript\">alert(\"PEDIDO OK\");</script>";
}else
{
echo "<script type\"text/javascript\">alert(\"ERRO STATUS PEDIDO $up_ped, contate o administrador\");</script>";
};
}
//echo $sql ."</br>";
}
$sqlView = "select $colunas from financeiro_nf where b12 = $B12";
$NFe = mysql_fetch_assoc(mysql_query($sqlView));
//echo $sqlView."</br>";
$txt= "NOTA FISCAL|1|
A|$NFe[A2]|$NFe[A3]|$NFe[A4]|
B|$NFe[B6]|$NFe[B7]|$NFe[B8]|$NFe[B9]|$NFe[B10]|$NFe[B11]|$NFe[B12]|$NFe[B13]|$NFe[B14]|$NFe[B15]|$NFe[B15A]|$NFe[B16]|$NFe[B25]|$NFe[B26]|$NFe[B27]|$NFe[B28]|$NFe[B29]|$NFe[B291]|$NFe[B292]|$NFe[B29A]|$NFe[B29]|$NFe[B]|$NFe[B29C]|$NFe[B29D]|";
if($B29!="1"){
$txt.="B02|$NFe[B02]|" ;
}
$txt.="
C|$NFe[C32]|$NFe[C33]|$NFe[C46]|$NFe[C47]|$NFe[C48]|$NFe[C49]|$NFe[C49A]|
C02|$NFe[C02]|
C05|$NFe[C0535]|$NFe[C0536]|$NFe[C0537]|$NFe[C0538]|$NFe[C0539]|$NFe[C0540]|$NFe[C0541]|$NFe[C0542]|$NFe[C0543]|$NFe[C0544]|$NFe[C0545]|
E|$NFe[E65]|$NFe[E77A]|$NFe[E78]|$NFe[E79]|$NFe[E791]|$NFe[E79A]|
E02|$NFe[E02]|";
if($B15a=="3"){
$txt.="
E03a|$NFe[E03A]|" ;
}
$txt.="
E05|$NFe[E0567]|$NFe[E0568]|$NFe[E0569]|$NFe[E0570]|$NFe[E0571]|$NFe[E0572]|$NFe[E0573]|$NFe[E0574]|$NFe[E0575]|$NFe[E0576]|$NFe[E0577]|";
if(!empty($NFe['G91'])){
$txt.="
G|$NFe[G91]|$NFe[G92]|$NFe[G93]|$NFe[G94]|$NFe[G95]|$NFe[G96]|$NFe[G97]|
G02|$NFe[G02]|";
}
$txt.=$I;
$txt.="
W|
W02|$NFe[W02328]|$NFe[W02329]|$NFe[W0232901]|$NFe[W02330]|$NFe[W02331]|$NFe[W02332]|$NFe[W02333]|$NFe[W02334]|$NFe[W02335]|$NFe[W02336]|$NFe[W02337]|$NFe[W02338]|$NFe[W02339]|$NFe[W02340]|$NFe[W02341]|$NFe[W02341A]|
W23||||||||
X|$NFe[X]|
X03|$NFe[X03361]|$NFe[X03362]|$NFe[X03363]|$NFe[X03364]|$NFe[X03365]|
X04|$NFe[X04]|
X26|$NFe[X26382]|$NFe[X26383]|$NFe[X26384]|$NFe[X26385]|$NFe[X26386]|$NFe[X26387]|
Y|
Y07|$NFe[Y07396_1]/01|$NFe[Y07397_1]|$NFe[Y07398_1]|";
if(!empty($NFe['Y07396_2'])){
$txt.="
Y07|$NFe[Y07396_2]/02|$NFe[Y07397_2]|$NFe[Y07398_2]|";
};
if(!empty($NFe['Y07396_3'])){
$txt.="
Y07|$NFe[Y07396_3]/03|$NFe[Y07397_3]|$NFe[Y07398_3]|";
};
$txt.="
Z|$NFe[Z400]|$NFe[Z401]|";
$data=date("d_m_Y");
$AnoMes = date("Ym");
$filename = "NFe/$AnoMes";
if (!(file_exists($filename))) {
mkdir("NFe/$AnoMes", 0777);
}
$nome="NFe_".$B12."_MTZ_1_".$data;
$diretorio = "//Versao 310";
if (!file_exists($diretorio)) {
mkdir($diretorio, 0700);
}
$arquivo = fopen("$diretorio/$nome.txt", "w");
//$texto = ereg_replace("[^a-zA-Z0-9_]", "", strtr($txt, "áàãâéêíóôõúüçÁÀÃÂÉÊÍÓÔÕÚÜÇ ", "aaaaeeiooouucAAAAEEIOOOUUC_"));
$texto=addslashes($txt);
//$texto = preg_replace('/(\'|")/', "", $texto);
fwrite($arquivo, $texto);
fclose($arquivo);
//echo $nome;
echo "<script type\"text/javascript\">alert(\"$msg $nome\");history.go(-2);</script>";
}else{
echo "<script type\"text/javascript\">alert(\"$msg $nome\");history.go(-2);</script>";
}
?>
Posting your code in PHP / Mysql would help a lot to elaborate a response, without it you can not identify what is replacing the characters.
– g.carvalho97
Hello, I’m sorry, I just put the code. Thank you
– Thiago
Already tried to set via shortcuts: For (") is: ALT+0147 and for (") is: ALT+0148
– checktech