这里,我想将csv中的记录插入mysql数据库,但结果为空。 我有csv在mysql中插入记录但结果为空。 在这里,当我要运行此文件时,它显示为空白。 问题在哪里?
import.php:
function connect() {
$host = 'localhost';
$db_name = 'test';
$db_user = 'abc';
$db_password = '1234';
return new PDO('mysql:host='.$host.';dbname='.$db_name, $db_user, $db_password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}
$pdo = connect();
$ftp_server= "ftp.com";
$euuser = "ftpusername";
$eupassword = "ftppwd";
$conn_id = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login_result = ftp_login($conn_id, $euuser, $eupassword);
$path=ftp_pwd($conn_id)."magento_prd/orderitems100050284.csv";
//echo "hello".$conn_id;
//exit;
$contents = ftp_nlist($conn_id, $path);
foreach ($contents as $file)
{
echo "<br>$file";
}
ftp_close($conn_id);
//exit;
//echo "hello";exit;
function insert_read_file($file){
$csv_file =$file;
if (is_file($csv_file)) {
if ($_FILES['csv_file']['size'] <= 0) {
$errorMsg.="Please select tab delimited .txt";
die();
} else {
$upload_file_extension = strtolower(end(explode('.', $file)));
if (strtolower($upload_file_extension) != 'txt') {
echo "not txt file";
die();
}
}
$column=Array ('Full Name','Source','Order ReferenceNum','Postal Service Name','Tracking Number','SourceUserName' ,'Phone Number');
if (($input = fopen($csv_file, "r")) !== FALSE) {
$row = fgetcsv($input, 1024, ',');
$re=array_diff($column,$row);
if(sizeof($re)!=0)
{
$errorMsg='csv not matched';
}
if(sizeof($row)==1){
$errorMsg='csv is blank';
}
$count = 0;
while (($row = fgetcsv($input)) !== FALSE) {
//print_r($row);
echo "</br>";
$sql = 'INSERT INTO tbl_order_details(order_id, orderdet_productsku, orderdet_iscase, orderdet_qty, item_sold_price) VALUES'
. '(:order_id, :orderdet_productsku, :orderdet_iscase, :orderdet_qty, :item_sold_price)';
$query = $pdo->prepare($sql);
$query->bindParam(':order_id', $row[0],PDO::PARAM_STR);
$query->bindParam(':orderdet_productsku', $row[1],PDO::PARAM_STR);
$query->bindParam(':orderdet_iscase',$row[2], PDO::PARAM_STR);
$query->bindParam(':orderdet_qty', $row[3],PDO::PARAM_STR);
$query->bindParam(':item_sold_price', $row[4],PDO::PARAM_STR);
$query->execute();
$count++;
}
}
}
else {
$errorMsg='not csv';
}
}
答案 0 :(得分:0)
只需更改一行:
$sql = 'INSERT INTO tbl_order_details(order_id, orderdet_productsku, orderdet_iscase, orderdet_qty, item_sold_price) VALUES'
. '(:order_id, :orderdet_productsku, :orderdet_iscase, :orderdet_qty, :item_sold_price)';
以下代码:
$sql = 'INSERT INTO tbl_order_details ( order_id, orderdet_productsku, orderdet_iscase, orderdet_qty, item_sold_price)
VALUES(:order_id,:orderdet_productsku,:orderdet_iscase,:orderdet_qty,:item_sold_price)';
注意:无需在(.) period
之前使用 VALUES
。
或更好使用如下:
$sql = 'INSERT INTO tbl_order_details SET order_id = :order_id,
orderdet_productsku = :orderdet_productsku, orderdet_iscase = :orderdet_iscase,
orderdet_qty = :orderdet_qty, item_sold_price = :item_sold_price';