我在互联网的帮助下放了一些PHP代码。我不是很擅长php我想从不同的页面上的txt文件存储更新SQL表
txt文件的结构如下:
1006704;1000;1000;1000;1000;1000;1000;198861;198861;198861;198861;198861;198861
1006705;1000;1000;1000;1000;1000;1000;98871;98871;98871;98871;98871;98871
1006709;1000;1000;1000;1000;1000;1000;48876;48876;48876;48876;48876;48876
这是php:
<?php
$connect = mysql_connect('localhost','something','pass');
if (!$connect) {
die('Could not connect to MySQL: ' . mysql_error());
}
$cid =mysql_select_db('database',$connect);
$text = file_get_contents('http://somedomainname/stock/something.txt');
$text = explode("\n",$text);
foreach($text as $line)
{
$temp = explode('Macma_Item_No:',$line);
$Macma_Item_No = explode(";",$temp[0]);
$temp = explode('Local_Inventory:',$line);
$Local_Inventory = explode(";",$temp[0]);
$temp = explode('Local_Future_2:',$line);
$Local_Future_2 = explode(";",$temp[0]);
$temp = explode('Local_Future_3:',$line);
$Local_Future_3 = explode(";",$temp[0]);
$temp = explode('Local_Future_4:',$line);
$Local_Future_4 = explode(";",$temp[0]);
$temp = explode('Local_Future_5:',$line);
$Local_Future_5 = explode(";",$temp[0]);
$temp = explode('Local_Future_6:',$line);
$Local_Future_6 = explode(";",$temp[0]);
$temp = explode('Inernational_Inventory:',$line);
$Inernational_Inventory = explode(";",$temp[0]);
$temp = explode('Inernational_Future_2:',$line);
$Inernational_Future_2 = explode(";",$temp[0]);
$temp = explode('Inernational_Future_3:',$line);
$Inernational_Future_3 = explode(";",$temp[0]);
$temp = explode('Inernational_Future_4:',$line);
$Inernational_Future_4 = explode(";",$temp[0]);
$temp = explode('Inernational_Future_5:',$line);
$Inernational_Future_5 = explode(";",$temp[0]);
$temp = explode('Inernational_Future_6:',$line);
$Inernational_Future_6 = explode(" ",$temp[0]);
$sql = "INSERT INTO oc_stock_international2 (Macma_Item_No,Local_Inventory,Local_Future_2,Local_Future_3,Local_Future_4,Local_Future_5,Local_Future_6,Inernational_Inventory,Inernational_Future_2,Inernational_Future_3,Inernational_Future_4,Inernational_Future_5,Inernational_Future_6) VALUES ('".$Macma_Item_No[0]."','".$Local_Inventory[0]."','".$Local_Future_2[0]."','".$Local_Future_3[0]."','".$Local_Future_4[0]."','".$Local_Future_5[0]."','".$Local_Future_6[0]."','".$Inernational_Inventory[0]."','".$Inernational_Future_2[0]."','".$Inernational_Future_3[0]."','".$Inernational_Future_4[0]."','".$Inernational_Future_5[0]."','".$Inernational_Future_6[0]."')";
mysql_query($sql);
}
?>
现在在这种状态下,它成功地从文件中的每一列写入相同的数据,但是当我将分隔符更改为不同的内容时,所有数据都将出现在第一列中。我确信这只是一个简单的错误,可能第一个看到这个会想到这一点,但谢谢所有愿意帮助的人
答案 0 :(得分:0)
哟需要更改这部分代码:
foreach($text as $line)
{
$parts = explode(";", $line);
$Macma_Item_No = $parts[0];
$Local_Inventory = $parts[1];
$Local_Future_2 = $parts[2];
$Local_Future_3 = $parts[3];
$Local_Future_4 = $parts[4];
$Local_Future_5 = $parts[5];
$Local_Future_6 = $parts[6];
$Inernational_Inventory = $parts[7];
$Inernational_Future_2 = $parts[8];
$Inernational_Future_3 = $parts[9];
$Inernational_Future_4 = $parts[10];
$Inernational_Future_5 = $parts[11];
$Inernational_Future_6 = $parts[12];
....
但是这段代码更清晰:
list($Macma_Item_No, $Local_Inventory, $Local_Future_2,
$Local_Future_3, $Local_Future_4, $Local_Future_5,
$Local_Future_6 ,$Inernational_Inventory,
$Inernational_Future_2, $Inernational_Future_3,
$Inernational_Future_4, $Inernational_Future_5,
$Inernational_Future_6) = explode(";", $line);
对于SQL部分,请更改为:
$sql = "INSERT INTO oc_stock_international2 (Macma_Item_No,Local_Inventory,Local_Future_2,Local_Future_3,Local_Future_4,Local_Future_5,Local_Future_6,Inernational_Inventory,Inernational_Future_2,Inernational_Future_3,Inernational_Future_4,Inernational_Future_5,Inernational_Future_6) VALUES ('$Macma_Item_No', '$Local_Inventory', '$Local_Future_2', '$Local_Future_3', '$Local_Future_4', '$Local_Future_5', '$Local_Future_6', '$Inernational_Inventory', '$Inernational_Future_2', '$Inernational_Future_3', '$Inernational_Future_4', '$Inernational_Future_5', '$Inernational_Future_6')";
SQL部分
有人建议:
更新:
我更改了SQL部分,将$
添加到最后四列,并将'
放到参数中,以便在开始时允许零。